Skip to content
Advertisement

linux sed, do insert and append at the same time

I have a question about linux sed command. I just want to insert something before every line and append something after every line. And i want to do these things at the same time.

For example, assume file.txt is like:

1
2
3 

I need to output:

hello
1
world
hello
2 
world
hello 
3
world

so I think the command should be like:

cat file.txt | sed 'i
hello
a
world
'

But the result is not right, a is not interpreted as sed command, do I need some delimiters here or did I do something wrong?

Advertisement

Answer

You should remove the backslash after “hello” and “world”, so the command becomes:

cat file.txt | sed 'i
hello
a
world 
'

Having a backslash after “hello” joins “hello” to “a” as if “a” is another line to be inserted.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement