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?

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

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.

| improve this answer | |
  • What about this command makes it "more correct"? – lowtex Jan 6 '17 at 0:57
  • It works? At least for me ;-) – ojs Jan 6 '17 at 0:59
  • 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 '17 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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