Craig
Craig

Reputation: 1985

Sed replace specific substring

I have a file that's generated as an output to an SQL query. I need to replace the nulls in the file with blanks, so something like sed -e"s/null//g" would work.

However there's a valid string of the form 'null/' (with a trailing forward slash) and that should not be replaced. Is there a way to replace only 'null' values while leaving 'null/' intact?

Upvotes: 0

Views: 199

Answers (2)

Kent
Kent

Reputation: 195029

The sed one-liner:

sed 's#null\([^/]\|$\)#\1#g' file

should work for your requirement.

It searches pattern: null and followed by a non-slash char (or EOL),

replace with the followed non-slash char.

Thus, null/ won't be touched.

Upvotes: 2

bracco23
bracco23

Reputation: 2211

I think this command should be enough:

sed -e "s/null[^/]//g"

Upvotes: 0

Related Questions