0

How to cat or echo everything between two words in a file ?

eg

df
df
Instance 
d
f
g
end

So I want everything between Instance and end

Thanks

Jotne
  • 40,548
  • 12
  • 51
  • 55
Roopak Vasa
  • 117
  • 1
  • 6

4 Answers4

4

This might work for you (GNU sed):

sed -n '/^Instance/,/^end/{//d;p}' file

or as Glenn pointed out:

sed -n '/^Instance/,/^end/{//!p}' file
potong
  • 55,640
  • 6
  • 51
  • 83
2

This should be correct awk

awk '/end/{f=0} f; /Instance/{f=1}' file
d
f
g

PS, data was not in one line. OP was missing code tags, so showed all in one line ...

Jotne
  • 40,548
  • 12
  • 51
  • 55
1
awk '{match($0,/Instance(.*)end/,a); print a[1]}' input.txt

Output:

 d f g 
a5hk
  • 7,532
  • 3
  • 26
  • 40
  • If there are more than one `end`, `.*` this greedy star will eat everything. But anyway, its wrong approach of you look at corrected data in OPs question. – Jotne Apr 24 '14 at 12:56
  • 1
    If there are more then one `end` or `Instance`, then we should know what is the desired behavior, find longest, shortest, nth occurrence – a5hk Apr 24 '14 at 13:29
0
sed -n '/^Instance/,/^end/{/^Instance/!{/^end/!p}}' file
d
f
g
iruvar
  • 22,736
  • 7
  • 53
  • 82