Input file:
$ 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
Required: Divide the above file in sub files each containing one address (One address being from the name of the person to the PIN number)
$ awk '
BEGIN{ fn = "add1.txt"; n = 1}
{
print > fn
if (substr($0,1,3) == "PIN") {
close (fn)
n++
fn = "add" n ".txt"
}
}
' address.txt
So output sub files files:
$ cat add1.txt
Mr X
Koramangala Post
3rd Cross, 17th Main
PIN: 12345
$ cat add2.txt
Mr Y
NGV
PIN: 45678
$ cat add3.txt
Mr Z
5th Ave, #23
NHM Post
LKV
PIN: 32456
Similar posts:
- Subdividing large file into multiple files using awk
- Send alternate lines to separate files using awk
- Break lines into multiple lines using sed and awk
- Split file and add header to each file using awk
Dedicated to all BASH newbies and Linux one liner lovers. Useful AWK,SED,BASH one liners.
Thursday, December 11, 2008
Split file based on pattern - awk
Subscribe to:
Post Comments (Atom)
© Jadu Saikia http://unstableme.blogspot.com

0 comments:
Post a Comment