LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed - prefixing a character string identified by a RegEx (https://www.linuxquestions.org/questions/programming-9/sed-prefixing-a-character-string-identified-by-a-regex-923792/)

danielbmartin 01-14-2012 02:29 PM

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

Telengard 01-14-2012 03:43 PM

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

sycamorex 01-14-2012 04:32 PM

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

Telengard 01-14-2012 04:53 PM

Quote:

Originally Posted by sycamorex (Post 4574624)
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
.


sycamorex 01-14-2012 05:07 PM

Quote:

Originally Posted by Telengard (Post 4574634)
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 &)

Telengard 01-14-2012 06:50 PM

Quote:

Originally Posted by sycamorex (Post 4574639)
sed tutorials

Good idea :)

sycamorex 01-14-2012 08:39 PM

Quote:

Originally Posted by Telengard (Post 4574691)

I think you've missed one. Actually, the best one:)
http://www.grymoire.com/Unix/Sed.html

Telengard 01-14-2012 09:40 PM

Quote:

Originally Posted by sycamorex (Post 4574742)
I think you've missed one.

Fair enough ;)

danielbmartin 01-15-2012 11:17 AM

Thanks to all who responded with helpful suggestions. I will follow through. Let's call this one SOLVED!

Daniel B. Martin


All times are GMT -5. The time now is 04:43 AM.