Let me be specific: NOT using variable in sed 1st command but assigning sed output to variable in bash shell, THEN using variable in 2nd sed command to append variable string to end of string in target file.

In bash shell is this possible...mvstr='sed s/^#.../&/'

Why do this? Trying to delete search pattern "#..." at beginning of line in file and append to end of string in same file.

$MVSTR may be #spx, #loc, #con Second sed command? s/"$MVSTR"/$;\"/w line terminates with ;" and write file

Is there a one-liner to accomplish this in 8000 files?


Writing sed "-e" strings isn't always easy.

try this on a test file:
MVSTR="#loc";sed -re "s/^$MVSTR(.*)$/\1$MVSTR/" filename

or using your replacement string as a literal instead:
sed -re "s/^(#loc)(.*)$/\2\1/" filename


Note here that $ equals "end of line" in the first portion of the sed 's' command. AND to have bash replace MVSTR inside the quoted sed command it must be inside double quotes, not only to protect it from other bash "interventions". \1 and \2 are "auto variables" that contain the content of the first and second parentheses in the search portion.

By the way http://tldp.org has a couple of nice Bash guides... In these you find examples of how to do this in any number of files. Look for the find command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.