tags:

views:

14

answers:

2

hi,

i have one file. it contains fields like this

i/p files :

a.txt
9842,5003,a,a,100
9942,5003,a,a,100
9852,5003,a,a,100
98456,5003,a,a,100
9742,5003,a,a,100

b.txt
7842,5003,a,a,100
7942,5003,a,a,100
7852,5003,a,a,100
98456,5003,a,a,100
7742,5003,a,a,100

c.txt
8842,5003,a,a,100
9842,5003,a,a,100
8852,5003,a,a,100
88456,5003,a,a,100
8742,5003,a,a,100

Operation to be done: I need to grep particular lines in the above input file (a.txt,b.txt & c.txt) output lines should contain all fields and also the filname as last field

o/p format : say i grep lines starts with "9" means output will be needed in below format

9842,5003,a,a,100,a.txt
9942,5003,a,a,100,a.txt
9852,5003,a,a,100,a.txt
98456,5003,a,a,100,a.txt
9742,5003,a,a,100,a.txt
98456,5003,a,a,100,b.txt
9842,5003,a,a,100,c.txt
+1  A: 
grep -H '^9' a.txt b.txt c.txt | awk -F : '{print $2","$1;}'

Grep for lines starting with 9, using the -H flag to print filenames, then use awk to get it into the format you wanted. The -F flag for awk changes the field separator to be ':'

ngoozeff
A: 

you can just use one awk command, no need to grep. grep and awk are complements.

awk '/^9/{print $2","$1","FILENAME}' [abc].txt
ghostdog74