Udit Gupta
Udit Gupta

Reputation: 3272

bash command to remove contents b/w two blocks from file

How to remove everything from file contained b/w two lines that have a specific word ?

For e.g. -

File:

Outstanding ..
dd  
gd
count ...
text2
text1  
text3

Outstanding ..
dd 
gswrff
dddfdd
count
text3
text4
text5     

The Output should be like this:

 text2
 text1
 text3
 text3
 text4
 text5

Upvotes: 0

Views: 73

Answers (2)

anubhava
anubhava

Reputation: 785196

Question isn't very clear. But if you're looking to delete text between 2 specific words then you can use this sed one-liner:

sed -i.bak '/Outstanding/,/count/d' file

OUTPUT:

text2
text1  
text3

text3
text4
text5  

Upvotes: 1

Adam Liss
Adam Liss

Reputation: 48290

If you're looking to capture only the lines with the word text in them:

grep text < file

or

sed -n /text/p < file

or

awk '/text/ { print }' < file

Upvotes: 1

Related Questions