user1079877
user1079877

Reputation: 9368

Remove all the strings out of specific tag in bash

Suppose I have this file:

this is 1th line
this is 2th line
<<EOM
this is 3th line
EOM>>
this is 4th line
<<EOM
this is 5th line
EOM>>
this is 6th line

Is there anyway to run a command in bash to get:

this is 3th line
this is 5th line

and get the full text when there's no <<EOM on it?

Upvotes: 0

Views: 98

Answers (2)

jijinp
jijinp

Reputation: 2662

grep -q "<<EOM" file && awk '/<<EOM/{flag=1;next}/EOM>>/{flag=0}flag' file || cat file

if <<EOM is found in file it will print the lines between <<EOM and EOM>> with awk. Else just cat the file

Upvotes: 2

mauro
mauro

Reputation: 5950

This should work:

$ sed -n '/<<EOM/,/EOM>>/p' file | grep -v EOM || cat file

I mean...

  1. It should print lines between EOM markers if they are present or:
  2. print the whole file if EOMs are not there

And... this is better (avoid removing lines between markers containing EOM):

$ sed -n '/<<EOM/,/EOM>>/p' file | grep -vw '<<EOM\|EOM>>' || cat file

Upvotes: 4

Related Questions