2

I have some raw data in one file say File1.txt

Colt McCoy QB CLE  135 222 1576    6   9   60.8%   74.51  
Josh Freeman QB TB 291 474 3451    25  6   61.4%   95.9  
Matt Cassel QB KC  262 450 3116    27  7   58.2%   93.0  
Michael Vick QB PHI    233 372 3018    21  6   62.6%   100.2  
Matt Schaub QB HOU 365 574 4370    24  12  63.6%   92.0  

File 2.txt has a list of names, one name per line

Josh  
Matt  

I want to get the output as Name and line number. For the above example it would be:

Josh: 2  
Matt: 3,5  

I have a command which gives me the list of names and the line numbers but I want to print it in the desired format above. My current command is

awk '{print $1, NR}' file1.txt | grep -f file2.txt

It shows

Josh 2
Matt 3
Matt 5

What can I do to make it print like the desired format?

2 Answers 2

1

awk alone can do this:

awk 'NR==FNR{seen[$1]=seen[$1]","NR; next};
{if ($1 in seen){printf("%s: %s\n", $1, substr(seen[$1], 2))}}' file2 file1
0

You can use diff -y file1 file2.

It will give you a side-by-side output from both files. It also has other options to parse your output.. Review the man page for more options.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .