Struggling with AWK

I know this is an easy problem, but I've been struggling with it all morning and finally admit I need help.

I have two files that I need to compare. After doing this the easy way (grep -F) I realize that there are other fields in file2 that match field1 in file1, so I turned to awk.

I want to compare the first field in each, and if field1 of file1 matches field1 of file2, print out the entire line in file2. I've been trying to write an awk script but just don't fully understand how to do this.

I get as far as:

awk '
FS="\t";
BEGIN {
while (getline <"file1.txt"){
$fieldq==$1;

and I can't figure out how to read in the second file to do the comparison and then print $0 from file2.

  1. Please use
    text CODE
    tags
  2. Please use sample input and output, its like using picture instead of words

try this,

awk 'BEGIN { while ( getline < "abc.txt" ) { arr[$0] = $1 } } { if ( $1 in arr ) { print } }' file2.txt

( not tested )

Another approach:

awk 'NR==FNR{a[$1];next}$1 in a' file1 file2
awk 'NR==FNR{a[$1];next}$1 in a' file1 file2

---------- Post updated at 10:08 AM ---------- Previous update was at 10:07 AM ----------

Wow , just few seconds difference :wink:

Can I suggest that you might look at Perl instead?

Awk is, well, awkward. Years ago, I'd keep my awk and sed skills up because I couldn't count on anything better being available on clients machines, but today Perl is everywhere. Why struggle when a Perl script will give you so much more with less effort?

Just my opinion, of course. See my "Why I love Perl" at Why I love Perl - I wrote that almost 10 years ago when I first started using Perl and I only love it more today.

Thanks, as soon as I get this report finished I'm going to go back and read up some more on awk. I still have the sed&awk book somewhere, just haven't used it in 10 years and am beyond rusty.

---------- Post updated at 08:22 AM ---------- Previous update was at 06:27 AM ----------

Because I'm older, stubborn and can do this at the shell prompt, no other good reason. Again, I've been away from unix and coding for a while, but at one point 10 or 12 years ago was extremely fluent, especially in PERL. I used to hassle people in my group for writing scripts when I could do the same thing at the shell prompt :b:

perl -ne '/^(.+?) /; $foo{$1}++; eof && print if $foo{$1} > 1' file1 file2

You imply that you can't solve this at the command prompt with perl. Not so. You also imply that you would have to "bother" other people if you use perl. True in this case. Consider me "bothered" for a perl command-liner.

sed / perl: dude, whatever you are comfortable with :slight_smile:

---------- Post updated at 01:57 AM ---------- Previous update was at 12:44 AM ----------

perl -ne '/^(.+?) /; ++$_{$1}; $ARGV eq 'b' && print if $_{$1} > 1' a  b

Sorry, the perl above is broken, this is not.

awk is awkward if you don't know how to awk.

With regards to string parsing and text manipulation, both are on par, sometimes awk is better with regards to speed and conciseness.

---------- Post updated at 01:10 AM ---------- Previous update was at 01:07 AM ----------

put your FS into BEGIN block. In the BEGIN block, you can't access field variables $1-$n.