[SOLVED] sed - prefixing a character string identified by a RegEx
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
*------------------
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.
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.
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 &)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.