Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.