LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 05: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, 05: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, 06: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, 07: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, 09:55 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
^ 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, 09:55 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
^ 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, 11:32 PM   #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, 12: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



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 03:10 AM
sed/awk help Eppo Programming 19 04-09-2011 02:50 AM
sed or awk help sharky Programming 13 03-02-2010 05:17 PM
awk and/or sed linux2man Linux - General 7 01-22-2007 10:02 AM
Sed and Awk Gins Programming 7 04-19-2006 10:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:53 AM.

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
Open Source Consulting | Domain Registration