November 14, 2003

perl,sed replace, append sample

在 ccc 後面加上一行 xxx

# cat test
aaa
bbb
ccc
ddd

# perl -pi -e "s/ccc/ccc\nxxx/" test

# cat test
aaa
bbb
ccc
xxx
ddd

把 xxx 換成 yyy
# perl -pi -e "s/xxx/yyy/" test

# cat test
aaa
bbb
ccc
yyy
ddd

把檔案中含有 xxx 字串的下一行加上一行 new line
# cat test
aaa 111
bbb 222
ccc 333
555 xxx 444
ddd kkkkkkkkkkkkk
xxx 666
mmmmmmmmmmmmm
lllllllllllllllllxxx
eeeeeee

# sed -e '/xxx/a\
new line' test > test.tmp

# mv test.tmp test

# cat test
aaa 111
bbb 222
ccc 333
555 xxx 444
new line
ddd kkkkkkkkkkkkk
xxx 666
new line
mmmmmmmmmmmmm
lllllllllllllllllxxx
new line
eeeeeee

Posted by pank at November 14, 2003 09:06 AM
Comments
Post a comment













Remember personal info?