The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script to find and replace string in multiple files pharos467 Shell Programming and Scripting 11 01-15-2009 11:40 AM
perl-like split function for bash? eur0dad Shell Programming and Scripting 5 09-05-2008 06:23 PM
[KSH] Split string into array piooooter Shell Programming and Scripting 3 09-01-2007 12:22 PM
split string help senthilk615 Shell Programming and Scripting 4 03-27-2006 06:43 PM
split a string gazingdown Shell Programming and Scripting 3 02-09-2006 05:34 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-29-2007
vikas027 vikas027 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 77
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.
I am using bash scripting on Sun OS.


Any other way of splitting this data ???

Pls help !!!

Thanks in adv.
Regards,
Vikas
  #2 (permalink)  
Old 10-29-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
Code:
awk '/SERVER/{n++}{output = f n; print > output; close(output) }' f=/vikas/list /vikas/final
  #3 (permalink)  
Old 10-29-2007
vikas027 vikas027 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 77
Quote:
Originally Posted by vgersh99 View Post
Code:
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.
  #4 (permalink)  
Old 10-29-2007
vikas027 vikas027 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 77
Quote:
Originally Posted by vgersh99 View Post
Code:
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
  #5 (permalink)  
Old 10-29-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
Code:
awk '/SERVER/{if (n) close(output); output= f ++n} n {print >> output }' f=/vikas/list /vikas/final

Last edited by vgersh99; 10-29-2007 at 02:36 PM..
  #6 (permalink)  
Old 10-29-2007
vikas027 vikas027 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 77
Quote:
Originally Posted by vgersh99 View Post
Code:
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.
  #7 (permalink)  
Old 10-29-2007
vikas027 vikas027 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 77
Quote:
Originally Posted by vgersh99 View Post
Code:
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.
Sponsored Links
Closed Thread

Bookmarks

Tags
linux, linux commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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



All times are GMT -4. The time now is 05:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0