r*****r 发帖数: 397 | 1 I have a directory with hundreds of *.doc files,and I want to search a word "t
otal", if the total value is greater than 50, then I want to copy this file to
a new directory,and change the name sequentially to file001.doc,file002.doc .
...sth like that.
So, when I grep "total" ,I got
file1.doc
total: 12
file2.doc
total: 13
file3.doc
total: 56
...
#! /bin/tcsh
foreach wordfile (*.doc)
grep total $wordfile | awk '($2 > 50) {com = sprintf("cp " $wordfile "
../new_dir");print c | a**n 发帖数: 313 | 2 Try this:
#! /bin/tcsh
foreach wordfile (*.doc)
grep total $wordfile | awk -v name=$wordfile '($2 > 50) {com = sprintf("cp " name "
../new_dir");print com;}'
end
【在 r*****r 的大作中提到】 : I have a directory with hundreds of *.doc files,and I want to search a word "t : otal", if the total value is greater than 50, then I want to copy this file to : a new directory,and change the name sequentially to file001.doc,file002.doc . : ...sth like that. : So, when I grep "total" ,I got : file1.doc : total: 12 : file2.doc : total: 13 : file3.doc
| r*****r 发帖数: 397 | 3 Hi, that works well, thanks!
But when I tried to use a variable "count" to keep track of how many files con
tain the search string and rename it sequentially in the new_dir, there is a p
roblem.
foreach wordfile (*.doc)
grep total $wordfile | awk -v name=$wordfile '($2 > 50) {count++;com = spr
intf("cp " name " ../new_dir/"count ".doc");print com;}'
end
Is this the same problem?I also tried to initialize this value outside the loo
p, sp, still got problem.
Can you see anything wrong with thi
【在 a**n 的大作中提到】 : Try this: : #! /bin/tcsh : foreach wordfile (*.doc) : grep total $wordfile | awk -v name=$wordfile '($2 > 50) {com = sprintf("cp " name " : ../new_dir");print com;}' : end
| a**n 发帖数: 313 | 4 what you wrote obviously is not what you want according to your description.
I am not a tcsh expert, and wonder who really uses this shell seriously for scripting.
You can use other scripting language such as perl to write one which is more portable.
I can provide another version which is not exactly you want. Good luck.
#! /bin/tcsh
set count=0
foreach wordfile (*.doc)
set count=`expr $count + 1`
grep total $wordfile | awk -v name=$wordfile -v cnt=$count '($2 > 50) { com = sprintf("
【在 r*****r 的大作中提到】 : Hi, that works well, thanks! : But when I tried to use a variable "count" to keep track of how many files con : tain the search string and rename it sequentially in the new_dir, there is a p : roblem. : foreach wordfile (*.doc) : grep total $wordfile | awk -v name=$wordfile '($2 > 50) {count++;com = spr : intf("cp " name " ../new_dir/"count ".doc");print com;}' : end : Is this the same problem?I also tried to initialize this value outside the loo : p, sp, still got problem.
|
|