Abhijeet Banerjee
Abhijeet Banerjee

Reputation: 37

Comparing 2 files content with specific values in shellScript

I am very new to the shell script and need help I have 2 files i.e

Student.txt
001, Peter, class3
002, Mohit, class4
and so on...

Marks.txt
001, History, 45
001, Maths, 55
002, computer, 76
002, Maths, 96
and so on...
I want to read the first word (i.e. Roll No. )from Student.txt i.e. 001,002 in my example and then search the content (roll no.) in another file Marks.txt 1st word AND 2nd word should be "History", (condition: $1 == roll no && $2 == History)

I going through awk cmd and tried but not able to make a complete solution

awk -F "," '{ print $1 }' student.txt awk -F "," '{ print $1, $2 }' marks.txt

Upvotes: 0

Views: 31

Answers (1)

Gyuri16
Gyuri16

Reputation: 143

This command first collects the roll numbers from the Marks.txt that have History in the second column and then prints lines from Student.txt that have roll number from this list:

awk 'BEGIN {FS=", "} FNR==NR{ if ($2=="History"){a[$1];} next} $1 in a' Marks.txt Student.txt

Output:

001, Peter, class3

EDIT: see this for more info on processing multiple files with awk: Using AWK to Process Input from Multiple Files

Upvotes: 1

Related Questions