LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-14-2012, 02:29 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,032

Rep: Reputation: 275Reputation: 275Reputation: 275
sed - prefixing a character string identified by a RegEx


I want to insert a fixed character string immediately before a target character string wherever it occurs. The tricky part is that the target string is not a constant, it is recognized by conforming to a Regular Expression.

This is a contrived example intended to illustrate the question. The actual application has nothing to do with telephone numbers. Some lines in the input file will have no phone numbers.

Insert the character string "Phone:" immediately before any string which looks like a telephone number.

Sample input file ...
Quote:
*------------------
Brickmasons: Herman 914-555-1234
Irving 845-555-2233 James 212-555-4455
*------------------
Carpenters: Larry 518-555-5678 Nathan 718-555-2244
*------------------
Desired output file ...
Quote:
*------------------
Brickmasons: Herman Phone:914-555-1234
Irving Phone:845-555-2233 James Phone:212-555-4455
*------------------
Carpenters: Larry Phone:518-555-5678 Nathan Phone:718-555-2244
*------------------
Daniel B. Martin
 
Old 01-14-2012, 03:43 PM   #2
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
You can do it with a substitution command using backreferences. As an example, suppose I want to prepend the word GNU/ to every occurrence of the word Linux.

Here's a sample text:

Code:
I use Linux.
Other people use Windows.
Linux is fine for me.
Here is the command to do the job:

Code:
sed 's|\(Linux\)|GNU/\1|' sample.txt
Here is the output:

Code:
I use GNU/Linux.
Other people use Windows.
GNU/Linux is fine for me.
HTH
 
Old 01-14-2012, 04:32 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,410
Blog Entries: 1

Rep: Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951
or you could use the & character which represents the matched pattern:

Code:
 sed -r 's/[0-9]{3}-[0-9]{3}-[0-9]{4}/Phone:&/g' file
 
1 members found this post helpful.
Old 01-14-2012, 04:53 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by sycamorex View Post
Code:
 sed -r 's/[0-9]{3}-[0-9]{3}-[0-9]{4}/Phone:&/g' file
Which is a great solution, with the caveat that:

Quote:
Originally Posted by info sed
Extended regexps are those that `egrep' accepts;
they can be clearer because they usually have less backslashes,
but are a GNU extension and hence scripts that use them are not
portable
.
 
Old 01-14-2012, 05:07 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,410
Blog Entries: 1

Rep: Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951
Quote:
Originally Posted by Telengard View Post
Which is a great solution, with the caveat that:
Fair enough, one could do it in a less concise way:
Code:
sed 's/[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]/Phone:&/g' file
However, as the OP mentioned, phone numbers are just an example so without more specific sample data
the only thing we can do is to refer him/her to sed tutorials (see: backreferences and &)
 
Old 01-14-2012, 06:50 PM   #6
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by sycamorex View Post
sed tutorials
Good idea
 
1 members found this post helpful.
Old 01-14-2012, 08:39 PM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,410
Blog Entries: 1

Rep: Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951
I think you've missed one. Actually, the best one
http://www.grymoire.com/Unix/Sed.html
 
1 members found this post helpful.
Old 01-14-2012, 09:40 PM   #8
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by sycamorex View Post
I think you've missed one.
Fair enough
 
Old 01-15-2012, 11:17 AM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,032

Original Poster
Rep: Reputation: 275Reputation: 275Reputation: 275
Thanks to all who responded with helpful suggestions. I will follow through. Let's call this one SOLVED!

Daniel B. Martin
 
  


Reply

Tags
regex, regular expression, sed, substitution


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed regex get multiple string match in array? ted_chou12 Programming 3 04-09-2011 03:16 AM
[SOLVED] SED assistance: add character in the middle of a string. Vryali Programming 4 10-21-2010 10:51 AM
Sed and regex: how to replace up to a certain string olliesa Linux - General 2 10-21-2009 02:49 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Using sed to convert a string to a character? whansard Linux - General 2 01-10-2003 05:13 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration