I have a file, say input, containing patterns like below:
quantum_mech_.*
astrophysics_.*
geology_.*
economy_*
I have another file , say subjects, which looks like:
quantum_mech_[101]
astrophysics_[102]
geology_[203]
quantum_mech_[007]
geology_[205]
I want to grep each of the line from input file and search the file "subject" and output the first match only and also print "Not Matched" if the line is not found in subject file at all . So I am expecting an output like:
quantum_mech_[101]
astrophysics_[102]
geology_[203]
Not Matched
I know this is pretty old problem, but none of the methods seem to be working properly for me. I tried several variations of below code:
script.csh:
cat $1 | while read line
do grep $line ./subject | head -1 >> output
set VAR=$?
if ( $VAR==0 ) then
echo "Not Matched\n" >> output
endif
done
Run As:
script.csh input
Any help/pointers using sed/grep/csh will be great.
Thanks and regards,
This works fine in csh and bash.
for line in `cat $1`;
do
grep -m1 $line ./subject || echo "Not matched"
done >> output
Thanks to dogbane's pointer, below is a better (and correct) way to do the same. The above also has issues when the lines have spaces in them.
while read line
do
grep -m1 "$line" ./subject || echo "Not matched"
done < $1 >> output