Help with looping a file and grepping a string

I have 2 files: fileA and fileB.

content of fileA
---------------
admin.teacher is in new york;
admin.mason is in new york;
admin.driver is in new york city;
user.trucker is in hartford;
admin.developer is in new york state;

content of fileB
----------------
admin.teacher is in chigago;
user.teacher is in new york;
admin.mason is in new york;
admin.developer is in new york state
admin.plumber is in new york;
admin.driver is in new york city;
user.trucker is in hartford;
admin.nurse is in new york state;
admin.teacher is in new york
user.painter is in new york;

This is what I am trying to achieve:
I would like to loop through fileB and if any member in fileA
is found in fileB then that line in fileB will be commented with (--)
I am looking for a neat script to do this for me. thx.

This means that the resulting file to look like:

FileC
----------
admin.teacher is in chigago;
user.teacher is in new york;
--admin.mason is in new york;
--admin.developer is in new york state
admin.plumber is in new york;
--admin.driver is in new york city;
--user.trucker is in hartford;
admin.nurse is in new york state;
--admin.teacher is in new york
user.painter is in new york;

You will learn faster if you do your own homework.

crap. you if dont have anything to offer dont say nothin. Do i have to tell you how much effort i have put in before trying to seek help from the forum?

Maybe try smthg like

awk 'NR==FNR{A[$0]=1}NR!=FNR{print (A[$0]?"--"$0:$0)}' fileA fileB >fileC

... by the way , your example lack the trailing semicolon in fileB for the 2 lines :

admin.developer is in new york state
admin.teacher is in new york

Use nawk instead of awk if on SunOS / Solaris plateform

# cat f1
admin.teacher is in new york;
admin.mason is in new york;
admin.driver is in new york city;
user.trucker is in hartford;
admin.developer is in new york state;
# cat f2
admin.teacher is in chigago;
user.teacher is in new york;
admin.mason is in new york;
admin.developer is in new york state;
admin.plumber is in new york;
admin.driver is in new york city;
user.trucker is in hartford;
admin.nurse is in new york state;
admin.teacher is in new york;
user.painter is in new york;
# nawk 'NR==FNR{A[$0]=1}NR!=FNR{print (A[$0]?"--"$0:$0)}' f1 f2
admin.teacher is in chigago;
user.teacher is in new york;
--admin.mason is in new york;
--admin.developer is in new york state;
admin.plumber is in new york;
--admin.driver is in new york city;
--user.trucker is in hartford;
admin.nurse is in new york state;
--admin.teacher is in new york;
user.painter is in new york;
1 Like

awesome!
You made it look so easy. I tried doing looping and greping with some intemediate files. Even though I finally got it to work, the approach was not pretty and I did not like it.
Your solution is just excellent. I love one-liners and this solution is just cool. Works like a charm . Thanks

---------- Post updated at 01:13 PM ---------- Previous update was at 01:10 PM ----------

Deliverer,
could you give some short explanation how your code works? in other words decipher it? lol

'NR==FNR{A[$0]=1}NR!=FNR{print (A[$0]?"

Before explaining further, please consider the following statement, observe what happend in each column and with a bit of deduction you will get it
This should help you to understand :

# nawk '{print NR,FNR,FILENAME,$0}' f1 f2
1 1 f1 admin.teacher is in new york;
2 2 f1 admin.mason is in new york;
3 3 f1 admin.driver is in new york city;
4 4 f1 user.trucker is in hartford;
5 5 f1 admin.developer is in new york state;
6 1 f2 admin.teacher is in chigago;
7 2 f2 user.teacher is in new york;
8 3 f2 admin.mason is in new york;
9 4 f2 admin.developer is in new york state;
10 5 f2 admin.plumber is in new york;
11 6 f2 admin.driver is in new york city;
12 7 f2 user.trucker is in hartford;
13 8 f2 admin.nurse is in new york state;
14 9 f2 admin.teacher is in new york;
15 10 f2 user.painter is in new york;

---------- Post updated at 08:35 PM ---------- Previous update was at 08:18 PM ----------

if NR==FNR, we are scanning the first file we build an associative array indexed by the content of the line
if NR!=FNR, we are scanning the other file (note that when the scan of the second file start, the first file has already been entirely scanned so if an element of the previously built array is found based on the content of $0, then we add it "--"$0 otherwise we just leave it as is :$0

ctsgnb, hey genius, are you an instructor at MIT? thanks a lot

If you made any effort, perhaps you should have posted it. From what I can see you didn't make any.

-- Closed --

Spare us the insults. Actually gus2000 is entitled to assume you let others do your homework here and IMHO he is probably correct - from what i have seen here and over the years before this sure looks like homework.

Sorry to say that but your grammar skills seem to match your scripting proficiency.

It may be news to you but the answer is actually: YES! We are neither a help desk nor a "we-will-do-your-work-for-you-for-free"-agency. We take proud in helping people to help themselves and usually tell them where they went wrong in their own efforts at solving the problem instead of solving the problem for them.

For this to happen it would have been imperative to post what you have tried instead of exposing yourself to be what you probably are (and what gus2000 supposed you to be).

All in all: get better Unix skills, get a better attitude and you definitely get an infraction.

bakunin