Split a file based on pattern in awk, grep, sed or perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split a file based on pattern in awk, grep, sed or perl
# 1  
Old 06-20-2008
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  
Old 06-20-2008
With (g)awk

Code:
awk 'BEGIN{RS="\n?B"} (NR-1){print "B" $0 > ("output-file_" NR)}' input-file

# 3  
Old 06-20-2008
Quote:
Originally Posted by ripat
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  
Old 06-20-2008
I guess it would something like this in Perl:
Code:
perl -n -e '/^B/ and open FH, ">output_".$n++; print FH;' file

# 5  
Old 06-20-2008
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  
Old 06-20-2008
thank you all

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

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

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

Code:
csplit -k input.txt '/^B/' '{99}'


Last edited by rbatte1; 05-02-2017 at 05:56 AM.. Reason: Added CODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Split one file to many based on pattern

Hello All, I have records in a file in a pattern A,B,B,B,B,K,A,B,B,K Is there any command or simple logic I can pull out records into multiple files based on A record? I want output as File1: A,B,B,B,B,K File2: A,B,B,K (9 Replies)
Discussion started by: deal1dealer
9 Replies

2. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

3. Shell Programming and Scripting

How to split a file based on pattern line number?

Hi i have requirement like below M <form_name> sdasadasdMklkM D ...... D ..... M form_name> sdasadasdMklkM D ...... D ..... D ...... D ..... M form_name> sdasadasdMklkM D ...... M form_name> sdasadasdMklkM i want split file based on line number by finding... (10 Replies)
Discussion started by: bhaskar v
10 Replies

4. Shell Programming and Scripting

Split the file based on pattern

Hi , I have huge files around 400 mb, which has clob data and have diffeent scenarios: I am trying to pass scenario number as parameter and and get required modified file based on the scenario number and criteria. Scenario 1: file name : scenario_1.txt ... (2 Replies)
Discussion started by: sol_nov
2 Replies

5. Shell Programming and Scripting

Split a file based on pattern and size

Hello, I have a large file (2GB) that I would like to split based on pattern and size. I've used the following command to split the file (token is "HELLO") awk '/HELLO/{i++}{print > "file"i}' input.txt and the output is similar to the following (i included filesize in KB): 10 ... (2 Replies)
Discussion started by: jl487
2 Replies

6. Shell Programming and Scripting

how to get data from hex file using SED or AWK based on pattern sign

I have a binary (hex) file I need to parse to get some data which are encoded this way: .* b4 . . . 01 12 .* af .* 83 L1 x1 x2 xL 84 L2 y1 y2 yL By another words there is a stream of hexadecimal bytes (in my example separated by space for better readability). I need to get value stored in... (3 Replies)
Discussion started by: sameucho
3 Replies

7. Shell Programming and Scripting

Help to search multiple pattern in file with grep/sed/awk

Hello All, I have a file which is having below type of data, Jul 19 2011 | 123456 Jul 19 2011 | 123456 Jul 20 2011 | 123456 Jul 20 2011 | 123456 Here I wanted to grep for date pattern as below, so that it should only grep "Jul 20" OR "Jul ... (9 Replies)
Discussion started by: gr8_usk
9 Replies

8. Shell Programming and Scripting

Split a file based on a pattern

Dear all, I have a large file which is composed of 8000 frames, what i would like to do is split the file into 8000 single files names file.pdb.1, file.pdb.2 etc etc each frame in the large file is seperated by a "ENDMDL" flag so my thinking is to use this flag a a point to split the files... (4 Replies)
Discussion started by: Mish_99
4 Replies

9. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question