0

sed -i -e "/"project1/baz"/ s/foo/bar/" test.txt

For example, i need to change word "foo" to word "bar" only if line contains "project1/baz", but as "project1/baz" contains "/" it throws an error: sed: can't find label for jump to `az/' what if instead of "project1/baz" sed input contained variable?

MwahMallah
  • 33
  • 3

1 Answers1

0

You need to escape the / to prevent it from terminating the address.

sed -i -e '/project\/baz/s/foo/bar' test.txt

Consider using the standard ed command instead of the non-standard -i option to sed.

ed test.txt <<EOF
/project\/baz/s/foo/bar
wq
EOF

or

printf '%s\n' '/project\/baz/s/foo/bar/' 'wq' | ed test.txt
chepner
  • 497,756
  • 71
  • 530
  • 681