Help with shell script to extract certain information

Hi,
I have a file which I need to programmatically split into two files.

All the information in the file before pattern "STOP HERE" is to be stripped and output into one file while everything after "STOP HERE" is to be output into a separate file.

I would appreciate help on how to do this please.

thank you.

You can use sed for achieving this.
Let us know what you have tried, thanks :slight_smile:

Unfortunately I didn't even know where to start. I was reading up on egrep and I've been having a look at sed as you suggested. If you could throw some more light, I'd appreciate it.

thank you

Try

awk '/STOP HERE/ {FN="XX"; next} {print > FN}' FN="YY" file
# get stuff before string from file and redirect to file1
sed '/STOP HERE/,$d' file > file1
# get stuff after string from file and redirect to file2
sed -n '/STOP HERE/,$ {/STOP HERE/!p}' file > file2

For correct output, the source file should have only 1 STOP HERE string.

Btw. personally I prefer this (verbose :D) awk for the "before stuff", don't know how to invert it though :rolleyes::

awk '!/STOP HERE/ {print} /STOP HERE/ {exit}' file > file1

Thank you both, much appreciated.

awk '/STOP HERE/ {FN="XX"; next} {print > FN}' FN="YY" file

Please could you explain the actions? FN, I can't seem to find that as an awk variable. Please could you expantiate a bit.

awk '!/STOP HERE/ {print} /STOP HERE/ {exit}' file > file1

Everything that is not 'STOP HERE' in the input file print but exit at 'STOP HERE' and output to file1, is this what this is doing? I will try this and see how it goes.

thank you again.

Exactly.

  1. Read line by line from file
  2. String STOP HERE *not* present in the line? -> print line to file1 (actually print to terminal/stdout, but due to stream redirection (>file1) it's printed to file1.
  3. String STOP HERE present in the line? -> exit

FN is an user defined variable

Print any line which does not contain the regex

awk '!/STOP HERE/ {print}'

Same that

awk '!/STOP HERE/'

Now print each line that does not contain STOP HERE and exit reading the file
if STOP HERE is found

awk '!/STOP HERE/ {print} /STOP HERE/ {exit}' file

Same effect that the following:

awk '/STOP HERE/{exit} {print}' file
awk '/STOP HERE/{exit}1' file

Please use code tags as required by forum rules!

awk '/STOP HERE/ {FN="XX"; next} {print > FN}' FN="YY" file

That snippet prints to the file whose name in in FN, preset to "YY" by the first parameter. When "STOP HERE" is encountered, the file name is switched to "XX", and the line in NOT printed due to the "next" directive.