Help with looping a file and grepping a string


 
Thread Tools Search this Thread
Operating Systems AIX Help with looping a file and grepping a string
# 1  
Old 04-07-2011
Java 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;
# 2  
Old 04-07-2011
You will learn faster if you do your own homework.
# 3  
Old 04-07-2011
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?
# 4  
Old 04-07-2011
Maybe try smthg like
Code:
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 :
Code:
admin.developer is in new york state
admin.teacher is in new york

Use nawk instead of awk if on SunOS / Solaris plateform

Code:
# 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;

Code:
# 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;

Code:
# 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;


Last edited by ctsgnb; 04-07-2011 at 03:08 PM..
This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 04-07-2011
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]?"
# 6  
Old 04-07-2011
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 :

Code:
# 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
# 7  
Old 04-07-2011
ctsgnb, hey genius, are you an instructor at MIT? thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

2. Shell Programming and Scripting

Grepping the logs with respect to string search

Hi Folks, I have a log file at the following location.. /opt/ert/abc.log Now abc.log contain the following enteries in this format below.. 23-Jul-2014 10:09.32.204 ERROR abc.log cdfrer tyre fgty >>>>> cqno : 78539132 abc Id : 0 Sabc : 20140724 Now in log file (abc.log) I want to... (2 Replies)
Discussion started by: tuntun343466
2 Replies

3. AIX

Grepping before and after lines for required string

Hi All, I am new to AIX unix . i need to grep for a pattern and if pattern is found then i need 3 before the pattern line found and 3 lines after the pattern found. (11 Replies)
Discussion started by: coolvibh
11 Replies

4. Shell Programming and Scripting

Display file date after grepping a string in the file

Hi All, I need to recursively grep several folders for a MAC address and display the results with the date of the file name at the start. Even better would be if the final results were displayed chronologically so the newest file is always at the end. Oldest at the top, regardless of what... (8 Replies)
Discussion started by: quemalr
8 Replies

5. Shell Programming and Scripting

grepping multiple matches in a single string

Hi All, I'm trying to grep for 3 patterns in a string of gibberish. It so happens that each line is appended by a date/time stamp and i was able to figure out how to extract only the datetime. here is the string.. i have to display tinker tailor soldier spy Please can some help... (2 Replies)
Discussion started by: Irishboy24
2 Replies

6. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

7. Shell Programming and Scripting

Grepping string from out file

Guys .. Need to pull this highlighted strings irrespective of line numbers & should be echoed . But these strings are from Outfile from different dir. In which way this can be grepped ?? Need an idea http-timeout 120 seconds persistent-timeout 180 seconds host-rewriting on ... (7 Replies)
Discussion started by: raghunsi
7 Replies

8. Shell Programming and Scripting

Grepping log file

Dear All, I have a log file that is dislpayed as: <msg time='2009-10-14T05:46:42.580+00:00' org_id='oracle' comp_id='tnslsnr' type='UNKNOWN' level='16' host_id='mtdb_a' host_addr='UNKNOWN' version='1'> <txt>14-OCT-2009 05:46:42 *... (19 Replies)
Discussion started by: x-plicit78
19 Replies

9. UNIX for Dummies Questions & Answers

grepping the first 3 characters from a file

give this a try and let me know if it works grep '^' filename rachael (2 Replies)
Discussion started by: rachael
2 Replies

10. UNIX for Dummies Questions & Answers

grepping the first 3 characters from a file

Hi I was wondering if it's possible to use a command to get the first 3 characters of a line in a text file, I tried grep but it returns the whole line but I am only interested in the first 3 characters. Is this possible with grep or I need any other command? Also is it possible deleting from... (2 Replies)
Discussion started by: g-e-n-o
2 Replies
Login or Register to Ask a Question