由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - what's wrong with this scripts?variable passing?
相关主题
how to sed from grep output in c shell? (转载)请教个script问题
How to fix this? /bin/ls: Argument list too longshould the .dll and .lib have the same name?
C语言一个passing variable的问题a PERL opendir/readdir question
how to check the value of $(IntDir) in visual stdioregister variable
一个batch file的问题lambda的一个疑问
怎么实现这个alias (转载)sql 数据是存在哪里 (转载)
Shell script 问题C++里面把数转成字符串的命令是啥啊?
怎么把string变为一个variable的名字 ?how to print 2 exponential digits in windows by using Perl
相关话题的讨论汇总
话题: wordfile话题: total话题: grep话题: doc话题: scripts
进入Programming版参与讨论
1 (共1页)
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.

1 (共1页)
进入Programming版参与讨论
相关主题
how to print 2 exponential digits in windows by using Perl一个batch file的问题
关于文件命名怎么实现这个alias (转载)
C# HtmlElement.InvokeMember at Amazon.comShell script 问题
how to program a shell extension怎么把string变为一个variable的名字 ?
how to sed from grep output in c shell? (转载)请教个script问题
How to fix this? /bin/ls: Argument list too longshould the .dll and .lib have the same name?
C语言一个passing variable的问题a PERL opendir/readdir question
how to check the value of $(IntDir) in visual stdioregister variable
相关话题的讨论汇总
话题: wordfile话题: total话题: grep话题: doc话题: scripts