Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove lines from file 2

Status
Not open for further replies.

pavNell

Technical User
Sep 27, 2002
178
US
My task is to remove lines from fileA that might also be in fileB.

For instance,
fileA looks like:

xxx
ooo
yyy
iii
zzz

fileB looks like:
iii
vvv
ttt
xxx
777
www

Needing the lines xxx and iii to be removed from fileA.

I've wrestled with diff, sort, uniq, comm and can't quite get it. Thanks for any help.
 
[tt]grep -vf fileB fileA[/tt]

You might need to dig around for a version of grep that supports -f, for example on Solaris you have to use /usr/xpg4/bin/grep.

Annihilannic.
 
Code:
sed 's#.*#/&/d#' fileB > /tmp/b && sed -f /tmp/b fileA > newFileA && rm /tmp/b

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I should have said grep -xvf fileB fileA to make sure that partially matching lines aren't removed.

Similarly vgersh99's solution could be modified for the same reason:

[tt]sed 's#.*#/^&$/d#' fileB > /tmp/b && sed -f /tmp/b fileA > newFileA && rm /tmp/b[/tt]


Annihilannic.
 
Annihilannic,
I don't see where my solution has been changed - I must be going blind ;)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
That was quick!! And works like a charm.
Thank a million to all of you!!
 
I have to give the [tt]comm[/tt] solution since this is the purpose of the [tt]comm[/tt] command.
Code:
sort fileA > fileA.sorted
sort fileB > fileB.sorted
comm -23 fileA.sorted fileB.sorted
Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top