荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: jjksam (eee), 信区: Linux
标  题: [合集]向大家请教一个替换问题!·!·
发信站: 荔园晨风BBS站 (Tue Nov 11 13:55:11 2003), 站内信件

atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 17:18:12 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

比如说我有近千个文件,想在这近千个文件中都追加一行字符“this is book“
我该如何做呢?大家有没有这样做过,对N多个文件同时进行写操作呢
大家帮帮我,我真的是急用,


dddddddd@bbs.pku.edu.cn (天下无山) 于Mon Oct 27 17:21:02 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

echo "this is a book" >> *



atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 17:30:55 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

执行时出错!·!·

[root@sw-www7 q]# echo "IN NS dns2.sinonets.net" >> *
-bash: *: ambiguous redirect


wy.bbs@bbs.hziee.edu.cn (围炉夜话站长) 于Tue Oct 28 09:50:46 2003)
提到:
转信站: SZU!news.ccie.net.cn!HZIEE

#!/bin/sh

for FILENAME in $(ls /somedir)
do
        echo "this is a book" >> $FILENAME
done


atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 17:55:07 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

提示错误,我按照你的原文进行操作的,新建了一个文件夹,
执行命令时出错:
./a: line 4: ehco: command not found


wy.bbs@bbs.hziee.edu.cn (围炉夜话站长) 于Tue Oct 28 10:01:59 2003)
提到:
转信站: SZU!news.ccie.net.cn!HZIEE

可能环境不一样,用下面的:

#!/bin/sh

for FILENAME in $(/bin/ls /somedir)
do
        /bin/echo "this is a test" >> /somedir/$FILENAME
done


hace.bbs@hace.dhs.org (半边海) 于Mon Oct 27 18:04:02 2003)
提到:
转信站: SZU!news.ccie.net.cn!hace

perl -e 'while(<*>){next if -d;open FH,">>$_" or next;print FH "this is book"}'


atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 18:05:09 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

还不行呀


wy.bbs@bbs.hziee.edu.cn (好好学习,天天向上) 于Tue Oct 28 10:10:44 2003)
提到:
转信站: SZU!news.ccie.net.cn!HZIEE

刚才没仔细看,原来是你打错字了

不是ehco ,而是echo

               ~

atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 18:11:35 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

非常感谢!·!

那我如何将此字符串插入某一关键字后面呢

比如说这近千个文件都有一关键字符串“this is desk"
我想插到这一串的后面
该如何执行,谢谢


Init.bbs@bbs.zju.edu.cn (RCSC) 于Mon Oct 27 18:07:34 2003)
提到:
转信站: SZU!news.ccie.net.cn!ZJUBBS

最好还是用find来做。 :)
不然遇到文件有空格之类的,那就又没的玩了。



Init.bbs@bbs.zju.edu.cn (RCSC) 于Mon Oct 27 18:08:36 2003)
提到:
转信站: SZU!news.ccie.net.cn!ZJUBBS

你看他写的,echo写成了ehco。。:(




Init.bbs@bbs.zju.edu.cn (RCSC) 于Mon Oct 27 18:11:48 2003)
提到:
转信站: SZU!news.ccie.net.cn!ZJUBBS

hi~~
if I want add this line on the head of the file,
must I use a temp file?

book"}'




jjksam (eee) 于Mon Oct 27 18:30:13 2003提到:

#!/bin/sh

find ./ -depth -name "*" -print | while read src ; do

  if ( ! [ -d ${src} ] && [ ${src} != $0 ])
  then
   echo "this is book" >> ${src}
  fi
done



atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 18:33:30 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

SORRY,是我表达错误

比如说有一目录,这个目录下有近千个文件,这近千个文件中都有一行"quit"
我想在每个文件的“quit"的后面回车
在下一行插入"this is book"
大哥,你能帮我写的完全些吗?我有急用


Init.bbs@bbs.zju.edu.cn (RCSC) 于Mon Oct 27 18:30:31 2003)
提到:
转信站: SZU!news.ccie.net.cn!ZJUBBS

要不就用sed了,
汗,好象避免不了用temp文件了。
不知道哪个效率高一点。



atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 18:43:29 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

大哥,帮我想想吧


jjksam (eee) 于Mon Oct 27 18:52:38 2003提到:

回车不知道怎么弄了,^M不行。

#!/bin/sh

find ./ -depth -name "*" -print | while read src ; do

  if ( ! [ -d ${src} ] && [ ${src} != $0 ] )
  then
   if(! [ -e ${src}.tmp ])
   then
   {
    tmp=${src}
    sed -e 's/quit/quit\ this is a book/g' ${src} > ${tmp}.tmp
    mv -f ${tmp}.tmp ${src}
   }
   fi
  fi
done


atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 18:59:18 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

多谢了


jjksam (eee) 于Mon Oct 27 19:07:31 2003提到:

晕,sed的换行不知道怎么弄了。^M和\n都不行的。



Init.bbs@bbs.zju.edu.cn (RCSC) 于Mon Oct 27 19:01:52 2003)
提到:
转信站: SZU!news.ccie.net.cn!ZJUBBS

你试验过了?
偶试了一下不行咯。



atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 19:11:24 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

每个文件中只有一行quit
但是执行第一个命令好没反映,还是原来的记录,一点没变


hace.bbs@hace.dhs.org (半边海) 于Mon Oct 27 19:16:19 2003)
提到:
转信站: SZU!news.ccie.net.cn!hace

不好意思, 想起了,是打漏了, :P
     perl -pi -e 's/quit\n/quit\nthis is book\n/g' *.c
     perl -pi -e 's/quit\n/quit\nthis is book\n/g' `find . -name "*.c"`


baggio.bbs@bbs.zju.edu.cn (Catcher of BBS) 于Mon Oct 27 19:12:09 2003)
提到:
转信站: SZU!news.ccie.net.cn!ZJUBBS


                    这个\n不要试试看




Init.bbs@bbs.zju.edu.cn (RCSC) 于Mon Oct 27 19:12:16 2003)
提到:
转信站: SZU!news.ccie.net.cn!ZJUBBS

[dhh@hhding test]$ cat 1
quit
hello world
[dhh@hhding test]$ sed 's/quit/quit\
this is a book/g' 1 > 1.tmp
[dhh@hhding test]$ cat 1.tmp
quit
this is a book
hello world
[dhh@hhding test]$




Init.bbs@bbs.zju.edu.cn (RCSC) 于Mon Oct 27 19:12:40 2003)
提到:
转信站: SZU!news.ccie.net.cn!ZJUBBS

没用,偶都试过了。-_-



atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 19:21:43 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

还是不行,还是提示
Can't open *.c: 没有那个文件或目录.


wy.bbs@bbs.hziee.edu.cn (好好学习,天天向上) 于Tue Oct 28 11:23:55 2003)
提到:
转信站: SZU!news.ccie.net.cn!HZIEE




atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 19:24:15 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.zixia.net!news.happynet.org!PKU

你这个只是替换了一个文件呀!·能不能全部替换呀


jjksam (eee) 于Mon Oct 27 19:28:53 2003提到:

晕倒~ 不过加到script里面就不行哦。
[dhh@hhding test]$ cat 1
quit
hello world
[dhh@hhding test]$ sed 's/quit/quit\
this is a book/g' 1 > 1.tmp
[dhh@hhding test]$ cat 1.tmp
quit
this is a book
hello world
[dhh@hhding test]$




atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 19:37:53 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

不知道为什么,现在执行
perl -e 'while(<*>){next if -d;open FH,">>$_" or next;print FH "this is
book"}'
这个命令只是在最后一行追加的
并不是换行再追加的


jjksam (eee) 于Mon Oct 27 19:54:32 2003提到:

当然会这样,因为>>重定向符就是append的功能。

不知道为什么,现在执行
perl -e 'while(<*>){next if -d;open FH,">>$_" or next;print FH "this is
book"}'
这个命令只是在最后一行追加的
并不是换行再追加的


Init.bbs@bbs.zju.edu.cn (RCSC) 于Mon Oct 27 19:46:34 2003)
提到:
转信站: SZU!news.ccie.net.cn!ZJUBBS

累死我了,弄好了。
这个在文件名有空格的情况下面也可以用。
[dhh@hhding test]$ more tr.sh
#!/bin/sh
# Get an unique tmp
tmp=fn`date +%N`
find ./ | while read src
do
if [ -d "${src}" ]
then
        echo "Alert! Dir $src found, no operate on it"
else
        echo "tr the file $src now ..."
        sed 's/quit/quit\
this is a book/g' "$src" > "$src.$tmp"
mv -f "$src.$tmp" "$src"
fi
done
echo "All finished, enjoy it!"

[dhh@hhding test]$



jjksam (eee) 于Mon Oct 27 19:59:40 2003提到:

哦,原来不能有空格的。
没想到这个。。


jjksam (eee) 于Mon Oct 27 20:02:33 2003提到:

改了一下.
#!/bin/sh

find ./ -type f -depth -name "*" -print | while read src ; do

  if ( [ ${src} != $0 ])
  then if(! [ -e ${src}.tmp ] )
   then
   {
     tmp=${src}
     sed -e 's/quit/&\
this is a book/g' ${src} > ${tmp}.tmp
     mv -f ${tmp}.tmp ${src}
   }
   fi
  fi
done
echo "done!"


atteck@bbs.pku.edu.cn (鹰钩鼻子蛤蟆嘴,魔族战士!) 于Mon Oct 27 20:03:21 2003)
提到:
转信站: SZU!news.ccie.net.cn!news.happynet.org!PKU

太好了,非常感谢~!
能不能再添点呀
比如说有些文件中本来就有“this is book"了
像这样的文件就略过



Init.bbs@bbs.zju.edu.cn (RCSC) 于Mon Oct 27 19:55:40 2003)
提到:
转信站: SZU!news.ccie.net.cn!ZJUBBS

一个疑问,
then if ( [ ${src} != $0 ]
         ~~~~~~~~~~~~~~~~这句干什么用啊?



jjksam (eee) 于Mon Oct 27 20:09:14 2003提到:

不要连自己都替换掉咯,$0表明运行的那个命令的名称,$1表示第一个参数,类推。


baggio.bbs@bbs.zju.edu.cn (Catcher of BBS) 于Mon Oct 27 20:03:44 2003)
提到:
转信站: SZU!news.ccie.net.cn!ZJUBBS



    自己加一个grep判断



wy.bbs@bbs.hziee.edu.cn (好好学习,天天向上) 于Tue Oct 28 12:43:19 2003)
提到:
转信站: SZU!news.ccie.net.cn!HZIEE

先写一个sed脚本,然后在shell脚本中调用
#!/bin/sed -f
/quit/ \a
this is a book

这样结构好一些


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店