Ubuntu – amend each line in a Text file using sed or alternative solution

command linetext processing

I want to amend each line in a Text file using sed or alternative solution

input.txt containing

line 1
line 2 
line 3 
...

output.txt should add two strings and

<before> line 1 and <after each line>
<before> line 2 and <after each line>
<before> line 3 and <after each line>
...

Can I use sed or something else?

Best Answer

With sed:

sed 's/.*/<before> & and <after each line>/' input.txt > output.txt

Explanations

  • s/x/y/ – substitute x by y
  • .* – take the whole line
  • & – get matched text

The rest is just the text you want to add before and after &.