Redian新闻
>
what's wrong with this scripts?variable passing?
avatar
what's wrong with this scripts?variable passing?# Programming - 葵花宝典
r*r
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
avatar
a*n
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

avatar
r*r
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

avatar
a*n
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。