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





Prefix your code/data with four white spaces. Please take a look at editing-help.
– Cyrus
Sep 4 '18 at 11:29





- 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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?