LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 06-03-2006, 10:47 AM   #1
nebbus
LQ Newbie
 
Registered: Jan 2006
Location: Norway
Distribution: Kubuntu, ubuntu
Posts: 11

Rep: Reputation: 0
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
 
Old 06-03-2006, 11:43 AM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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
 
Old 06-03-2006, 12:07 PM   #3
nebbus
LQ Newbie
 
Registered: Jan 2006
Location: Norway
Distribution: Kubuntu, ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
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
 
Old 06-03-2006, 12:46 PM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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.
 
  


Reply

Tags
line, remove, sed


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 07:32 AM
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 04:41 PM
How do I delete a line in a file with fstream (C++)? CrazyHoboMan Programming 3 08-18-2005 02:06 AM
locate and delete a line from a file eilison Programming 3 05-17-2005 06:07 PM
Delete line from flat text file in C zaichik Programming 6 01-26-2005 06:16 PM


All times are GMT -5. The time now is 12:03 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration