Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-13-2014, 05:04 PM   #1
Registered: Jun 2013
Posts: 71

Rep: Reputation: Disabled
Ksh Help - Remove a list of lines from a larger file and leave remaining

I have a short list of ips

I have a larger list of ips

file2 other stuff more stuff udda more stuff dafdfdsa fdasfdsf afdad fdafdasfda afdafdsa afdafd fdafda

If any of the ips in file1 match in file2, I want to delete the matched line in file2.

remainingfile other stuff udda more stuff fdasfdsf fdafdasfda afdafd

I understand that I just loop through file1 and check against file2, what I dont know is how to remove only the lines matching file1.

while read line
grep "^$line " file2
done < file1
Ive been reading/searching on the web to see if I can find a sed example.. but no luck.
I appreciate any help.

Old 10-13-2014, 09:52 PM   #2
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,040

Rep: Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409

have you tried something like:

mv file2 file2.old
grep -v -f file1 file2.old > file2

Although you may need to edit file1 and place a '^' at the start of each line.
Check the grep man page for information regarding using the -v and -f options.

1 members found this post helpful.
Old 10-14-2014, 07:59 AM   #3
Registered: Jun 2013
Posts: 71

Original Poster
Rep: Reputation: Disabled
Thanks Evo .. I didnt realize grep could do it. That does the job.. a little slow on large files, but I can put up with that. Thanks again !
Old 10-14-2014, 08:07 AM   #4
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,435

Rep: Reputation: 5066Reputation: 5066Reputation: 5066Reputation: 5066Reputation: 5066Reputation: 5066Reputation: 5066Reputation: 5066Reputation: 5066Reputation: 5066Reputation: 5066
if speed was important: you would need to sort both files and a short perl/awk/whatever script will do the job much quicker....


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] AWK: Remove of Lines matching from a supplied List of Objects? CaptainDerp Linux - Newbie 12 05-30-2013 08:39 AM
How to remove first 2 lines of a file in a script nazs Programming 16 02-19-2007 07:08 AM
awk to remove first 3 lines and print remaining $1, $2 fields phyx Linux - General 1 01-10-2007 05:21 PM
How to remove lines from a file doza Linux - General 2 04-27-2005 11:59 AM
lines remaining when I launch an app or terminal spiteres Linux - General 0 05-27-2003 06:24 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:54 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration