I am trying to use the sed command to replace some text in file.

echo 'native_transport_port: 9042' | sed -E 's/native_transport_port:/\W9042/native_transport_port:9080'

I get an error: sed: 1: "s/native_transport_port ...": bad flag in substitute command: 'n'

I tried echo 'native_transport_port: 9042' | sed -E 's/native_transport_port:\s9042/native_transport_port:\s9080' But get an error: sed: 1: "s/native_transport_port ...": unterminated substitute in regular expression

I am basically trying to search for native_transport_port and replace 9042 with 9080.

1 Answer 1


You need to finish the s command with a final /:

sed -E 's/native_transport_port:\s9042/native_transport_port:\s9080/'

While I'm at it, \s doesn't mean anything special in the replacement section, it becomes s; so you should do

sed -E 's/native_transport_port:\s9042/native_transport_port: 9080/'

or, if you want to reproduce the whitespace as-is,

sed -E 's/(native_transport_port:\s)9042/\19080/'

which re-uses the matched text in the replacement.

If your sed doesn't support Perl-style \s, you can match on space instead:

sed 's/native_transport_port: 9042/native_transport_port: 9080/'
  • I tried echo 'native_transport_port: 9042' | sed -E 's/native_transport_port:\s9042/native_transport_port: 9080/' But It still shows with port 9042 instead of 9080 Feb 14, 2017 at 16:35
  • That's weird, echo 'native_transport_port: 9042' | sed -E 's/native_transport_port:\s9042/native_transport_port: 9080/' gives the expected result on my system. Feb 14, 2017 at 16:40
  • Interesting. I tried on a linux system and it works. On my mac it doesn't! Feb 14, 2017 at 16:41
  • That is interesting, is the target macOS? Feb 14, 2017 at 16:42
  • 1
    Do you need to match any whitespace or can you use sed 's/native_transport_port: 9042/native_transport_port: 9080/'? Feb 14, 2017 at 16:49

You must log in to answer this question.

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