In linux […]
ed -s file.txt << EOT
1i
.
wq
EOT
cat - file.txt > file.txt~ << EOT
EOT
# cat instead of mv to preserve file attributes of original file.
cat file.txt~ > file.txt
rm $_
In FreeBSD ... how?
[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] #
sed -e '1{H;g;}' i.txt
* but, that only works with gsed(1).An elegant solution. For in-place editing, the processing of the "I" (or "i") option is problematic with gsed:Leverage this:
$ gsed -i "" -e '1H;1g' i.txt
gsed: can't read : No such file or directory
$ echo $?
2
If you use > , you will delete original contents of the file.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.
Maybe the filenames I used were too long to see what I've done:If you use > , you will delete original contents of the file.