Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace only last match in a line with sed?

With sed, I can replace the first match in a line using

sed 's/pattern/replacement/'

And all matches using

sed 's/pattern/replacement/g'

How do I replace only the last match, regardless of how many matches there are before it?

like image 895
Jan Warchoł Avatar asked Oct 27 '17 08:10

Jan Warchoł


People also ask

How do you put something at the end of a line using sed?

Using a text editor, check for ^M (control-M, or carriage return) at the end of each line. You will need to remove them first, then append the additional text at the end of the line.

How do I change the last occurrence of a string in Unix?

`sed` command can be used to replace any part of a string or a line of the file in different ways by using regular expression patterns. This tutorial showed the ways to replace the last occurrence of the searching text in a string or a file by using multiple `sed` commands.

How do you use sed to match word and perform find and replace?

Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.

How do you use sed to replace the first occurrence in a file?

With GNU sed's -z option you could process the whole file as if it was only one line. That way a s/…/…/ would only replace the first match in the whole file. Remember: s/…/…/ only replaces the first match in each line, but with the -z option sed treats the whole file as a single line.


Video Answer


1 Answers

Copy pasting from something I've posted elsewhere:

$ # replacing last occurrence
$ # can also use sed -E 's/:([^:]*)$/-\1/'
$ echo 'foo:123:bar:baz' | sed -E 's/(.*):/\1-/'
foo:123:bar-baz
$ echo '456:foo:123:bar:789:baz' | sed -E 's/(.*):/\1-/'
456:foo:123:bar:789-baz
$ echo 'foo and bar and baz land good' | sed -E 's/(.*)and/\1XYZ/'
foo and bar and baz lXYZ good
$ # use word boundaries as necessary - GNU sed
$ echo 'foo and bar and baz land good' | sed -E 's/(.*)\band\b/\1XYZ/'
foo and bar XYZ baz land good

$ # replacing last but one
$ echo 'foo:123:bar:baz' | sed -E 's/(.*):(.*:)/\1-\2/'
foo:123-bar:baz
$ echo '456:foo:123:bar:789:baz' | sed -E 's/(.*):(.*:)/\1-\2/'
456:foo:123:bar-789:baz

$ # replacing last but two
$ echo '456:foo:123:bar:789:baz' | sed -E 's/(.*):((.*:){2})/\1-\2/'
456:foo:123-bar:789:baz
$ # replacing last but three
$ echo '456:foo:123:bar:789:baz' | sed -E 's/(.*):((.*:){3})/\1-\2/'
456:foo-123:bar:789:baz

Further Reading:

  • Buggy behavior if word boundaries is used inside a group with quanitifiers - for example: echo 'it line with it here sit too' | sed -E 's/with(.*\bit\b){2}/XYZ/' fails
  • Greedy vs. Reluctant vs. Possessive Quantifiers
  • Reference - What does this regex mean?
  • sed manual: Back-references and Subexpressions
like image 180
Sundeep Avatar answered Sep 19 '22 01:09

Sundeep