I want to remove the line that contains set -x in the file `"$(which tsc)", but I get an error:

$ sed -i ".bak" 'set -x' "$(which tsc)"
sed: -e expression #1, char 1: unknown command: `.'

I checked the solution here, but my line terminators are LF.


You have syntactic error. There can't be any space after -i, just the extension; this is the source of the (initial) error message.

Also, to remove a line based on a pattern you need /<pattern>/ d with sed (there are other approaches but this one is the cleanest). So do:

sed -i".bak" '/set -x/ d' "$(which tsc)"

Optionally, as the backup extension does not contain any whitespace or control characters, you could get away without quoting in this case:

sed -i.bak '/set -x/ d' "$(which tsc)"
  • You also have to remove the space after the -i. – Julie Pelletier Jan 27 '17 at 8:06
  • Old versions of sed do not accept whitespace in between the pattern and the d. – zwol Jan 27 '17 at 15:10

Try this command:

sed -i.bak '/pattern to match/d' ./infile

Also you can refer to Delete lines in a text file that containing a specific string (on Stack Overflow) for more methods to delete lines which contain set -x.

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.