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 03-04-2015, 06:37 AM   #1
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Rep: Reputation: Disabled
awk or sed help


Hi All,
How can I replace the particular word using sed or awk
Code:
$ BUGZILLAURL="https://mylocalserver.com/bugzilla"
$ PROJECTNAME="mybugs"
$ echo "$BUGZILLAURL/$PROJECTNAME" 
https://mylocalserver.com/bugzilla/mybugs
There is a urlbase line in data/params file which has an empty variable, pls see 2nd line from below command's output.
Code:
$ grep -i "urlbase" data/params 
           'docs_urlbase' => 'docs/%lang%/html/',
           'urlbase' => '',
           'webdotbase' => 'http://www.research.att.com/~north/cgi-bin/webdot.cgi/%urlbase%',
Or search only for that line which I wanted to be as 'urlbase' => 'https://mylocalserver.com/bugzilla/mybugs',
Code:
$ grep -i "'urlbase'" data/params 
           'urlbase' => '',
So expecting your kind help to replace '' word with 'https://mylocalserver.com/bugzilla/mybugs' using sed or awk.

Thanks.
 
Old 03-04-2015, 06:57 AM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Set the variables you need then run:

Code:
sed -i -e "/^'urlbase'/s@''@'$BUGZILLAURL/$PROJECTNAME'@g" data/params
 
Old 03-04-2015, 07:00 AM   #3
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
You should read learn and build up self capability to use sed.

Read info pages of sed. That is the official documentation about sed. It has also examples.

Run:

Code:
info sed
 
Old 03-04-2015, 08:11 AM   #4
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by veerain View Post
Set the variables you need then run:

Code:
sed -i -e "/^'urlbase'/s@''@'$BUGZILLAURL/$PROJECTNAME'@g" data/params
Thanks for the reply Sir,
I ran the above command after setting the variables but it did not append the entry(https://mylocalserver.com/bugzilla/mybugs) to urlbase variable line in the data/params file.
Code:
# echo "$BUGZILLAURL/$PROJECTNAME" 
https://mylocalserver.com/bugzilla/mybugs
# sed -i -e "/^'urlbase'/s@''@'$BUGZILLAURL/$PROJECTNAME'@g" data/params
# echo $?
0
# grep -i "'urlbase'" data/params
           'urlbase' => '',
#
Am I missing anything? Kindly suggest and expecting your kind reply.
 
Old 03-04-2015, 10:55 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
^ implies the string being searched for ('urlbase') is at the start of the line, is this the case?

Also, you have been given an example, try and work with it as opposed to just trying the given solution verbatim and then saying it doesn't work.
 
Old 03-04-2015, 10:55 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
^ implies the string being searched for ('urlbase') is at the start of the line, is this the case?

Also, you have been given an example, try and work with it as opposed to just trying the given solution verbatim and then saying it doesn't work.
 
Old 03-05-2015, 12:32 AM   #7
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Try this instead:

Code:
sed -i -e "/'urlbase'/s@''@'$BUGZILLAURL/$PROJECTNAME'@g" data/params
or this:

Code:
sed -i -e "/^[[:space:]]\+'urlbase'/s@''@'$BUGZILLAURL/$PROJECTNAME'@g" data/params
or this:

Code:
sed -i -e "/'urlbase'/s@''@'$BUGZILLAURL/$PROJECTNAME'@g" data/params
And refer to info pages of sed to modify appropriately.
 
1 members found this post helpful.
Old 03-05-2015, 01:43 AM   #8
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by veerain View Post
Try this instead:

Code:
sed -i -e "/'urlbase'/s@''@'$BUGZILLAURL/$PROJECTNAME'@g" data/params
or this:

Code:
sed -i -e "/^[[:space:]]\+'urlbase'/s@''@'$BUGZILLAURL/$PROJECTNAME'@g" data/params
or this:

Code:
sed -i -e "/'urlbase'/s@''@'$BUGZILLAURL/$PROJECTNAME'@g" data/params
And refer to info pages of sed to modify appropriately.
Excellent SED master, all the above 3 commands worked nicely. Thanks a lot for this great help.
 
  


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
[SOLVED] 'sed' or 'awk' help upendra_35 Linux - Newbie 9 10-23-2012 04:10 AM
sed/awk help Eppo Programming 19 04-09-2011 03:50 AM
sed or awk help sharky Programming 13 03-02-2010 06:17 PM
awk and/or sed linux2man Linux - General 7 01-22-2007 11:02 AM
Sed and Awk Gins Programming 7 04-19-2006 11:32 AM


All times are GMT -5. The time now is 06:42 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