I would like sed to remove empty lines from a file. I used this command:

cat file | sed '; ^$ ; d'

and was given the following error:

 sed: -e expression #1, char 3: unknown command: `^'

Why is this happening? Isn't sed supposed to interpret ^ as "the beginning of the line"? If so, why am I getting this error message?

  • 2
    You didn't give sed a command, like 's' for search & replace
    – Jeff Schaller
    Jan 6, 2017 at 0:45

1 Answer 1


The (more) correct way is

cat file | sed '/^$/d'

But I am sure people will have lots of answers to the way of deleting empty lines.

  • 1
    What about this command makes it "more correct"?
    – lowtex
    Jan 6, 2017 at 0:57
  • It works? At least for me ;-)
    – ojs
    Jan 6, 2017 at 0:59
  • 1
    When you use semicolons then you are telling sed that there are multiple commands to execute, so sed is expecting a seperate command, like d, inbetween each semicolon.
    – ojs
    Jan 6, 2017 at 1:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .