-   Linux - Newbie (
-   -   Ksh Help - Remove a list of lines from a larger file and leave remaining (

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


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.


evo2 10-13-2014 10:52 PM


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.


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.