×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

compare 2 files and print the common content of file1

compare 2 files and print the common content of file1

compare 2 files and print the common content of file1

(OP)
Hi
I would like lines from file1 that are common to file2 as in the following example:

CODE

cat file1
22    A    alp    19    gtein    
255    B    macr    12    pring    
3    C    lin    12    pseue    
90    D    N-ace    8    1    

CODE

cat file2
B
D

CODE

cat outfile
255    B    macr    12    pring    
90    D    N-ace    8    1    

thank you for your help
 

RE: compare 2 files and print the common content of file1

With awk, you'll need to put file2 in an array and then print records from file1 if field 2 is on that array.

That's easier if you process file2 first, then file1. Google around for "awk array". Chances are you'll get yourself right back in this forum winky smile.

But there's a neat solution with the UNIX join command:

CODE

join -1 2 -2 1 file1 file2

Only glitch is that the output field separator is (by default) one space character. But if you can live with that...

HTH,

p5wizard

RE: compare 2 files and print the common content of file1

(OP)
thank you for your help.
Joseph

RE: compare 2 files and print the common content of file1

I'm trying to learn awk, so I done it as exercise
join.awk

CODE

# Run:
# awk -f join.awk file1.txt file2.txt
BEGIN {
  FS = " "
  lines_found = 0
}
{
  if (FILENAME == ARGV[1]) {
    # mark line from 1.file in array
    line_array[$1] = 1
  }
  if (FILENAME == ARGV[2]) {
    # print adequate lines from 2.file
    if (line_array[$2]) {
      print $0
      lines_found ++
    }
  }
}
END {
  printf "There were %d lines found and printed.\n", lines_found
}
Output:

CODE

$ awk -f join.awk file1.txt file2.txt
255    B    macr    12    pring    
90    D    N-ace    8    1
There were 2 lines found and printed.
I changed the file order i.e.
file1.txt

CODE

B
D
file2.txt

CODE

22    A    alp    19    gtein    
255    B    macr    12    pring    
3    C    lin    12    pseue    
90    D    N-ace    8    1

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close