LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-30-2009, 03:58 AM   #1
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Rep: Reputation: 0
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.
 
Old 04-30-2009, 04:04 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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!
 
Old 04-30-2009, 09:02 AM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,541
Blog Entries: 23

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
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!
 
Old 05-01-2009, 11:52 AM   #4
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Original Poster
Rep: Reputation: 0
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
 
Old 05-01-2009, 11:57 AM   #5
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 05-01-2009, 01:07 PM   #6
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Original Poster
Rep: Reputation: 0
appreciate that you can give one example as i don't really understand the tutorial mean.
Thank you in advance.
 
Old 05-01-2009, 01:23 PM   #7
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 05-01-2009, 01:25 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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"
 
Old 05-01-2009, 01:32 PM   #9
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Original Poster
Rep: Reputation: 0
To all,

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


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 06:21 PM
How to add a word at the front of a file? fsloke Programming 3 12-22-2008 09:02 PM
Problems Copying & Pasting In Word When Word Closes - Ubuntu davidx Linux - Software 3 10-22-2008 09:21 PM
LXer: Benchmarking Microsoft Word 95 through Word 2007 LXer Syndicated Linux News 0 07-24-2008 02:00 AM
putting word in front of each line in a file stryka Slackware 3 12-06-2005 03:32 PM


All times are GMT -5. The time now is 06:27 PM.

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