Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Problem changing the line in a file


    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/" />

    My script will ask the user to select the value for 'servicePrincipal' and modify the file. My script is as below:
    echo "Enter Service Principal: "
    oldspn='<property name="servicePrincipal" value="HTTP/" />'
    read newspn
    str1='property name='
    blank=' '
    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:
    sed: -e expression #1, char 70: unknown option to `s'
    Any help would be greatly appreciated.

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

  2. #2
    Linux Enthusiast
    Join Date
    Dec 2011
    Turtle Island West
    Just a guess:

    oldspn='<property name="servicePrincipal" value="HTTP/" />'

    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
    hello and welcome, nnookala!

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

    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. $spacer_open
  5. #4
    Thank you very much. It worked.

  6. #5
    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