If you don't need to do this in place, you can use awk
and redirect the output to another file.
awk '{if (NR==1 && NF==0) next};1' somefile
This will print the contents of the file except the first line ( NR == 1
) and does not contain any data ( NF == 0
).
NR
number of the current line, NF
number of fields on this line, separated by spaces / tabs
eg.
$ cat -n data.txt 1 2 this is some text 3 and here 4 too 5 6 blank above 7 the end $ awk '{if (NR==1 && NF==0) next};1' data.txt | cat -n 1 this is some text 2 and here 3 too 4 5 blank above 6 the end
and
cat -n data2.txt 1 this is some text 2 and here 3 too 4 5 blank above 6 the end $ awk '{if (NR==1 && NF==0) next};1' data2.txt | cat -n 1 this is some text 2 and here 3 too 4 5 blank above 6 the end
Update:
This sed
solution should also work for an in-place replacement:
sed -i.bak '1{/^$/d}' somefile
The original file will be saved with the extension .bak
Levon source share