Home > Enterprise >  Appending line with special characters in shell
Appending line with special characters in shell

Time:08-11

I want to appends each line of the file as below:

file.txt :

adminsrv.tar

Output:

- archive: "packages/images/adminsrv.tar"

I tried several method like the one below and ended up with error:

input=/var/tmp/file.txt  
while read -r line
Do
sed -i 's|$line|" - archive: "packages//images//$line"'
done < $input

CodePudding user response:

You can just insert new text before every line using sed and there is no need to run a shell loop:

sed 's~.*~- archive: "packages/images/&"~' "$input"

- archive: "packages/images/adminsrv.tar"

This sed uses .* for search for any text and then it substitutes matched text with replacement text and & which is back-reference of the matched text.

CodePudding user response:

Perhaps, if you want to edit in place

sed -i'' -Ee 's@^(.*)$@- archive: "packages/images/\1"@' file.txt

Check man sed.

  • Related