Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a solution for this in awk:

awk '{if (NR==1 && NF==0) next};1' somefile

but was unable to find one that worked in sed. E.g.,

sed -i.bak '/^$/{1,1d;}' somefile

ended up deleting the first blank line it found (I'm not a sed expert, so I was just trying out things). The advantage to sed in this case is that I would be able to do this w/o temporary files. ie in place.

share|improve this question
up vote 10 down vote accepted

Try this one:

sed '1{/^$/d}' file
share|improve this answer
It can't delete the first line if it isn't blank. Please check how it works and write if it is ok. – rush Jun 27 '12 at 14:39
Works great! .. Thanks a bunch – Levon Jun 27 '12 at 16:14
I understand the /^$/d part, by putting the line number in front and enclosing it in {} makes it only apply to that line? I assume I could also give a range of line numbers in the form 2,4 (start at line 2 and end at 4)? – Levon Jun 27 '12 at 16:23
Yes, you're right. – rush Jun 27 '12 at 16:25

An alternative:

sed '0,/^$/d' file

And as a bold guessing, you may want to delete all the blank lines before the first line(containing any chars), if it is the case, we can try the below:

sed '1,/^$/d' file

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.