Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, I am trying to write a script that changes the value in an xml file with the user input. The xml file I want to modify has the following ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2013
    Posts
    2

    Problem changing the line in a file


    Hi,

    I am trying to write a script that changes the value in an xml file with the user input. The xml file I want to modify has the following line:
    <property name="servicePrincipal" value="HTTP/postgres.mydomain.com" />

    My script will ask the user to select the value for 'servicePrincipal' and modify the file. My script is as below:
    Code:
    #!/bin/sh
    echo "Enter Service Principal: "
    oldspn='<property name="servicePrincipal" value="HTTP/postgres.mydomain.com" />'
    filename="/home/nnookala/OWFsecurityContext.xml"
    read newspn
    lt='<'
    str1='property name='
    dblquot='"'
    blank=' '
    str2=servicePrincipal
    str3='value='
    str4=' />'
    completestring=$lt$str1$dblquot$str2$dblquot$blank$str3$dblquot$newspn$dblquot$str4
    echo "completestring: " $completestring
    
    #sed '42s/.*/$completestring/' $filename > newfile
    sed "42s/$oldspn/$completestring/" $filename > newfile
    I am trying to change the text of line 42. When I use single quotes in the command it is replacing the entire line with '$completestring'. Not getting the value of the variable. When I use double quotes in the command I am getting the following error:
    Code:
    sed: -e expression #1, char 70: unknown option to `s'
    Any help would be greatly appreciated.

    Thanks,
    Nookala.
    Last edited by atreyu; 08-23-2013 at 02:28 PM. Reason: added CODE tags to aid in readability

  2. #2
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    362
    Just a guess:

    oldspn='<property name="servicePrincipal" value="HTTP/postgres.mydomain.com" />'

    has a slash in it that you are using as the delimiter with sed. Try using '%' as the delimiter. eg:

    sed "42s%$oldspn%$completestring%" $filename > newfile

    and see if that works.

  3. #3
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hello and welcome, nnookala!

    try escaping the slash, e.g. "\/". and try this shorter sed command:

    Code:
    sed -i.bak '42s/^\(.*value=\).*$/\1"newval" \/>/'
    it makes a backup up of the original sed input file, and uses the \1 variable to match most of the line for you, so you just provide the new value for the value XML attribute (don't forget the double-quotes).

  4. #4
    Just Joined!
    Join Date
    Aug 2013
    Posts
    2
    Thank you very much. It worked.

  5. #5
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    excellent, marking as Solved for you. note that you can do this yourself using the Thread Tools link at the top of the page.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •