Review your favorite Linux distribution.
Go Back > Blogs > linux-related notes
User Name


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Great tip to mass edit a file with lots of paths, using variables and "sed"

Posted 12-17-2019 at 08:33 AM by the dsc
Tags sed

Sed "default" delimiter is "/", or at least that's what most people use. I tipically find "|" more readable, so that's "my" default. Regardless, it can still be troublesome if you're dealing with paths and variables, it seems somehow the command will "break up".

Glenn Jackman posted on Stack Overflow:
A pure bash answer: use parameter expansion to backslash-escape any slashes in the variable:

sed "s/${var//\//\\/}/replace/g" $file
(Still works with pipe instead of slash)

From that you can do things such as:

while read rmvpath ; do  
grep "${rmvpath}" pathliststoedit*.* | sed 's|:.*||' | sort -u | while read f ; do
sed -i "${rmvpath//\//\\/}/d" "${f}"  
done ; done < listofpathstoremove.txt
Will remove any path listed on "listofpathstoremove.txt" from any file starting with "pathliststoedit."

My actual implementation is an one-liner, and has "cat" instead of the file input at the end of done, but I guess the fix here must work as well. In any case, create a directory for back-up files of the "pathliststoedit" type. If it has any use, of course.

Sed there is just removing the line, a one-command version of something along the lines of "grep -v pattern file | sponge file" or whatever, but it can of course be used to edit the lines and save on the same file as well.
Posted in Uncategorized
Views 3018 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 09:40 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration