how to find and comment out lines in one file against another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find and comment out lines in one file against another file
# 1  
Old 04-07-2011
Question how to find and comment out lines in one file against another file

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;

Moderator's Comments:
Mod Comment Double Post. Closed.

Last edited by Scott; 04-07-2011 at 08:55 PM..
# 2  
Old 04-07-2011
By "member" you mean whole lines to be the same? Or just the first column?
# 3  
Old 04-07-2011
below code is match by whole line.
Code:
awk 'NR==FNR{a[$0]++;next} a[$0]{$0="--"$0}1' fileA fileB

below code is match by member.
Code:
awk 'NR==FNR{a[$1]++;next} a[$1]{$1="--"$1}1' fileA fileB

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove Comment Lines From Script/Input File

Hello, I have a SAS code that predominantly has comments line and the real code like below and i want to remove ONLY THE COMMENTS from the code in the single line or spanned across multiple lines. /******************************************************************** *** This Is a Comment... (4 Replies)
Discussion started by: arooonatr
4 Replies

2. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

Find all lines in file such that each word on that line appears in at least n lines of the file

I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Discussion started by: uncleMonty
9 Replies

4. Shell Programming and Scripting

Comment ( -- ) lines from 10 to 25 for every .sql file

Platform : RHEL 5.4 I have several .sql files in a directory. I want to comment lines 10 to 25 for all .sql files. How can I do this ? The symbol for comment in SQL is -- eg: -- select salary from emp where empname = 'URS' ; (3 Replies)
Discussion started by: omega3
3 Replies

5. UNIX for Dummies Questions & Answers

Comment lines in file without vi editor

Legends, Can you please help me in following. I need to comment lines from “/tmp/a.txt” from the line A to line B through the command prompt only. Please use variables not direct values like 2 or 5 It can be done with VI editor but it's not matches with my requirement (: 2,5 s/^/#/g). ... (1 Reply)
Discussion started by: sdosanjh
1 Replies

6. UNIX for Dummies Questions & Answers

find lines in another file based on contents in a second file

Hello, I have a file with tab delimited columns like: File1 A 2 C R F 4 D Q C 9 A B ...... I want to grep out the lines in a second file, File2, corresponding to each line in File1 Can I do this: while read a b c d do grep '$a\t$b\t$c\t$d' File2 >>... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

7. Shell Programming and Scripting

Find and change the lines in the file.

Hi Everyone, I have a JIL (Job Information Language) Definition for Several Jobs in a file. I would like to do the following: insert_job: KS_INT_BIZ_INT job_type: c command: /home/filter/script.sh INT machine: MACHINE owner: filter permission: gx,mx date_conditions: 1 days_of_week:... (3 Replies)
Discussion started by: filter
3 Replies

8. Shell Programming and Scripting

using awk to comment out lines to the end of file

Hello, I have a file as follow a b c c d d e I would like to write a awk command to insert # from the first occurence of "c" to the end of the files. OUTPUT should be like this a b #c (5 Replies)
Discussion started by: phamp008
5 Replies

9. Shell Programming and Scripting

How to find number of lines in a file

Hi How do I find number of lines of a file? Below commands returned 0. But, the file is showing 20 lines when I open it in editplus tool. Each line contains 601 columns. Please advise I want to incorporate some word at the begining of each of those 20 lines -Somesh $ wc -l <... (2 Replies)
Discussion started by: somesh_p
2 Replies

10. Shell Programming and Scripting

find string, then get the next 3 lines in a file

Hi all. HPUX - /bin/sh (posix) I am parsing a 3 field flat file, space deliminted example data.file acct dining mem open 0 50 dep 50 0 close 255 0 acct plus mem open 100 100 dep 50 0 close 323 0 I would like to find strings, then write the next 3 lines out to another file ... (8 Replies)
Discussion started by: lyoncc
8 Replies
Login or Register to Ask a Question