LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-08-2013, 05:26 AM   #1
anandg111
Member
 
Registered: Jan 2012
Posts: 56

Rep: Reputation: Disabled
Sed - the repeated pattern (&)


can somebody please explain the repeated pattern (&) in sed with propper example.
 
Old 08-08-2013, 05:47 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by anandg111 View Post
can somebody please explain the repeated pattern (&) in sed with propper example.
The & has nothing to do with repeated patterns.
Quote:
& Replaced by the string matched by the regular expression.
If you use something like this:
Code:
sed 's/foo/&bar/' infile
The & is replaced with the regular expression (foo in this case) in the replace part.
Code:
$ cat infile
foo bar

$ sed 's/foo/&bar/' infile 
foobar bar
The above can be rewritten as:
Code:
sed 's/foo/foobar/' infile
Here's another example using a regular expression:
Code:
$ cat infile
foo bar

$ sed 's/.*/& foobar/' infile 
foo bar foobar
The above can also be written as (there are probably more ways):
Code:
sed 's/foo bar/foo bar foobar/' infile

or

sed 's/$/ foobar/' infile
 
3 members found this post helpful.
Old 08-08-2013, 05:51 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
& 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)/'
My birth year is (1968).
However if you don't know what the number is, you cannot put it in the replacement string. In this case the & pattern is your friend:
Code:
$ echo "My birth year is 1974." | sed 's/[0-9]\+/(&)/'
My birth year is (1974).
$ echo "My birth year is 1935." | sed 's/[0-9]\+/(&)/'
My birth year is (1935).
Basically it is the whole string matched by the regular expression, that you need to repeat in the replacement string without knowing what is it.

Edit: too late!
 
2 members found this post helpful.
  


Reply

Tags
sed bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [SOLVED] sed | how to modify line with repeated word rnoçmoi Programming 1 02-20-2012 10:01 AM
[SOLVED] Pattern matching with sed vinaytp Linux - Newbie 3 05-24-2010 07:33 AM
Sed pattern replacement icyrail Programming 3 10-27-2009 05:33 AM
last pattern with sed? xpto09 Linux - Newbie 6 10-04-2007 08:01 PM
conting repeated substrings:SED perl anirudh Programming 1 10-29-2004 01:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration