LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 10-13-2014, 06:04 PM   #1
sumncguy
Member
 
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

Quote:
file1
10.10.10.2
10.10.10.4
10.10.10.6
10.10.10.8
10.10.10.10
I have a larger list of ips

Quote:
file2
10.10.10.1 other stuff
10.10.10.2 more stuff
10.10.10.3 udda more stuff
10.10.10.4 dafdfdsa
10.10.10.5 fdasfdsf
10.10.10.6 afdad
10.10.10.7 fdafdasfda
10.10.10.8 afdafdsa
10.10.10.9 afdafd
10.10.10.10 fdafda

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

Quote:
remainingfile
10.10.10.1 other stuff
10.10.10.3 udda more stuff
10.10.10.5 fdasfdsf
10.10.10.7 fdafdasfda
10.10.10.9 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.

Code:
while read line
do
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.

Pop
 
Old 10-13-2014, 10:52 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

have you tried something like:

Code:
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.

Evo2.
 
1 members found this post helpful.
Old 10-14-2014, 08:59 AM   #3
sumncguy
Member
 
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, 09:07 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

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


Reply


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 09:39 AM
How to remove first 2 lines of a file in a script nazs Programming 16 02-19-2007 08:08 AM
awk to remove first 3 lines and print remaining $1, $2 fields phyx Linux - General 1 01-10-2007 06:21 PM
How to remove lines from a file doza Linux - General 2 04-27-2005 12:59 PM
lines remaining when I launch an app or terminal spiteres Linux - General 0 05-27-2003 07:24 AM


All times are GMT -5. The time now is 08:54 AM.

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