# Help needed - shell scripting

**URL:** <https://community.unix.com/t/help-needed-shell-scripting/161851>\
**Category:** Shell Programming and Scripting\
**Created:** [May 22, 2006, 7:14am UTC](https://community.unix.com/t/help-needed-shell-scripting/161851 "2006-05-22T07:14:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![garric](https://community.unix.com/letter_avatar/garric/32/6_449c533f586939b6e5f246632a46421f.png) [@garric](https://community.unix.com/u/garric)\
**Post date:** [May 22, 2006, 7:14am UTC](https://community.unix.com/t/help-needed-shell-scripting/161851/1 "2006-05-22T07:14:57Z")

</div>

I have a file that has text in the format below

ABC  
/ Some text /  
ABC  
/ Some text /  
ABC  
/ Some text /  
ABC  
/ Some text /  
ABC  
/ Some text /

How can I seperate the text between a pair of 'ABC' into seperate files ???

any information would be of great help. Thanks

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [May 22, 2006, 7:20am UTC](https://community.unix.com/t/help-needed-shell-scripting/161851/2 "2006-05-22T07:20:26Z")

</div>

Do you mean you want to create a file that is minus all the ABC's?

```nohighlight
grep -v 'ABC' oldfile > newfile

```

---

<div class="post-metadata">

**Author:** ![garric](https://community.unix.com/letter_avatar/garric/32/6_449c533f586939b6e5f246632a46421f.png) [@garric](https://community.unix.com/u/garric)\
**Post date:** [May 22, 2006, 7:28am UTC](https://community.unix.com/t/help-needed-shell-scripting/161851/3 "2006-05-22T07:28:29Z")

</div>

no, if we consider the example ... i want to create five different files with the content of each coming from the text betweeen two consecutive 'ABC's

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [May 22, 2006, 10:00am UTC](https://community.unix.com/t/help-needed-shell-scripting/161851/4 "2006-05-22T10:00:41Z")

</div>

```nohighlight
#/bin/ksh
let filecnt=0
while read record
do
     if [`echo $record | grep -q '^ABC'` -ne 0] ; then
           echo "$record" > newfile"$filecnt"
           let filecnt=$filecnt + 1
     fi
done < oldfile 

```

---

<div class="post-metadata">

**Author:** ![tayyabq8](https://community.unix.com/user_avatar/community.unix.com/tayyabq8/32/483_2.png) [@tayyabq8](https://community.unix.com/u/tayyabq8)\
**Post date:** [May 22, 2006, 10:54am UTC](https://community.unix.com/t/help-needed-shell-scripting/161851/5 "2006-05-22T10:54:58Z")

</div>

Hi,

Out of interest, is possible to redirect the same output with sed to textfile.$count? using branching (b), hold space or whatsoever is available in sed to output different sections from one file to more than files?

Regards,  
Tayyab

---

<div class="post-metadata">

**Author:** ![garric](https://community.unix.com/letter_avatar/garric/32/6_449c533f586939b6e5f246632a46421f.png) [@garric](https://community.unix.com/u/garric)\
**Post date:** [May 22, 2006, 5:38pm UTC](https://community.unix.com/t/help-needed-shell-scripting/161851/6 "2006-05-22T17:38:25Z")

</div>

Thsi is gave an error, something regarding ' unary operators for -ne'

---

<div class="post-metadata">

**Author:** ![Ygor](https://community.unix.com/user_avatar/community.unix.com/ygor/32/104_2.png) [@Ygor](https://community.unix.com/u/Ygor)\
**Post date:** [May 22, 2006, 10:28pm UTC](https://community.unix.com/t/help-needed-shell-scripting/161851/7 "2006-05-22T22:28:09Z")

</div>

Perhaps use awk...

```nohighlight
$ < infile awk '/ABC/{c++; next} {print > "outfile." c}'

$ head outfile.*
==> outfile.1 <==
/ Some text /

==> outfile.2 <==
/ Some text /

==> outfile.3 <==
/ Some text /

==> outfile.4 <==
/ Some text /

==> outfile.5 <==
/ Some text /

```

---

<div class="post-metadata">

**Author:** ![garric](https://community.unix.com/letter_avatar/garric/32/6_449c533f586939b6e5f246632a46421f.png) [@garric](https://community.unix.com/u/garric)\
**Post date:** [May 23, 2006, 3:14am UTC](https://community.unix.com/t/help-needed-shell-scripting/161851/8 "2006-05-23T03:14:04Z")

</div>

Thnks a lot, works gr8. But if i had something in the format

ABC  
/unecessary text/  
/needed text/  
ABC  
/unecessary text/  
/needed text/  
ABC  
/unecessary text/  
/needed text/

How can I seperate it into files where the 'unecessary text' is removed from each file. so i should have  
--------File 1--------  
ABC  
/needed text/

--------File 2--------  
ABC  
/needed text/

--------File 3--------  
ABC  
/needed text/

Thanks.

---

<div class="post-metadata">

**Author:** ![Ygor](https://community.unix.com/user_avatar/community.unix.com/ygor/32/104_2.png) [@Ygor](https://community.unix.com/u/Ygor)\
**Post date:** [May 23, 2006, 6:08am UTC](https://community.unix.com/t/help-needed-shell-scripting/161851/9 "2006-05-23T06:08:26Z")

</div>

See [the awk manual](http://www.cs.uu.nl/docs/vakken/st/nawk/nawk_toc.html)
