match 2 files in Linux one file with 1 column second file with 2 columns
match 2 files in Linux one file with 1 column second file with 2 columns
I have 2 files.
file 1:
123456789
789465123
456789132
file 2:
r6rg6r6,123456789
dfg4665,789465123
dthegfd,456789132
I need to match the id's and print into a file the id's
r6rg6r6
dfg4665
dthegfd
I tried with awk, cmp, comm, diff and nothing
awk
cmp
comm
diff
this is the commands that im trying to run in the command line
awk '(NR==FNR)a[$1]; next($2 in a)print $1' file1 file2 > test.csv
- diff file1 file2 > test.csv - awk 'FNR==NRa[$1];next($1 in a)print $1' file1 file2 > test.csv - comm -12 file1 file2 > text.csv
– E.M
Sep 4 '18 at 11:33
@E.M This is a scray unreadable command line. Can you edit your question and feed in this information there?
– kvantour
Sep 4 '18 at 11:52
Are there really headers in both files? And how are the columns delimited? TABs?
– PesaThe
Sep 4 '18 at 13:51
there is no headers in the files i wrote that for the example
– E.M
Sep 4 '18 at 13:57
1 Answer
1
Did you try grep, it is mighty powerful
grep
grep -F <file1> <file2> | cut -d ' ' -f1
or just awk:
awk
awk '(NR==FNR)a[$1]; next($2 in a)print $1' <file1> <file2>
However, since you already tried this solution, it might be a problem with CRLF terminations due to a dos/windows copy. While you can resolve this with dos2unix, you can also use the following awk line (GNU awk only):
dos2unix
awk
awk -v RS='r?n' '(NR==FNR)a[$1]; next($2 in a)print $1' <file1> <file2>
im tring to do awk '(NR==FNR)a[$1]; next($2 in a)print $1' <file1> <file2> > test.csv and i get an empty file
– E.M
Sep 4 '18 at 12:04
@E.M What you are probably suffering from is the dos carriage return
r. In "dos"-files. line termination is done with rn while in unix lines it is n only. Since awk uses n as a record separator, your indices or $2 fields have an extra r. This is why a match will always fail. You can check this with the command file <filex> and you can convert with dos2unix.– kvantour
Sep 4 '18 at 12:10
r
rn
n
awk
n
$2
r
file <filex>
dos2unix
but there is no extra r
– E.M
Sep 4 '18 at 12:14
@E.M Are you indicating that
file <filename1> and file <filename2> both return ASCII text and not ASCII text, with CRLF line terminators– kvantour
Sep 4 '18 at 12:17
file <filename1>
file <filename2>
ASCII text
ASCII text, with CRLF line terminators
OP significantly changed the format of input files. This command works with comma as a delimiter:
awk -F, '....– PesaThe
Sep 4 '18 at 14:03
awk -F, '...
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Prefix your code/data with four white spaces. Please take a look at editing-help.
– Cyrus
Sep 4 '18 at 11:29