nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

split files by specifying a string (bash shell)

This is a discussion on split files by specifying a string (bash shell) within the Shell scripting forums, part of the Development/Scripting category; Hi all, I have a file of around 300 lines in which string "SERVER" occurs around 32 times. ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-29-2007, 09:49 PM
Member
User
 
Join Date: Oct 2007
My distro: Linux RHEL 5
Posts: 32
vikas027 is on a distinguished road
Unhappy split files by specifying a string (bash shell)

Hi all,

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

for eg.
Quote:
SERVER
.....
.....
....

SERVER

.....
.....
....

SERVER.....
.....
....
I need to split files like, for eg

Quote:
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. This cmd is running fine in Linux, But not in Sun OS (Solari.
I am using bash scripting on Sun OS.


Any other way of splitting this data ???

Pls help !!!

Thanks in adv.
Regards,
Vikas

Last edited by vikas027; 10-29-2007 at 11:58 PM.
Reply With Quote
  #2 (permalink)  
Old 10-29-2007, 11:15 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Any distro with shell
Posts: 840
nixcraft is an unknown quantity at this point
Default

I'm not sure if I understood your problem, can you explain it little bit..
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 10-29-2007, 11:55 PM
Member
User
 
Join Date: Oct 2007
My distro: Linux RHEL 5
Posts: 32
vikas027 is on a distinguished road
Default

Quote:
Originally Posted by nixcraft View Post
I'm not sure if I understood your problem, can you explain it little bit..

Hi,

Well I am explaining it again.
I have a file say final.txt, which has some data in it.

For e.g.
Quote:
final.txt

SERVER
w
easf
fwsfw
fsdf
23

SERVER
wefwrg
234gf
dfgh32
fdfg3r
gdfg34
grer3467
322rfgt45

SERVER
3211
33
453de
43ffg
Now, I need to split this file, by matching a string SERVER such than I have three files list1, list2, list3.

As in this case, it may be:

Quote:
File list1
SERVER
w
easf
fwsfw
fsdf
23

File list2
SERVER
wefwrg
234gf
dfgh32
fdfg3r
gdfg34
grer3467
322rfgt45

File list3
SERVER
3211
33
453de
43ffg
Now, I know the command to split this file final.txt i.e.
awk '/SERVER/{n++}{print > f n}' f=/vikas/list /vikas/final

But the problem I am facing is that my actual file final.txt is pretty big, which needs to splitted into around 30 files, which is however not being possible by this command, as it can only split upto 10 files at max only.

Hope I have made my point clear.

Thanks.
Reply With Quote
  #4 (permalink)  
Old 10-30-2007, 03:48 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Any distro with shell
Posts: 840
nixcraft is an unknown quantity at this point
Default

Try

Code:
awk '/SERVER/{n++}{print >"out" n ".txt" }' final.txt
It will read input from final.txt and produced out1.txt, out2.txt... out30.txt
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #5 (permalink)  
Old 11-01-2007, 04:22 PM
Member
User
 
Join Date: Oct 2007
My distro: Linux RHEL 5
Posts: 32
vikas027 is on a distinguished road
Smile Hi All

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.

Code:
/usr/xpg4/bin/awk '/SERVER/{n++}{print > f n}' f=/vikas/list /vikas/final
Thanks again.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
How can I set Path in bash shell chanderbio Shell scripting 2 06-13-2008 10:38 AM
SCP with a bash shell password linux and unix servers jerry Networking, Firewalls and Security 1 06-09-2007 05:53 PM
Shell script for automatic conversion of files in tar files kasimani Shell scripting 2 02-08-2007 03:45 PM
shell script to open log files and check for faults trueman82 Shell scripting 1 11-23-2006 02:35 AM
example for string connect to a command. ryan Shell scripting 2 02-22-2005 01:05 PM


All times are GMT +5.5. The time now is 09:16 PM.


Powered by vBulletin® Version 3.7.2 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30