# split files by specifying a string (bash shell)

**URL:** <https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148>\
**Category:** Shell Programming and Scripting\
**Created:** [October 29, 2007, 12:34pm UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148 "2007-10-29T12:34:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikas027](https://community.unix.com/letter_avatar/vikas027/32/6_22734026db12803ebb3e059622ac3534.png) [@vikas027](https://community.unix.com/u/vikas027)\
**Post date:** [October 29, 2007, 12:34pm UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/1 "2007-10-29T12:34:50Z")

</div>

Hi all,

I have a file of around 300 lines in which string "SERVER" occurs around 32 times.

for eg.

> [@](#):
>
> SERVER  
> .....  
> .....  
> ....
> 
> SERVER
> 
> .....  
> .....  
> ....
> 
> SERVER  
> .....  
> .....  
> ....

I need to split files like, for eg

> [@](#):
>
> file1  
> SERVER  
> ....  
> ....  
> ....
> 
> file2  
> SERVER  
> .....  
> ...  
> ....
> 
> file3  
> SERVER  
> .....  
> ....  
> ....

I am using this code  
awk '/SERVER/{n++}{print \> f n}' f=/vikas/list /vikas/final

But the problem is that it makes maximum of 10 files, but I need more than 30.  
I have tried using nawk, but didnt worked.  
I am using bash scripting on Sun OS.

Any other way of splitting this data ???

Pls help !!!

Thanks in adv.  
Regards,  
Vikas

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [October 29, 2007, 12:44pm UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/2 "2007-10-29T12:44:23Z")

</div>

```nohighlight
awk '/SERVER/{n++}{output = f n; print > output; close(output) }' f=/vikas/list /vikas/final

```

---

<div class="post-metadata">

**Author:** ![vikas027](https://community.unix.com/letter_avatar/vikas027/32/6_22734026db12803ebb3e059622ac3534.png) [@vikas027](https://community.unix.com/u/vikas027)\
**Post date:** [October 29, 2007, 12:53pm UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/3 "2007-10-29T12:53:19Z")

</div>

> [@vgersh99](#):
>
> ```plaintext
> awk '/SERVER/{n++}{output = f n; print > output; close(output) }' f=/vikas/list /vikas/final
> 
> ```

Thanks vgersh99,  
will get back to you after trying this command.

---

<div class="post-metadata">

**Author:** ![vikas027](https://community.unix.com/letter_avatar/vikas027/32/6_22734026db12803ebb3e059622ac3534.png) [@vikas027](https://community.unix.com/u/vikas027)\
**Post date:** [October 29, 2007, 2:16pm UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/4 "2007-10-29T14:16:52Z")

</div>

> [@vgersh99](#):
>
> ```plaintext
> awk '/SERVER/{n++}{output = f n; print > output; close(output) }' f=/vikas/list /vikas/final
> 
> ```

hi,  
this cmd is making as many blank files as the string SERVER is there in the final file. ☹

pls help

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [October 29, 2007, 2:28pm UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/5 "2007-10-29T14:28:53Z")

</div>

```nohighlight
awk '/SERVER/{if (n) close(output); output= f ++n} n {print >> output }' f=/vikas/list /vikas/final

```

---

<div class="post-metadata">

**Author:** ![vikas027](https://community.unix.com/letter_avatar/vikas027/32/6_22734026db12803ebb3e059622ac3534.png) [@vikas027](https://community.unix.com/u/vikas027)\
**Post date:** [October 29, 2007, 2:45pm UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/6 "2007-10-29T14:45:18Z")

</div>

> [@vgersh99](#):
>
> ```plaintext
> awk '/SERVER/{if (n) close(output); output= f ++n} n {print >> output }' f=/vikas/list /vikas/final
> 
> ```

hey,  
This didnt made any file, not even a blank one.  
Thanks.

---

<div class="post-metadata">

**Author:** ![vikas027](https://community.unix.com/letter_avatar/vikas027/32/6_22734026db12803ebb3e059622ac3534.png) [@vikas027](https://community.unix.com/u/vikas027)\
**Post date:** [October 29, 2007, 2:50pm UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/7 "2007-10-29T14:50:49Z")

</div>

> [@vgersh99](#):
>
> ```plaintext
> awk '/SERVER/{if (n) close(output); output= f n++} n {print >> output }' f=/vikas/list /vikas/final
> 
> ```

hey, this command worked.  
Thanks a ton friend. Its running on my Linux machine smoothly.  
Now I have to check it on Solaris machine, will do that tomorrow.  
Thanks again.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [October 29, 2007, 2:55pm UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/8 "2007-10-29T14:55:35Z")

</div>

> [@vikas027](#):
>
> hey,  
> This didnt made any file, not even a blank one.  
> Thanks.

Sorry 'bout that - some awk's increment pre-increment ops don't seem to work the way they supposed to.

```plaintext
awk '/SERVER/{if (n++) close(output); output= f n} n {print >> output }' f=/vikas/list /vikas/final

```

On Solaris - use 'nawk' or '/usr/xpg4/bin/awk'

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [October 29, 2007, 4:52pm UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/9 "2007-10-29T16:52:46Z")

</div>

```nohighlight
nawk '/^SERVER/ { 
	close(f); f = "/vikas/list" ++c 
	} 
{ 
	print > f 
}' /vikas/final

```

---

<div class="post-metadata">

**Author:** ![vikas027](https://community.unix.com/letter_avatar/vikas027/32/6_22734026db12803ebb3e059622ac3534.png) [@vikas027](https://community.unix.com/u/vikas027)\
**Post date:** [October 31, 2007, 2:46am UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/10 "2007-10-31T02:46:44Z")

</div>

> [@vgersh99](#):
>
> Sorry 'bout that - some awk's increment pre-increment ops don't seem to work the way they supposed to.
> 
> ```plaintext
> awk '/SERVER/{if (n++) close(output); output= f n} n {print >> output }' f=/vikas/list /vikas/final
> 
> ```
> 
> On Solaris - use 'nawk' or '/usr/xpg4/bin/awk'

hello friend,

this command is only making two files list1 & list2.

Thanks.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [October 31, 2007, 8:06am UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/11 "2007-10-31T08:06:43Z")

</div>

works fine with 3 (and more) 'SERVER' lines...

---

<div class="post-metadata">

**Author:** ![vikas027](https://community.unix.com/letter_avatar/vikas027/32/6_22734026db12803ebb3e059622ac3534.png) [@vikas027](https://community.unix.com/u/vikas027)\
**Post date:** [November 1, 2007, 6:52am UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/12 "2007-11-01T06:52:30Z")

</div>

Hi,

I tried lots of commands including the above ones. They all run fine on Linux machines  
BUT not on the solaris machines, dont know the reason behind it.

Anyways, MANY MANY THANKS to all for your time and help. I found this command to work perfectly.

```nohighlight
/usr/xpg4/bin/awk '/SERVER/{n++}{print > f n}' f=/vikas/list /vikas/final

```

Thanks again.

---

<div class="post-metadata">

**Author:** ![dummy\_needhelp](https://community.unix.com/letter_avatar/dummy_needhelp/32/6_22734026db12803ebb3e059622ac3534.png) [@dummy\_needhelp](https://community.unix.com/u/dummy_needhelp)\
**Post date:** [November 1, 2007, 1:57pm UTC](https://community.unix.com/t/split-files-by-specifying-a-string-bash-shell/177148/13 "2007-11-01T13:57:56Z")

</div>

Can anyone pleasee explain me how the command is interpreted. Thanks alot in advance
