1

I've used sed to print all lines starting from one pattern ending in another:

Lets say file1 contents are these:

outside-text1
==START
inner-text1
==END
outside-text2
==START
inner-text2
==END

The command:

sed -n '/==START/,/==END/p' file1

will print :

==START
inner-text1
==END
==START
inner-text2
==END

In my case I would like to print:

outside-text1
outside-text2
Marinos An
  • 9,481
  • 6
  • 63
  • 96
  • Possible duplicate of [Remove lines which are between given patterns from a file (using Unix tools)](https://stackoverflow.com/questions/1996585/remove-lines-which-are-between-given-patterns-from-a-file-using-unix-tools) – Sundeep Jan 25 '18 at 16:02

3 Answers3

2

Negate the match with !.

$ sed -n '/==START/,/==END/!p' file1
outside-text1
outside-text2
iamauser
  • 11,119
  • 5
  • 34
  • 52
2

Or even easier, just use the d (delete) function to delete from the start to end patterns:

$ sed '/==START/,/==END/d' file.txt
outside-text1
outside-text2

It's just a cleaner alternative to suppressing printing of pattern-space and negating the print.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
1

Following awk may help you in same.

awk '/^==END/{flag="";next} /^==START/{flag=1;next} !flag'   Input_file

OR

awk '/^==START/{flag=1;} /^==END/{flag="";next} !flag'  Input_file

OR

awk '/^==START/{flag=1} !flag;  /^==END/{flag=""}'  Input_file

Output will be as follows.

outside-text1
outside-text2

Solution 2nd: Following sed may also help you in same.

sed  '/==START/,/==END/d'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93