Why won't the tab be inserted on the first added line?

89 views Asked by At

I am trying to add multiple lines to a file, all with a leading a tab. The lines should be inserted on the first line after matching a string.

Assume a file with only one line, called "my-file.txt" as follows:

foo

I have tried the following sed command:

sed "/^foo\$/a \tinsert1\n\tinsert2" my-file.txt

This produces the following output:

foo
tinsert1
    insert2

Notice how the the tab that should be on the first (inserted) line is omitted. Instead it prints an extra leading 't'.

Why? And how can I change my command to print the tab on the first line, as expected?

2

There are 2 answers

2
hek2mgl On BEST ANSWER

With GNU sed:

sed '/^foo$/a \\tinsert1\n\tinsert2' file
     <---- single quotes!      --->

Produces:

foo
    insert1
    insert2

From the manual:

  a \

  text   Append text, which has each embedded newline preceded by a backslash.

Since the text to be append itself has to to be preceded by a backslash, it needs to be \\t at the beginning.


PS: If you need to use double quotes around the sed command because you want to inject shell variables, you need to escape the \ which precedes the text to be appended:

ins1="foo"
ins2="bar"
sed "/^foo\$/a \\\t${ins1}\n\t${ins2}" file
0
Ed Morton On

sed is for doing s/old/new on individual strings, that is all. Just use awk:

$ awk '{print} $0=="foo"{print "\tinsert1\n\tinsert2"}' file
foo
        insert1
        insert2

The above will work using any awk in any shell on every UNIX box and is trivial to modify to do anything else you might want to do in future.