LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk or sed help (https://www.linuxquestions.org/questions/linux-newbie-8/awk-or-sed-help-4175535725/)

cli 03-04-2015 05:37 AM

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.

veerain 03-04-2015 05:57 AM

Set the variables you need then run:

Code:

sed -i -e "/^'urlbase'/s@''@'$BUGZILLAURL/$PROJECTNAME'@g" data/params

veerain 03-04-2015 06:00 AM

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

cli 03-04-2015 07:11 AM

Quote:

Originally Posted by veerain (Post 5326698)
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.

grail 03-04-2015 09:55 AM

^ 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.

grail 03-04-2015 09:55 AM

^ 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.

veerain 03-04-2015 11:32 PM

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.

cli 03-05-2015 12:43 AM

Quote:

Originally Posted by veerain (Post 5327120)
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.


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