# Help with shell script to extract certain information

**URL:** <https://community.unix.com/t/help-with-shell-script-to-extract-certain-information/348073>\
**Category:** Shell Programming and Scripting\
**Created:** [November 13, 2014, 9:41am UTC](https://community.unix.com/t/help-with-shell-script-to-extract-certain-information/348073 "2014-11-13T09:41:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![PTL](https://community.unix.com/letter_avatar/ptl/32/5_5575768a8748004e209b776fc1b2916d.png) [@PTL](https://community.unix.com/u/PTL)\
**Post date:** [November 13, 2014, 9:41am UTC](https://community.unix.com/t/help-with-shell-script-to-extract-certain-information/348073/1 "2014-11-13T09:41:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![balajesuri](https://community.unix.com/user_avatar/community.unix.com/balajesuri/32/1423_2.png) [@balajesuri](https://community.unix.com/u/balajesuri)\
**Post date:** [November 13, 2014, 10:23am UTC](https://community.unix.com/t/help-with-shell-script-to-extract-certain-information/348073/2 "2014-11-13T10:23:03Z")

</div>

You can use [sed](https://www.gnu.org/software/sed/manual/sed.html)for achieving this.  
Let us know what you have tried, thanks 🙂

---

<div class="post-metadata">

**Author:** ![PTL](https://community.unix.com/letter_avatar/ptl/32/5_5575768a8748004e209b776fc1b2916d.png) [@PTL](https://community.unix.com/u/PTL)\
**Post date:** [November 13, 2014, 11:19am UTC](https://community.unix.com/t/help-with-shell-script-to-extract-certain-information/348073/3 "2014-11-13T11:19:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 13, 2014, 12:20pm UTC](https://community.unix.com/t/help-with-shell-script-to-extract-certain-information/348073/4 "2014-11-13T12:20:12Z")

</div>

Try

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

```

---

<div class="post-metadata">

**Author:** ![junior-helper](https://community.unix.com/letter_avatar/junior-helper/32/5_5575768a8748004e209b776fc1b2916d.png) [@junior-helper](https://community.unix.com/u/junior-helper)\
**Post date:** [November 13, 2014, 12:28pm UTC](https://community.unix.com/t/help-with-shell-script-to-extract-certain-information/348073/5 "2014-11-13T12:28:10Z")

</div>

```nohighlight
# get stuff before string from file and redirect to file1
sed '/STOP HERE/,$d' file > file1

```

```nohighlight
# 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::

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

```

---

<div class="post-metadata">

**Author:** ![PTL](https://community.unix.com/letter_avatar/ptl/32/5_5575768a8748004e209b776fc1b2916d.png) [@PTL](https://community.unix.com/u/PTL)\
**Post date:** [November 15, 2014, 1:02pm UTC](https://community.unix.com/t/help-with-shell-script-to-extract-certain-information/348073/6 "2014-11-15T13:02:45Z")

</div>

Thank you both, much appreciated.

```nohighlight
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.

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![junior-helper](https://community.unix.com/letter_avatar/junior-helper/32/5_5575768a8748004e209b776fc1b2916d.png) [@junior-helper](https://community.unix.com/u/junior-helper)\
**Post date:** [November 15, 2014, 1:49pm UTC](https://community.unix.com/t/help-with-shell-script-to-extract-certain-information/348073/7 "2014-11-15T13:49:00Z")

</div>

> [@ptl](#):
>
> is this what this is doing?

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

---

<div class="post-metadata">

**Author:** ![Aia](https://community.unix.com/user_avatar/community.unix.com/aia/32/917_2.png) [@Aia](https://community.unix.com/u/Aia)\
**Post date:** [November 15, 2014, 2:02pm UTC](https://community.unix.com/t/help-with-shell-script-to-extract-certain-information/348073/8 "2014-11-15T14:02:50Z")

</div>

> [@ptl](#):
>
> 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.

FN is an user defined variable

Print any line which does not contain the regex

```plaintext
awk '!/STOP HERE/ {print}'

```

Same that

```plaintext
awk '!/STOP HERE/'

```

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

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

```

Same effect that the following:

```plaintext
awk '/STOP HERE/{exit} {print}' file

```

```plaintext
awk '/STOP HERE/{exit}1' file

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 16, 2014, 6:54am UTC](https://community.unix.com/t/help-with-shell-script-to-extract-certain-information/348073/9 "2014-11-16T06:54:26Z")

</div>

Please use code tags as required by forum rules!

```nohighlight
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.
