LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Ksh Help - Remove a list of lines from a larger file and leave remaining (https://www.linuxquestions.org/questions/linux-newbie-8/ksh-help-remove-a-list-of-lines-from-a-larger-file-and-leave-remaining-4175522009/)

sumncguy 10-13-2014 06:04 PM

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

evo2 10-13-2014 10:52 PM

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.

sumncguy 10-14-2014 08:59 AM

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 !

pan64 10-14-2014 09:07 AM

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


All times are GMT -5. The time now is 06:18 PM.