Solved How to add a blank line before the first line?

In linux […]
There is no “Linux sed”. You probably meant GNU sed but it isn’t part of Linux. Another method is the line‑based editor ed(1):​
Bash:
ed -s file.txt << EOT
1i

.
wq
EOT
Since you need to rewrite the entire file anyway (I don’t know of any file system that supports prepending data blocks), the naive solution is of course:​
Bash:
cat - file.txt > file.txt~ << EOT

EOT
# cat instead of mv to preserve file attributes of original file.
cat file.txt~ > file.txt
rm $_
This needs some protection against errors, though, e. g. if the first cat(1) fails because the disk is full you do not want to continue. For a non‐interactive sh(1)ell script you can specify the -e option.​
 
In FreeBSD ... how?
I don't think that there is a common syntax that will get the job done with both FreeBSD sed(1) and GNU sed using an identical command.

Leverage this:
Code:
[0-0] # which sed gsed
/usr/bin/sed
/usr/local/bin/gsed
[1-0] # cat i.txt
L 1
L 2
L last
[2-0] # sed -e '1H;1g' i.txt

L 1
L 2
L last
[3-0] # gsed -e '1H;1g' i.txt

L 1
L 2
L last
[4-0] #

Of course, more compact would be sed -e '1{H;g;}' i.txt* but, that only works with gsed(1).

___
* edit: for a one-digit line number, actually requiring more characters ....
 
Leverage this:
An elegant solution. For in-place editing, the processing of the "I" (or "i") option is problematic with gsed:
Code:
$ gsed  -i "" -e '1H;1g' i.txt
gsed: can't read : No such file or directory
$ echo $?
2
It's possible to work around by removing the space between the "-i" and its (empty) argument, which works for both sed and gsed.
 
Command
Code:
sed -i -e '1{H;g;}' file.txt
works in FreeBSD and linux. Great thanks for everyone.
 
echo > empty_line.txt
cat empty_line.txt file.txt > new_file_with_added_empty_line.txt
Adds a blank line before the first line in all shells on all unixlike systems, right? 😁
Sorry, couldn't resist.
 
echo > empty_line.txt
cat empty_line.txt file.txt > new_file_with_added_empty_line.txt
Adds a blank line before the first line in all shells on all unixlike systems, right? 😁
Sorry, couldn't resist.
If you use > , you will delete original contents of the file. ;)

The purpose is to add a blank row in the first line to the file which has some contents.
 
If you use > , you will delete original contents of the file.
Maybe the filenames I used were too long to see what I've done:
first line produces a new file containing an empty line, only.
second line concatenates this file with the file in question a empty line has to be added to, and creates a new file again, which then is the original file with an added line before its context.

I spared the parts needed to rename the new one to the name of the original file, and delete the spare file, because I did not really meant this seriously, didn't really meant to discuss this very basic shell usage.
Clearly this thread was about sed/ed usage, but since the "specifications" let that part out I simply wanted to make a joke by fulfilling the asked goal with another way.
:rolleyes:

Edit:
I didn't see Kai Burghardt already posted that "naive solution", even more elegant than my approach.
 
Back
Top