Monday, January 7, 2008
Subdividing a file - BASH Newbie
Input file is a list of std codes of some cities in India,
$ cat citycodes.txt
Ajodya 03214
Amdanga 03216
Amlagona 03227
Amta 03214
Arambag 03211
Arbelia 03217
Baduria 03217
Bagmundi 03252
...
...
1) I want to subdivide this file into subfiles with 3 entries each.
$ awk '{print >("std_" int((NR+2)/3))}' citycodes.txt
It will create files
std_1,std_2,std_3...
with 3 entries each.
2)
Now if we want the entries to be in files in a fashion like:
"Amdanga","Amlagona","Amta" should come in the file "std_Am" (std_$FIRST_2_LETTERS) etc.
$ cat citycodes.txt | while read line
> do
> echo "$line" >> std_`echo $line | cut -c1-2`
> done
3) Based on the first field
$ cat cus.out
100 ABC A123
100 BVC D234
100 BNC N324
200 CBC A122
200 AVC D294
200 HNC N324
200 HNC N324
300 GBC A173
300 FVC D234
300 DNC N344
400 DNC N344
$ awk '{print > $1".txt"}' cus.out
$ ls *.txt
100.txt 200.txt 300.txt 400.txt
$ cat 300.txt
300 GBC A173
300 FVC D234
300 DNC N344
$ cat 200.txt
200 CBC A122
200 AVC D294
200 HNC N324
200 HNC N324
Subscribe to:
Post Comments (Atom)
© Jadu Saikia www.UNIXCL.com
1 comment:
Can you please tell me how to split the file based on the nth occurance or pattern(my pattern is same all over the file-so I would like to split file at 30th occurrence of it)
Post a Comment