The UNIX and Linux Forums  

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 10: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 04:34 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-29-2007
Registered User
 

Join Date: Oct 2007
Posts: 66
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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-29-2007
vgersh99's Avatar
Moderator
 

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

Join Date: Oct 2007
Posts: 66
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.
Reply With Quote
  #4 (permalink)  
Old 10-29-2007
Registered User
 

Join Date: Oct 2007
Posts: 66
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
Reply With Quote
  #5 (permalink)  
Old 10-29-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 4,537
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 01:36 PM..
Reply With Quote
  #6 (permalink)  
Old 10-29-2007
Registered User
 

Join Date: Oct 2007
Posts: 66
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.
Reply With Quote
  #7 (permalink)  
Old 10-29-2007
Registered User
 

Join Date: Oct 2007
Posts: 66
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
linux, linux commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 08:06 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
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

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66