Compare two files and print the common fields and their corresponding line numbers in the 1st file

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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?
text-processing awk grep
add a comment |Â
up vote
2
down vote
favorite
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?
text-processing awk grep
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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?
text-processing awk grep
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?
text-processing awk grep
text-processing awk grep
edited Mar 13 '17 at 18:50
don_crissti
47.3k15125155
47.3k15125155
asked Mar 13 '17 at 18:16
Raj Kiran
134
134
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
awk alone can do this:
awk 'NR==FNRseen[$1]=seen[$1]","NR; next;
if ($1 in seen)printf("%s: %sn", $1, substr(seen[$1], 2))' file2 file1
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
awk alone can do this:
awk 'NR==FNRseen[$1]=seen[$1]","NR; next;
if ($1 in seen)printf("%s: %sn", $1, substr(seen[$1], 2))' file2 file1
add a comment |Â
up vote
1
down vote
accepted
awk alone can do this:
awk 'NR==FNRseen[$1]=seen[$1]","NR; next;
if ($1 in seen)printf("%s: %sn", $1, substr(seen[$1], 2))' file2 file1
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
awk alone can do this:
awk 'NR==FNRseen[$1]=seen[$1]","NR; next;
if ($1 in seen)printf("%s: %sn", $1, substr(seen[$1], 2))' file2 file1
awk alone can do this:
awk 'NR==FNRseen[$1]=seen[$1]","NR; next;
if ($1 in seen)printf("%s: %sn", $1, substr(seen[$1], 2))' file2 file1
answered Mar 13 '17 at 18:47
community wiki
don_crissti
add a comment |Â
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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 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.
edited Aug 26 at 4:10
answered Mar 13 '17 at 18:19
Moshe Shitrit
12
12
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f351207%2fcompare-two-files-and-print-the-common-fields-and-their-corresponding-line-numbe%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password