开发者

How to do complex replacements over multiple lines using sed?

I want to do this su开发者_如何转开发bstitution in a set of files :

replace :

KEY
KEY1|VAL1
KEY
KEY2|VAL2

by :

KEY
KEY1|VAL1

Eg :

KEY
KEY|sde
KEY
KEY|45g

by

KEY
KEY|sde

In short, I need to remove the second occurence of KEY \n KEY|VAL pair. I am not good at using sed.


If I understand your problem correctly (and I might not), this is one solution:

sed -ne '/^KEY$/{p;n;p;n;n}' file

Explanation:

When finding KEY in a line by itself:

  • p - print the line
  • n;p - go to the next line and print it too.
  • n;n - skip the next 2 lines.

... and repeat.

Edit (explanation continued):

  • The -n tell sed not to print a line unless explicitly directed to do so (with p).
  • The -e is not strictly required here. It says the following string is a sed expression.


sed -e 'N;/key2\|val2/ d' your_file.txt should do the trick, note the \ is to quote the | otherwise your shell might interprete the command wrongly.


you can try using awk. Note, only the 2nd instance of your required pattern is changed.

# cat file
KEY
KEY1|VAL1
blah
blah
KEY
KEY2|VAL2
junk
junk
KEY
KEY3|VAL3
KEY
KEY4|VAL4
KEY
KEY5|VAL5

$ awk '/^KEY$/&&!f&&!x{s=$0;getline;s=s"\n"$0;f=1;print s;next}f&&/^KEY$/{print s;getline;x=1;f=0;next}1' file
KEY
KEY1|VAL1
blah
blah
KEY
KEY1|VAL1
junk
junk
KEY
KEY3|VAL3
KEY
KEY4|VAL4
KEY
KEY5|VAL5


Is it a problem if you use other tools also? I just tried:

sort "file" | uniq | sed '/KEY2/d'

You can even emulate uniq using sed, check: http://sed.sourceforge.net/sed1line.txt

Hope that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜