LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Delete line from file (http://www.linuxquestions.org/questions/linux-newbie-8/delete-line-from-file-451222/)

nebbus 06-03-2006 11:47 AM

Delete line from file
 
Hi

I have two files, keep.txt and remove.txt. Both of them contain a list of files with full path like this:

keep.txt
/full/path/to/file/name1.txt
/full/path/to/file/name2.txt
/full/path/to/file/name with space1.txt
/full/path/to/file/name with space2.txt
/full/path/to/another/file/name1.txt
/full/path/to/another/file/name2.txt
/full/path/to/another/file/name3.txt
/full/path/to/another/file/name4.txt
/full/path/to/file/name4.txt

remove.txt
/full/path/to/another/file/name4.txt
/full/path/to/another/file/name2.txt
/full/path/to/file/name3.txt
/full/path/to/file/name1.txt
/full/path/to/file/anothername.txt

Every line that exists in remove.txt should be removed from keep.txt. It is possible for a filename to only figure in remove.txt, and a filename can have multiple entries in keep.txt. The path can contain spaces.

I have tried with sed but I think there is a problem because the path contains unescaped /

I want to read remove.txt line by line, and check if that line also figures in keep.txt, if so delete it from keep.txt. remove.txt is no longer needed when the script finishes.

Can someone please help me?

nebbus

spirit receiver 06-03-2006 12:43 PM

You might want to escape the "." and other special characters that might appear in your filenames. I also assume that your filenames don't contain "|", but then the following should work:
Code:

#! /bin/bash
IFS=$'\n'
for NAME in $(cat remove.txt)
do
  sed -ie "\|^$NAME\$|d" keep.txt
done


nebbus 06-03-2006 01:07 PM

Quote:

Originally Posted by spirit receiver
You might want to escape the "." and other special characters that might appear in your filenames. I also assume that your filenames don't contain "|", but then the following should work:
Code:

#! /bin/bash
IFS=$'\n'
for NAME in $(cat remove.txt)
do
  sed -ie "\|^$NAME\$|d" keep.txt
done


Thanks a lot! It worked!

You assumed right, the filenames don't contain "|".

But, if you have the time... Could you please explain what the IFS=$'\n' does? The rest I think I can understand.

nebbus

spirit receiver 06-03-2006 01:46 PM

IFS is an abbreviation of "internal field separator". We need that environment variable to deal with the space characters contained in your filenames: By default, the output of "cat remove.txt" would be split at any whitespace to form a list for the loop. But we want that output to be split only at newline characters, that's why we define IFS to contain a newline character only.
This is explained a little further at http://www.faqs.org/docs/bashman/bashref_33.html.


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