Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8502883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:36:30+00:00 2026-06-11T01:36:30+00:00

Could some one please help to compare two files, i have used the below

  • 0

Could some one please help to compare two files,
i have used the below command but i couldnt succeed on it,

awk -F, ‘NR == FNR {a[$1,$2]; next} (($1,$2) in a )’ temp1.dat temp2.dat

Here is my need,
need to compare first two fields in the below two dat files and merge the result as expected in file3(first field, second field, 3 field of temp1.dat, 3 field of temp2.dat)

File1:temp1.dat

A, AB,100
B,BB,200
C,CC,300

File2:temp2.dat

A,AB,10
C,CC,30
D,DF, 4

File3 :output

A, AB,100,10
C,CC,300,30
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:36:32+00:00Added an answer on June 11, 2026 at 1:36 am

    awk -F, 'BEGIN{OFS=","}FNR==NR{a[$1$2]=$3;next}($1$2 in a && $3=$3","a[$1$2])' file2 file1

    tested below:

    > cat file1
    A,AB,100
    B,BB,200
    C,CC,300
    > cat file2
    A,AB,10
    C,CC,30
    D,DF,4
    > awk -F, 'BEGIN{OFS=","}FNR==NR{a[$1$2]=$3;next}($1$2 in a && $3=$3","a[$1$2])' file2 file1
    A,AB,100,10
    C,CC,300,30
    > 
    
    • FNR==NR{a[$1$2]=$3;next} is applied for the first file file2
    • it says untill FNR==NR execute teh block of code.
    • FNR=line number of the current file
    • NR= line number of the total lines of two files.
    • so after the above statement an associative array with index as $1$2 and value as $3.
    • Now ($1$2 in a && $3=$3","a[$1$2])
      this executes for FNR!=NR.where in it checks for index $1$2 exists as an index in the array and then teh second condition is changing the 3rd field of file1 to $3=$3","a[$1$2]so now $0 contains the common($1$2) lines which changed 3rd field in them.

    similar logic has to be written for four files also.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could some one please help me out on this I have the following json
Please see following examples first, and which one is better? Could you compare some
Could some one please help me understand when to use SNAPSHOT isolation level over
Could some one please help with code. I want to show the submenu only
Its late and I'm struggling, could some one please help me out. I want
Could someone please help me with the code for below requirement or scenarios...I achieved
yes, this is a homework-type question but could you please help me out? In
Could all those CORBA experts out there please help me with this one. I
I have a big problem I hope some one can help me with it.
Could someone please help me to convert this String into a Map ... String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.