kentfort
kentfort

Reputation: 3

Insert newline before first line

I am trying to insert a newline before the first line of text in a file. The only solution i have found so far is this:

sed -e '1 i
')

I do not like to have an actual newline in my shell script. Can this be solved any other way using the standard (GNU) UNIX utilities?

Upvotes: 0

Views: 3607

Answers (4)

dawg
dawg

Reputation: 103834

You could use awk:

$ awk 'FNR==1{print ""} 1' file

Which will work with any number of files.

Upvotes: 1

thkala
thkala

Reputation: 86333

Here's a pure sed solution with no specific shell requirements:

sed -e '1 s|^|\n|'

EDIT:

Please note that there has to be at least one line of input for this (and anything else using a line address) to work.

Upvotes: 3

Dennis Williamson
Dennis Williamson

Reputation: 360085

For variety:

echo | cat - file

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798636

A $ before a single-quoted string will cause bash to interpret escape sequences within it.

sed -e '1 i'$'\n'

Upvotes: 2

Related Questions