search pattern and mark/tag

Hi All,

I have to search for patterns from a pattern file in a file and mark the matching lines.

Input File:

Student1 60 30
Student2 71 91
Student3 88 98

Pattern file:

Student1 Fail
Student2 Pass
Student2 Pass

Desired output:

Student1 60 30 Fail
Student2 71 91 Pass
Student3 88 98 Pass

Any help with awk or grep is greatly appreciated.

Is there a typo in the last line of "Pattern file"? Is it supposed to begin with Student3? If so, are both files sorted by their common key (the first column)?

1 Like

Thanks for pointing it out. Indeed its a typo. Its supposed to be Student3! and yes, both the files are sorted by first column.

Then you can probably just use join.

join input_file pattern_file

Regards,
Alister

1 Like

I guess this is homework, but since Alister has provided the solution, I'd like to provide the other.

awk 'NR==FNR{a[$1]=$2;next}{print $0,a[$1]}' pattern.file input.file

@ saint2006

Looks you can compare the mark directly in input.file to report the student fail or pass.

If you really want to learn shell, you can try to use input.file get the result directly. Compare the marks if both are great than 60, will be pass, otherwise, will be fail.

Ha! That hadn't occurred to me. I took it as a school teacher driven to unix scripting by the same budgetary constraints which sacked the sysadmin (not unlike the biologists and geneticists that often come here seeking help to process DNA and bat data). :wink:

Regards,
Alister

1 Like