1

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
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93

2 Answers2

1

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

Perhaps, if you want to edit in place

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

Check man sed.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134