Sed - the repeated pattern (&)
can somebody please explain the repeated pattern (&) in sed with propper example.
|
Quote:
Quote:
Code:
sed 's/foo/&bar/' infile Code:
$ cat infile Code:
sed 's/foo/foobar/' infile Code:
$ cat infile Code:
sed 's/foo bar/foo bar foobar/' infile |
& is substituted by the matched string. It's useful when you want to preserve the string matched by a regular expression, but you don't know what exactly is a priori. Suppose you want to embed a number in a string within parentheses. If you know what number is it you should do simply:
Code:
$ echo "My birth year is 1968." | sed 's/1968/(1968)/' Code:
$ echo "My birth year is 1974." | sed 's/[0-9]\+/(&)/' Edit: too late! ;) |
All times are GMT -5. The time now is 12:15 AM. |