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
Split a file with no pattern -- Split, Csplit, Awk madhunk UNIX for Dummies Questions & Answers 10 12-17-2007 12:57 PM
perl - how do i find out if a file doesn't contain a pattern? mjays Shell Programming and Scripting 4 09-19-2007 07:28 AM
multiple pattern split in perl umen Shell Programming and Scripting 3 08-01-2006 03:43 AM
perl pattern matching vs. grep junkmail426 Shell Programming and Scripting 0 09-28-2005 11:40 AM
awk script to split a file based on the condition superprogrammer Shell Programming and Scripting 12 06-14-2005 04:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-20-2008
kumarn kumarn is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 3
Split a file based on pattern in awk, grep, sed or perl

Hi All,

Can someone please help me write a script for the following requirement in awk, grep, sed or perl.

Code:
Buuuu xxx bbb
Kmmmm rrr ssss uuuu
Kwwww zzzz ccc
Roooowwww eeee
Bxxxx jjjj dddd
Kuuuu eeeee nnnn
Rpppp cccc vvvv cccc
Rhhhhhhyyyy tttt
Lhhhh rrrrrssssss
Bffff mmmm iiiii
Ktttt eeeeeee
Kyyyyy iiiii wwww
Rwwww rrrr sssss eeee
Rnnnnn xxxxxxccccc
I like to split the above file into 3 files like below,

file1:
Code:
Buuuu xxx bbb
Kmmmm rrr ssss uuuu
Kwwww zzzz ccc
Roooowwww eeee
file2:
Code:
Bxxxx jjjj dddd
Kuuuu eeeee nnnn
Rpppp cccc vvvv cccc
Rhhhhhhyyyy tttt
Lhhhh rrrrrssssss
file3:
Code:
Bffff mmmm iiiii
Ktttt eeeeeee
Kyyyyy iiiii wwww
Rwwww rrrr sssss eeee
Rnnnnn xxxxxxccccc
Basically the file need to be start with "B" record and start a new file when it come across another "B" record.

Appreciate you help.

Thanks

Kumar

Last edited by Yogesh Sawant; 06-20-2008 at 04:25 AM.. Reason: added code tags
  #2 (permalink)  
Old 06-20-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
With (g)awk

Code:
awk 'BEGIN{RS="\n?B"} (NR-1){print "B" $0 > ("output-file_" NR)}' input-file
  #3 (permalink)  
Old 06-20-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,312
Quote:
Originally Posted by ripat View Post
With (g)awk

Code:
awk 'BEGIN{RS="\n?B"} (NR-1){print "B" $0 > ("output-file_" NR)}' input-file
Ripat, your solution doesn't give the desired output...

Try this:

Code:
awk '/^B/{close("file"f);f++}{print $0 > "file"f}' file
Regards
  #4 (permalink)  
Old 06-20-2008
spirtle spirtle is offline
Registered User
  
 

Join Date: Jun 2008
Location: Scotland
Posts: 150
I guess it would something like this in Perl:
Code:
perl -n -e '/^B/ and open FH, ">output_".$n++; print FH;' file
  #5 (permalink)  
Old 06-20-2008
drl's Avatar
drl drl is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 712
Hi.

It is useful to know how to do custom splitting with awk, perl, etc., but one can often use utilities that are already present, such as csplit:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate context splitting, csplit.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) csplit
echo

FILE=${1-data1}

# Remove debris from previous run.

rm -f xx*

if uname -a | grep SunOS
then
  csplit -k $FILE '/^B/' '{99}'
else
  csplit -z $FILE '/^B/' '{*}'
fi

echo " Samples of output files:"
for file in xx*
do
  echo
  echo "-- $file --"
  head -2 $file
done

exit 0
Producing:
Code:
$ ./s1

(Versions displayed with local utility "version")
SunOS 5.10
GNU bash 3.00.16
csplit - no version provided for /usr/bin/csplit.

SunOS vm-solaris 5.10 Generic_120012-14 i86pc i386 i86pc
0
64
89
csplit: {99} - out of range
90
 Samples of output files:

-- xx00 --

-- xx01 --
Buuuu xxx bbb
Kmmmm rrr ssss uuuu

-- xx02 --
Bxxxx jjjj dddd
Kuuuu eeeee nnnn

-- xx03 --
Bffff mmmm iiiii
Ktttt eeeeeee
See man csplit for details (Solaris man page has some examples, unlike the man page in Linux) ... cheers, drl
  #6 (permalink)  
Old 06-20-2008
kumarn kumarn is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 3
thank you all

All solutions really worked great. Now I have a choice. Thanks. Appreciate your help.

awk '/^B/{close("file"f);f++}{print $0 > "file"f}' input.txt

perl -n -e '/^B/ and open FH, ">output_".$n++; print FH;' input.txt

csplit -k input.txt '/^B/' '{99}'
Closed Thread

Bookmarks

Tags
linux, solaris

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 11:37 AM.


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