1

I am using sed to insert some lines above a pattern in a file. Example of the file

Stuff

Pattern

Stuff

Here is the code I am using to split up the file to insert text before Pattern

     patternline=$(grep -n "Pattern" "/my/file" | cut -f1 -d:)
     firstcut=$(($patternline -1))

     firstpart=$(sed -n 1,"$firstcut"p "/my/file")
     secondpart=$(sed -n ''"$abspathcomment"',$p' "/my/file")

     # Indentation intentional as snippet is nested within an IF statement
     text=$(cat <<EOF

Text I want to insert with one leading and two trailing new lines


EOF
)

     echo "$firstpart$text$secondpart" > "/my/file"

I am splitting the file using sed and inserting the text I wish in the middle and finally using cat to output the contents to the same file.

I expect to get (something similar to) the following output for the file

Stuff

Text I want to insert with one leading and two trailing new lines


Pattern

Stuff

But instead I get

Stuff

Text I want to insert with one leading and two trailing new linesPattern

Stuff

I'm not sure if sed or bash is stripping the newlines. How can I keep them when echoing the result to a file in bash?

1
  • 2
    In addition to the neat sed solution posted by Kusalananda, note that you don't need a HERE doc, even if you want to assign to text. Just use quotes: text=' ..... '. The text between the quotes can span several physical lines. Feb 23, 2018 at 11:04

1 Answer 1

3

It's the command substitution that eats up the trailing newlines:

a=$( printf 'hello\nworld\n\n\n\n\n\n' )
printf 'a is "%s"\n' "$a"

Output:

a is "hello
world"

Using sed to solve the original problem:

sed '/Pattern/i\
Text I want to insert with one leading and two trailing new lines\
\

' file

Or, with GNU sed:

sed '/Pattern/i Text I want to insert with one leading and two trailing new lines\n\n' file

Here, we use the i ("insert") command in sed to insert a particular set of lines before any line matching Pattern. By escaping the two newlines (in the first sed variation) we are able to insert them into the text. The leading newline in the result is the same that was preceding Pattern from the start.

For the example data, this generates

Stuff

Text I want to insert with one leading and two trailing new lines


Pattern

Stuff

This has the benefit that none of the existing whitespace is modified.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .