awk: print duplicates from two files
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
File1:
23455|abc|xyz
23455|abc|xsd
34433|wer|sad
45655|fdf|fcd
File2:
v343v|23455
z565z|23455
c9898|34433
b2323|45655
Output should be:
23455|abc|xyz|v343v
23455|abc|xyz|z565z
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
I am using the below command :
awk -F'|' 'NR==FNR a[$1]=$1" $2 > 0 " $1' file1 file2 > result.txt
But its only showing the result :
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
Note: Both the files have data in random order
linux awk
add a comment |Â
up vote
0
down vote
favorite
File1:
23455|abc|xyz
23455|abc|xsd
34433|wer|sad
45655|fdf|fcd
File2:
v343v|23455
z565z|23455
c9898|34433
b2323|45655
Output should be:
23455|abc|xyz|v343v
23455|abc|xyz|z565z
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
I am using the below command :
awk -F'|' 'NR==FNR a[$1]=$1" $2 > 0 " $1' file1 file2 > result.txt
But its only showing the result :
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
Note: Both the files have data in random order
linux awk
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
File1:
23455|abc|xyz
23455|abc|xsd
34433|wer|sad
45655|fdf|fcd
File2:
v343v|23455
z565z|23455
c9898|34433
b2323|45655
Output should be:
23455|abc|xyz|v343v
23455|abc|xyz|z565z
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
I am using the below command :
awk -F'|' 'NR==FNR a[$1]=$1" $2 > 0 " $1' file1 file2 > result.txt
But its only showing the result :
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
Note: Both the files have data in random order
linux awk
File1:
23455|abc|xyz
23455|abc|xsd
34433|wer|sad
45655|fdf|fcd
File2:
v343v|23455
z565z|23455
c9898|34433
b2323|45655
Output should be:
23455|abc|xyz|v343v
23455|abc|xyz|z565z
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
I am using the below command :
awk -F'|' 'NR==FNR a[$1]=$1" $2 > 0 " $1' file1 file2 > result.txt
But its only showing the result :
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
Note: Both the files have data in random order
linux awk
edited Jun 13 at 10:25
asked Jun 13 at 10:01
Maddys
33
33
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
$ join -t '|' -2 2 file1 file2
23455|abc|xyz|v343v
23455|abc|xyz|z565z
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
This performs a relational JOIN operation on the two files. The -t '|'
tells join
that |
is used as a field delimiter in the input data, and -2 2
tells it to use the second field in the second of the two files (rather than the first).
This assumes that the join column is sorted in the two files.
Are they not sorted, then pre-sort the files using
sort -t '|' -k1 -o file1 file1
sort -t '|' -k2 -o file2 file2
or sort at the same time as you call join
using a process substitution in a shell that supports this:
join -t '|' -2 2
<( sort -t '|' -k1 file1 )
<( sort -t '|' -k2 file2 )
thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
â Maddys
Jun 13 at 10:23
@Maddys See updated answer.
â Kusalananda
Jun 13 at 10:33
add a comment |Â
up vote
0
down vote
Using awk
:
awk 'BEGIN"NR==FNRa[$0];nextfor(i in a)if(index(i,$2)==1) print i,$1' file1 file2
The for
loops trough all lines of file1 stored in the array a
. If the first element of the second file matches and array entry, print it.
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
$ join -t '|' -2 2 file1 file2
23455|abc|xyz|v343v
23455|abc|xyz|z565z
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
This performs a relational JOIN operation on the two files. The -t '|'
tells join
that |
is used as a field delimiter in the input data, and -2 2
tells it to use the second field in the second of the two files (rather than the first).
This assumes that the join column is sorted in the two files.
Are they not sorted, then pre-sort the files using
sort -t '|' -k1 -o file1 file1
sort -t '|' -k2 -o file2 file2
or sort at the same time as you call join
using a process substitution in a shell that supports this:
join -t '|' -2 2
<( sort -t '|' -k1 file1 )
<( sort -t '|' -k2 file2 )
thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
â Maddys
Jun 13 at 10:23
@Maddys See updated answer.
â Kusalananda
Jun 13 at 10:33
add a comment |Â
up vote
1
down vote
accepted
$ join -t '|' -2 2 file1 file2
23455|abc|xyz|v343v
23455|abc|xyz|z565z
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
This performs a relational JOIN operation on the two files. The -t '|'
tells join
that |
is used as a field delimiter in the input data, and -2 2
tells it to use the second field in the second of the two files (rather than the first).
This assumes that the join column is sorted in the two files.
Are they not sorted, then pre-sort the files using
sort -t '|' -k1 -o file1 file1
sort -t '|' -k2 -o file2 file2
or sort at the same time as you call join
using a process substitution in a shell that supports this:
join -t '|' -2 2
<( sort -t '|' -k1 file1 )
<( sort -t '|' -k2 file2 )
thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
â Maddys
Jun 13 at 10:23
@Maddys See updated answer.
â Kusalananda
Jun 13 at 10:33
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
$ join -t '|' -2 2 file1 file2
23455|abc|xyz|v343v
23455|abc|xyz|z565z
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
This performs a relational JOIN operation on the two files. The -t '|'
tells join
that |
is used as a field delimiter in the input data, and -2 2
tells it to use the second field in the second of the two files (rather than the first).
This assumes that the join column is sorted in the two files.
Are they not sorted, then pre-sort the files using
sort -t '|' -k1 -o file1 file1
sort -t '|' -k2 -o file2 file2
or sort at the same time as you call join
using a process substitution in a shell that supports this:
join -t '|' -2 2
<( sort -t '|' -k1 file1 )
<( sort -t '|' -k2 file2 )
$ join -t '|' -2 2 file1 file2
23455|abc|xyz|v343v
23455|abc|xyz|z565z
23455|abc|xsd|v343v
23455|abc|xsd|z565z
34433|wer|sad|c9898
45655|fdf|fcd|b2323
This performs a relational JOIN operation on the two files. The -t '|'
tells join
that |
is used as a field delimiter in the input data, and -2 2
tells it to use the second field in the second of the two files (rather than the first).
This assumes that the join column is sorted in the two files.
Are they not sorted, then pre-sort the files using
sort -t '|' -k1 -o file1 file1
sort -t '|' -k2 -o file2 file2
or sort at the same time as you call join
using a process substitution in a shell that supports this:
join -t '|' -2 2
<( sort -t '|' -k1 file1 )
<( sort -t '|' -k2 file2 )
edited Jun 13 at 10:33
answered Jun 13 at 10:13
Kusalananda
101k13199312
101k13199312
thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
â Maddys
Jun 13 at 10:23
@Maddys See updated answer.
â Kusalananda
Jun 13 at 10:33
add a comment |Â
thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
â Maddys
Jun 13 at 10:23
@Maddys See updated answer.
â Kusalananda
Jun 13 at 10:33
thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
â Maddys
Jun 13 at 10:23
thanks for the quick reply.. please note that both the files have random data order.. join statement asks for sorted data..
â Maddys
Jun 13 at 10:23
@Maddys See updated answer.
â Kusalananda
Jun 13 at 10:33
@Maddys See updated answer.
â Kusalananda
Jun 13 at 10:33
add a comment |Â
up vote
0
down vote
Using awk
:
awk 'BEGIN"NR==FNRa[$0];nextfor(i in a)if(index(i,$2)==1) print i,$1' file1 file2
The for
loops trough all lines of file1 stored in the array a
. If the first element of the second file matches and array entry, print it.
add a comment |Â
up vote
0
down vote
Using awk
:
awk 'BEGIN"NR==FNRa[$0];nextfor(i in a)if(index(i,$2)==1) print i,$1' file1 file2
The for
loops trough all lines of file1 stored in the array a
. If the first element of the second file matches and array entry, print it.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Using awk
:
awk 'BEGIN"NR==FNRa[$0];nextfor(i in a)if(index(i,$2)==1) print i,$1' file1 file2
The for
loops trough all lines of file1 stored in the array a
. If the first element of the second file matches and array entry, print it.
Using awk
:
awk 'BEGIN"NR==FNRa[$0];nextfor(i in a)if(index(i,$2)==1) print i,$1' file1 file2
The for
loops trough all lines of file1 stored in the array a
. If the first element of the second file matches and array entry, print it.
answered Jun 13 at 11:10
oliv
85927
85927
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%2f449495%2fawk-print-duplicates-from-two-files%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