0

my txt file is constructed like:

random text
random text
random text

start_here
important text
important text
important text
end_here

random text
random text

start_here
important text
important text
important text
end_here

...
start_here
...
end_here
...

i need to print everything between start_here and end_here (no matter if these lines are in or not) in a new file.

i tried with awk but the output file is empty.

awk '/start_here/,/end_here/' inputfile.txt > outputfile.txt

Where is my mistake?

user3352472
  • 99
  • 1
  • 7

3 Answers3

1

This works fine for me, but you can try:

awk '/start_here/{f=1} f; /end_here/{f=0}' file

or

awk '/start_here/{f=1} /end_here/{f=0;print} f' file

All gives this:

start_here
important text
important text
important text
end_here
start_here
important text
important text
important text
end_here
start_here
...
end_here

If you do not like the start/end text use this:

awk '/end_here/{f=0} f; /start_here/{f=1}' file

important text
important text
important text
important text
important text
important text
...
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • thank you for all the ideas. i have no error appearing. its "just" the output which is missing. – user3352472 Feb 26 '14 at 11:39
  • Run it with out any `> output` so you see what is going on. Try also `awk '{print $0} file` just to see if `file` is read, and you get any output. – Jotne Feb 26 '14 at 12:15
0

your code should work I have tested here

you can also use perl:

perl -lne 'print if(/start_here/.../end_here/)'

Tested here

Vijay
  • 65,327
  • 90
  • 227
  • 319
0

Your script is almost a working sed script:

sed -n '/start_here/,/end_here/p' inputfile.txt > outputfile.txt
aragaer
  • 17,238
  • 6
  • 47
  • 49