nixCraft Linux Forum

nixCraft

Linux / UNIX 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. for eg. ...

Register free or login to your existing account and remove all advertisements.


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

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-29-2007, 09:49 PM
Member
User
 
Join Date: Oct 2007
OS: Linux RHEL 5
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
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 (Solaris).
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
OS: RHEL
Scripting language: Bash and Python
Posts: 2,693
Thanks: 11
Thanked 242 Times in 182 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

I'm not sure if I understood your problem, can you explain it little bit..
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 10-29-2007, 11:55 PM
Member
User
 
Join Date: Oct 2007
OS: Linux RHEL 5
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
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
OS: RHEL
Scripting language: Bash and Python
Posts: 2,693
Thanks: 11
Thanked 242 Times in 182 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
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 Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #5 (permalink)  
Old 11-01-2007, 04:22 PM
Member
User
 
Join Date: Oct 2007
OS: Linux RHEL 5
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
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


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
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 Off


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 05:32 PM.


Powered by vBulletin® Version 3.8.4 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2009 nixCraft. All rights reserved

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 31 32 33 34 35 36 37 38