Input file:
$ cat details.txt
May 27 17:01:07 IST 2009
A,5050,3434
B,42122,12121
C,26232,3424
May 27 18:01:47 IST 2009
A,1212,3434
B,12122,12121
C,23232,24324
May 27 19:01:27 IST 2009
A,8212,6434
B,2122,2121
C,3232,4324
Required output: Divide the above file into sub-files ( based on the codes i.e. A,B,C etc) and each sub-file should contain the information related to that code (Basically the lines which starts with this code) and the sub-file should look like:
$ cat A.out
May 27 17:01:07 IST 2009,5050,3434
May 27 18:01:47 IST 2009,1212,3434
May 27 19:01:27 IST 2009,8212,6434
$ cat B.out
May 27 17:01:07 IST 2009,42122,12121
May 27 18:01:47 IST 2009,12122,12121
May 27 19:01:27 IST 2009,2122,2121
$ cat C.out
May 27 17:01:07 IST 2009,26232,3424
May 27 18:01:47 IST 2009,23232,24324
May 27 19:01:27 IST 2009,3232,4324
Awk script for the same:
$ awk '
BEGIN {FS=OFS=","}
$1 ~ /IST/ {dt=$0; next}
{print dt,$2,$3 > $1".out"}
' details.txt
Related posts:
- Divide a file using awk
- Split file into subfiles based on pattern using awk
- Append line identifier to each line using awk
1 comment:
I just started learning about awk and I find it extremely useful.
thanks!
Post a Comment