LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   want to add a word in front if found another word (https://www.linuxquestions.org/questions/linux-newbie-8/want-to-add-a-word-in-front-if-found-another-word-722644/)

coolplanet 04-30-2009 02:58 AM

want to add a word in front if found another word
 
hi,

I want to add a word in front if found the word i want :

eg RewriteCond
if found the RewriteCond then add ###TEST###

I still want to keep the RewriteCond word

###TEST###RewriteCond

how to do this using SED ? Please help thank you.

colucix 04-30-2009 03:04 AM

Just substitute "RewriteCond" with "###TEST###RewriteCond":
Code:

sed 's/RewriteCond/###TEST###RewriteCond/g' file
Take a look at this very good sed tutorial if you want to learn sed secrets! ;)

onebuck 04-30-2009 08:02 AM

Hi,

Welcome to LQ!

Sounds like homework to me. Please read the LQ Rules.

'Rute Tutorial & Exposition'
'Linux Documentation Project'
'LinuxSelfHelp'

The above links are just a few good references that can be used by a newbie to enhance their endeavor with GNU/Linux.

These links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!

coolplanet 05-01-2009 10:52 AM

Hi Sir,

Sorry i think i ask wrongly, I rephase
my question :

If i want to insert ####TEST#### below the ####HAPPY#### after found the word ####HAPPY#### is it possible?

eg

####HAPPY####
####TEST####RewriteCond
####TEST####RewriteRule
####TEST####Rewrite
####TEST####Rewrite

Sorry to disturb you all :)

forrestt 05-01-2009 10:57 AM

OK, if you had read the tutorial presented by colucix, you would have found your answer. Perhaps you can read just the pertinent part.

HTH

Forrest

coolplanet 05-01-2009 12:07 PM

appreciate that you can give one example as i don't really understand the tutorial mean.
Thank you in advance.

forrestt 05-01-2009 12:23 PM

From the tutorial:

Quote:

This example will remove comments everywhere except the lines between the two keywords:

sed -e '1,/start/ s/#.*//' -e '/stop/,$ s/#.*//'
What this is doing is using the -e parameter to specify that the pieces between single quotes are to be used as a SED expression. The first expression means:

Starting with the first line (specified by the 1) and continuing until "the pattern 'start' is found" (specified by the /start/) perform the substitution of replacing "#" followed by "any number of" (specifed by the *) "any character" (specified by the ".") with nothing (specified by the emptyness between the second / and the last / in the substitution command).

I'll leave decoding the second expression as an exercise for you.

For your homework, you may also need to know that '^' matches the beginning of a line.

Forrest

pixellany 05-01-2009 12:25 PM

cool;

You need to tell us WHAT you don't understand!!!

forrestt has given you a link to the addressing feature of SED. The general construct is to first specify an "address range" and then operate within that range.

Example:
sed '/brown/,/car/s/dog/cat/' filename
Translation:
Start with the first line containing "brown", and end with the first line containing "car". In this range, replace the first occurrence on each line of "dog" with "cat"

coolplanet 05-01-2009 12:32 PM

To all,

Thank you for the explaination, i will try to figure out and apply to the script.
Thank you :)


All times are GMT -5. The time now is 01:39 PM.