LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sed insert before character (https://www.linuxquestions.org/questions/linux-newbie-8/sed-insert-before-character-934282/)

VladC 03-13-2012 03:45 PM

Sed insert before character
 
I have the following homework. I need to enter a certain word, let s call it "exemple" , before every lowercase character in a document. I tryed

sed '/[a-z]/i \exemple' text.txt


OUTPUT:

1: exemple
2: same text no changed

Can you please help me figure this out?

David the H. 03-13-2012 03:55 PM

The "i" command is for inserting whole lines above the one you matched (and "a" appends them after it). You need to use the "s" command to edit the line itself.

In this case you need to match the character or string you want on the left side of the substitution, and save it in a regex backreference. Then you can insert it into the right-hand-side replacement text. This means you'll probably want to use the -r program option. Don't forget the "g" substitution modifier too if there can be multiple matches on the line (s///g).

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt


PS: Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

sycamorex 03-13-2012 04:01 PM

An easy way of doing it would be using the special character "&".

VladC 03-15-2012 12:44 AM

Thank you guys. Solved


All times are GMT -5. The time now is 07:33 PM.