1

I want to delete in a file each line beginning by '#'. I ran that (I am using osx)

 sed -i '' -e 's/#.*/d' file

but I get this error message :

sed: 1: "s/#.*/d
": unescaped newline inside substitute pattern
5
  • 1
    You need sed -i '/^#/d' file. Commented Oct 29, 2013 at 10:08
  • @gniourf_gniourf: sed -i '/^#/d' filesed: 1: "file": invalid command code f Commented Oct 29, 2013 at 10:28
  • @GarethRees you probably don't have GNU sed that handles the -i option... Commented Oct 29, 2013 at 10:30
  • @GarethRees Funny that my GNU sed is happy with the command I gave... are you sure you didn't mistype it? Besides, that command is exactly the one you gave in your answer :D. Commented Oct 29, 2013 at 10:36
  • The OP is on OS X, where the sed is BSD-ish, and that means that the -i option requires an argument. Commented Oct 29, 2013 at 10:43

2 Answers 2

1

The s command in sed means "substitute" and it takes two arguments:

s/pattern/replacement/

What you want to do is just to match lines starting with # and delete them, so you need the sed program:

/^#/d

Note that the pattern needs to start with ^ (meaning "start of line") otherwise it will match a # anywhere in the line.

Sign up to request clarification or add additional context in comments.

Comments

1

As stated by Gareth Rees above, the correct command is:

sed '/^#/ d' file

This good sed tutorial contains your question as an example:

http://www.grymoire.com/Unix/Sed.html#toc-uh-30

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.