shell > cat str.base i love linux and you LOVE liux too i love love love love love you i like linux ################ shell > cp -f str.base str.txt ; sed -n '/love/p' str.txt i love linux and you LOVE liux too i love love love love love you shell > cp -f str.base str.txt ; sed -n '/love/=' str.txt 1 2 ################ shell > cp -f str.base str.txt ; sed -i '/love/i1' str.txt ; cat str.txt 1 i love linux and you LOVE liux too 1 i love love love love love you i like linux shell > cp -f str.base str.txt ; sed -i '/love/a1' str.txt ; cat str.txt i love linux and you LOVE liux too 1 i love love love love love you 1 i like linux shell > cat add.txt aaa shell > cp -f str.base str.txt ; sed -i '/love/radd.txt' str.txt ; cat str.txt i love linux and you LOVE liux too aaa i love love love love love you aaa i like linux shell > cp -f str.base str.txt ; sed -i '/love/wnew.txt' str.txt ; cat new.txt i love linux and you LOVE liux too i love love love love love you ################ shell > cp -f str.base str.txt ; sed -i '/love/d' str.txt ; cat str.txt i like linux ################ shell > sed 's/love/ai/' str.base i ai linux and you LOVE liux too i ai love love love love you i like linux shell > sed 's/love/ai/g' str.base i ai linux and you LOVE liux too i ai ai ai ai ai you i like linux shell > sed 's/love/ai/2g' str.base i love linux and you LOVE liux too i love ai ai ai ai you i like linux shell > sed 's/love/ai/2' str.base i love linux and you LOVE liux too i love ai love love love you i like linux shell > sed 's/love/ai/ig' str.base i ai linux and you ai liux too i ai ai ai ai ai you i like linux
反向引用
& 和 \1 可以引用模式匹配到的整个串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
shell > sed -i 's/love/&s/g' str.base ; cat str.base i loves linux and you LOVE liux too i loves loves loves loves loves you i like linux #&可以引用匹配到的整个串 shell > sed -i 's/l...s/&AAA/g' str.base ; cat str.base i lovesAAA linux and you LOVE linux too i lovesAAA lovesAAA lovesAAA lovesAAA lovesAAA you i like linux #\1可以引用整个串,也可以引用部分。所有需要反向引用的部分,需要使用\(\)扩起来。 shell > sed -i 's/\(l...s\)AAA/\1BBB/g' str.base ; cat str.base i lovesBBB linux and you LOVE linux too i lovesBBB lovesBBB lovesBBB lovesBBB lovesBBB you i like linux shell >