1

file1

area@wide.com
geo@earth.com
sun@far.com

file2

area@wide.com:200
moon@mars.com:100
venus@distance.com:0
geo@earth.com:201
sun@far.com:10

file3 should look like:

area@wide.com:200
geo@earth.com:201
sun@far.com:10

I tried:

awk -F, 'BEGIN{OFS=":"} NR==FNR{a[$1$2]=$3;next}'
oguz ismail
  • 1
  • 16
  • 47
  • 69
awkselfish
  • 21
  • 3
  • 1
    Why are you using commas as a delimiter, when there don't seem to be any commas in the data? – Ian McGowan May 26 '20 at 06:36
  • again I was testing it out but it should be ":" – awkselfish May 26 '20 at 06:37
  • 1
    Can you paste your actual command, and the output? The question doesn't make much sense as-is. Are you trying to only show lines from file2, where the first field appears in file1? – Ian McGowan May 26 '20 at 06:39
  • I've edited the file structures and what the output should look like – awkselfish May 26 '20 at 06:46
  • 1
    @awkselfish, IMHO you should really post correct samples(which are near to your actual Input_file(s)), codes posted in 2 posts were working for your samples but then Sundeep used his nice trick to remove spaces from your lines and it got it worked, so please always work on your samples to give us clear picture about your question, cheers. – RavinderSingh13 May 26 '20 at 07:08

1 Answers1

2
awk -F: 'NR==FNR{a[$0]; next} $1 in a' file1 file2
  • -F: set : as field separator
  • NR==FNR{a[$0]; next} build array keys based on complete line contents of file1
  • $1 in a print line from file2 if first field is a key in array a


Looks like your files may have dos-style line ending, in which case you can use:

awk -F: 'NR==FNR{sub(/\r$/, ""); a[$0]; next} $1 in a' file1 file2

Here, the carriage return at the end of lines in file1 is removed before using it as a key, thus it will match first field of file2. Also, output will be dos-style.

See also Why does my tool output overwrite itself and how do I fix it?


If there are whitespace characters like space, tab, etc at end of line, that can also cause issue. In such a case, use

awk -F: 'NR==FNR{sub(/[[:space:]]*$/, ""); a[$0]; next} $1 in a' file1 file2

This will remove extra whitespaces at end of line before using it as a key. This will also work for dos-style files.

Sundeep
  • 23,246
  • 2
  • 28
  • 103
  • awk 'NR==FNR{sub(/\r$/, ""); a[$0]; next} $1 in a' file1 FS=: file2 THIS WORKED. Thank you. – awkselfish May 26 '20 at 06:58
  • does it work if you use `awk 'NR==FNR{sub(/[[:space:]]*$/, ""); a[$0]; next} $1 in a' file1 FS=: file2` – Sundeep May 26 '20 at 07:00
  • 1
    You could use `awk -F':' '...' file1 file2` instead of setting FS among the file name args after the text of the script. – Ed Morton May 26 '20 at 11:51
  • 1
    @EdMorton right! I do not recall why I set it after file1, I probably started with some assumption but unable to recall now.. probably something like what if file1 has `:` character but then `$1` for file2 cannot have `:` character – Sundeep May 26 '20 at 12:01