Categories
Development Linux

Shell *for* loop through file and for each line use sed to delete lines from other file

I’m trying to clean up long-cluttered-filename.txt by deleting the lines that match the strings in my delete-these-lines.txt

$ cat delete-these-lines.txt
context
platform
server
civicrm
site_enabled
client
redirection
cron_key
^\s*[0-9]*\s
alias
profile
install
ssl_
address

…and with a simple for loop I can iterate through the lines of that file like so:

$ for l in delete-these-lines.txt; do cat $l; done
context
platform
server
civicrm
site_enabled
client
redirection
cron_key
^\s*[0-9]*\s
alias
profile
install
ssl_
address

but when I try to substitute cat for sed it doesn’t work. Neither of these work:

for l in delete-these-lines.txt; do sed -i "/$l/d" long-cluttered-filename.txt; done
for l in delete-these-lines.txt; do sed -i '/"$l"/d' long-cluttered-filename.txt; done

Is this a problem with the $l variable being interpolated? Is this a limitation of for loops?

I see answers like loop sed to delete line with strings provided in a list of strings from a text file and Read line from file then delete that use while loops with read but I’d prefer to use a for loop if it’s possible, and if it’s not possible I’d like to know why.

Leave a Reply

Your email address will not be published. Required fields are marked *