Thursday, April 2, 2009

Split a file based on start pattern - awk


In one of my earlier post , I already discussed how we can divide a file like address.txt (below) into sub files, such that each of one contains only one address (in that example one address was being considered till the PIN number pattern)

$ cat address.txt
Mr X
Koramangala Post
3rd Cross, 17th Main
PIN: 12345
Mr Y
NGV
PIN: 45678
Mr Z
5th Ave, #23
NHM Post
LKV
PIN: 32456

Lets see how we can divide a similar file like address.txt into subfiles, each containing one address, but this time we will consider a address being starts from the pattern "Mr" or "Mrs" (i.e. All lines from "Mr" or "Mrs" till next "Mr" or "Mrs" should go to one file and so on)

I have just modified the file as

$ cat record.txt
Mrs X
Koramangala Post
3rd Cross, 17th Main
PIN: 12345
status=nill
Mr Y
NGV
PIN: 45678
Mrs Z
5th Ave, #23
NHM Post
LKV
PIN: 32456
spl case=yes

So we are expecting 3 subfiles like this:

$ cat add_1
Mrs X
Koramangala Post
3rd Cross, 17th Main
PIN: 12345
status=nill

$ cat add_2
Mr Y
NGV
PIN: 45678

$ cat add_3
Mrs Z
5th Ave, #23
NHM Post
LKV
PIN: 32456
spl case=yes

The solution:

$ awk '/^Mr|^Mrs/{close("add_"f);f++}{print $0 > "add_"f}' record.txt

Related post:
- Split file based on pattern using awk
- Sub dividing file using awk

No comments:

© Jadu Saikia www.UNIXCL.com