有多种条件的sed

时间:2014-07-27 06:25:57

标签: regex replace sed

我希望在使用sed命令搜索/替换文件中的文本方面得到您的帮助,具体目的如下。
假设我/tmp/foo有以下内容。

aaa whatever_a1 KEYWORD_1 whatever_a2
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
aaa whatever_aaa1 whatever_aaa2
aaa whatever_aaa1 whatever_aaa2 KEYWORD_1 KEYWORD_2
ddd whatever_d1 whatever_d2 KEYWORD_1

回报应如下:

1。找到/ tmp / foo哪些行包含aaa whatever_a1 KEYWORD_1 whatever_a2>>>将行替换为Hello World

Hello World
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
aaa whatever_aaa1 whatever_aaa2
aaa whatever_aaa1 whatever_aaa2 KEYWORD_1 KEYWORD_2
ddd whatever_d1 whatever_d2 KEYWORD_1


2。 find / tmp / foo哪些行以aaa>>>开头将行替换为Hello World

Hello World
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
Hello World
Hello World
ddd whatever_d1 whatever_d2 KEYWORD_1


3。找到/ tmp / foo哪些行包含aaaKEYWORD_1>>>将行替换为Hello World

Hello World
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
aaa whatever_aaa1 whatever_aaa2
Hello World
ddd whatever_d1 whatever_d2 KEYWORD_1


4。在开始时找到/ tmp / foo哪些行包含aaa AND KEYWORD_1>>>将行替换为Hello World

Hello World
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
aaa whatever_aaa1 whatever_aaa2
Hello World
ddd whatever_d1 whatever_d2 KEYWORD_1


5。找到/ tmp / foo哪些行包含aaaKEYWORD_1>>>仅将KEYWORD_1替换为NEW_TEXT

aaa whatever_a1 NEW_TEXT whatever_a2
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
aaa whatever_aaa1 whatever_aaa2
aaa whatever_aaa1 whatever_aaa2 NEW_TEXT KEYWORD_2
ddd whatever_d1 whatever_d2 KEYWORD_1


6。找到/ tmp / foo哪些行包含aaaKEYWORD_1>>>仅清除KEYWORD_1文字

aaa whatever_a1  whatever_a2
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
aaa whatever_aaa1 whatever_aaa2
aaa whatever_aaa1 whatever_aaa2  KEYWORD_2
ddd whatever_d1 whatever_d2 KEYWORD_1


7。找到/ tmp / foo哪些行包含aaaKEYWORD_1>>>仅将KEYWORD_1后面的字符串替换为Hello World(将KEYWORD_1保留在行中)

aaa whatever_a1 KEYWORD_1 Hello World
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
aaa whatever_aaa1 whatever_aaa2
aaa whatever_aaa1 whatever_aaa2 KEYWORD_1 Hello World
ddd whatever_d1 whatever_d2 KEYWORD_1


8。找到/ tmp / foo哪些行包含aaaKEYWORD_1>>>仅将KEYWORD_1的字符串前面替换为Hello World(将KEYWORD_1保留在行中)

Hello World KEYWORD_1 whatever_a2
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
aaa whatever_aaa1 whatever_aaa2
Hello World KEYWORD_1 KEYWORD_2
ddd whatever_d1 whatever_d2 KEYWORD_1


9。找到/ tmp / foo哪些行包含aaaKEYWORD_1>>>仅将KEYWORD_1后面的字符串替换为Hello World(包括KEYWORD_1文字)

aaa whatever_a1 Hello World
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
aaa whatever_aaa1 whatever_aaa2
aaa whatever_aaa1 whatever_aaa2 Hello World
ddd whatever_d1 whatever_d2 KEYWORD_1


10。找到/ tmp / foo哪些行包含aaaKEYWORD_1>>>仅将KEYWORD_1的字符串前置替换为Hello World(包括KEYWORD_1文本)

Hello World whatever_a2
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
aaa whatever_aaa1 whatever_aaa2
Hello World KEYWORD_2
ddd whatever_d1 whatever_d2 KEYWORD_1


11。找到/ tmp / foo哪些行包含aaaKEYWORD_1 AND KEYWORD_2>>>将行替换为Hello World

aaa whatever_a1 KEYWORD_1 whatever_a2
bbb whatever_b1 whatever_b2
ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
aaa whatever_aaa1 whatever_aaa2
Hello World
ddd whatever_d1 whatever_d2 KEYWORD_1

非常感谢,
的Cuong

2 个答案:

答案 0 :(得分:1)

sed 's/aaa whatever_a1 KEYWORD_1 whatever_a2/Hello World/g' name_of_file.txt 

(或者您拥有的任何文件格式)...然后执行#1然后对特定行进行特定更改,只需执行

sed -e '2s/from_this/to_this/' 

其中2表示第2行,或5表示第5行,等等。 Sed可以产生一些非常强大的单线。

答案 1 :(得分:0)

你可能对awk更开心,例如:

  1. 查找/ tmp / foo哪些行包含aaa whatever_a1 KEYWORD_1 whatever_a2>>>将行替换为Hello World

    awk' / aaa whatever_a1 KEYWORD_1 whatever_a2 / {$ 0 =" Hello World" 1'的/ tmp /富

  2. 找到/ tmp / foo哪些行以aaa>>>开头将行替换为Hello World

    awk' / ^ aaa / {$ 0 =" Hello World" 1'的/ tmp /富

  3. 查找/ tmp / foo哪些行包含aaa AND KEYWORD_1>>>将行替换为Hello World

    awk' / aaa /&& / KEYWORD_1 / {$ 0 =" Hello World" 1'的/ tmp /富

  4. 在开头找到/ tmp / foo哪些行包含aaa AND KEYWORD_1>>>将行替换为Hello World

    awk' / ^ aaa /&& / KEYWORD_1 / {$ 0 =" Hello World" 1'的/ tmp /富

  5. 查找/ tmp / foo哪些行包含aaa AND KEYWORD_1>>>仅将KEYWORD_1替换为NEW_TEXT

    awk' / aaa / {sub(/ KEYWORD_1 /," NEW_TEXT")} 1'的/ tmp /富

  6. 查找/ tmp / foo哪些行包含aaa AND KEYWORD_1>>>仅清除KEYWORD_1文本

    awk' / aaa / {sub(/ KEYWORD_1 /,"")} 1'的/ tmp /富

  7. 查找/ tmp / foo哪些行包含aaa AND KEYWORD_1>>>将KEYWORD_1后面的字符串替换为仅Hello World(将KEYWORD_1保持在行中)

    awk' / aaa / {sub(/ KEYWORD_1。* /," KEYWORD_1 Hello World")} 1'的/ tmp /富

  8. 查找/ tmp / foo哪些行包含aaa AND KEYWORD_1>>>将KEYWORD_1的字符串前面替换为Hello World(将KEYWORD_1保持在行中)

    awk' / aaa / {sub(/.* KEYWORD_1 /," Hello World KEYWORD_1")} 1'的/ tmp /富

  9. 查找/ tmp / foo哪些行包含aaa AND KEYWORD_1>>>将KEYWORD_1后面的字符串替换为仅Hello World(包括KEYWORD_1文本)

    awk' / aaa / {sub(/ KEYWORD_1。* /," Hello World")} 1'的/ tmp /富

  10. 查找/ tmp / foo哪些行包含aaa AND KEYWORD_1>>>将KEYWORD_1的字符串前面替换为仅Hello World(包括KEYWORD_1文本)

    awk' / aaa / {sub(/.* KEYWORD_1 /," Hello World")} 1'的/ tmp /富

  11. 查找/ tmp / foo哪些行包含aaa AND KEYWORD_1 AND KEYWORD_2>>>将行替换为Hello World

    awk' / aaa /&& / KEYWORD_1 /&& / KEYWORD_2 / {$ 0 =" Hello World" 1'的/ tmp /富

  12. 请注意,如果您不想在替换文本sub(/KEYWORD.*/,"KEYWORD foo")中明确重复搜索到的密钥,则可以将GNU awk用于gensub():$0=gensub(/(KEYWORD).*/,"\1 foo","")。此外,如果您不想在另一个字符串的中间找到/替换KEYWORD,您可以使用GNU awk的\<\>字分隔符来隔离它它