Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

linux - Sed to search regex pattern and remove line with above pattern

this is my file.txt content. Here I'm trying to search pattern text: 'About' and remove items:[{ line with the command as below:

file.txt:

                items: [{
                        text: 'About',
                        handler: function() { TP.aboutWindow() }

Command: sed -n -i -E '/text: 'About'/{n; $p; x; d}; x; 1!p; ${x;p;}' file.txt

but it seems to be not working. Is there any way to make it work to remove line above pattern?

Thanks in advance.

question from:https://stackoverflow.com/questions/65931991/sed-to-search-regex-pattern-and-remove-line-with-above-pattern

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use

sed -i "N;/
.*text: 'About'/"'!P;D' file.txt

Details:

  • -i - file inplace replacement mode on
  • N - opens a two-line window throughout the whole file (it reads the next line prepended with a newline into pattern space)
  • .*text: 'About' matches a newline, then any text and then text: 'About'
  • !P;D - if there is a match, do not print the first line, then delete the first line and repeat.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...