Thirst replacement with variables

1

I would need to replace a variable in a file and even though I tried it with sed I did not reach the goal.

I set the stage.

I have a config.xml file like the following:

   <widget id="com.example.hello" version="0.0.1">
    <name>HelloWorld</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="[email protected]" href="http://cordova.io">
        Apache Cordova Team
    </author>
     <enter>PASSWORD</enter>
    <content src="index.html" />
    <access origin="*" />
</widget>

I would need to run a shell script that would take the value from the same command and replace it, example.

$./script.sh config.xml NUEVOPASSWORD

And so get:

<widget id="com.example.hello" version="0.0.1">
<name>HelloWorld</name>
<description>
    A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="[email protected]" href="http://cordova.io">
    Apache Cordova Team
</author>
 <enter>NUEVOPASSWORD</enter>
<content src="index.html" />
<access origin="*" />

That is, modify the value in the <enter> tag with the given parameter.

The value is variable so I will not know it when I'm going to run the script.

    
asked by Jose Torres 11.11.2016 в 12:17
source

4 answers

1

To modify XML files it is better to use an ad-hoc tool, because parsing them can give problems .

A useful tool is xmlstarlet , with which you can say things like:

xmlstarlet ed -u "//enter" -v 'NUEVAPASSWORD' archivo

Here, keep the file as you are modifying the content of the <enter> tag:

<enter>NUEVAPASSWORD</enter>

By default xmlstarlet displays the result of the modifications on the screen. If you want to modify the original file, add the -L option:

xmlstarlet ed -L -u "//enter" -v 'NUEVAPASSWORD' fichero
    
answered by 11.11.2016 в 15:12
1

The following script the bash modifies the password:

#!/usr/bin/env bash
#Modifica un texto de la forma <$1>texto</$1>
#Dejándolo con la forma <$1>$2</$1>
#El tercer parámetro es el archivo a modificar.
#Por ejemplo, si se invoca este script con esta línea de comandos :
#modifica.sh enter NEWPASSWORD config.xml
#contra un archivo que contiene la línea : <enter>PASSWORD</enter>
#esta línea será cambiada a :    <enter>NEWPASSWORD</enter>
sed -i.BAK '[email protected]\(<'"$1"'>\).\+\(</'"$1"'>\)@'"$2"'@' "$3"

How does this sed command work?
The '' are to prevent bash from interpreting the content and passing the text as thirsty.
The "" are for the parameters $1 and $2 with which the script should be invoked. $1 is the tag (enter in the example) and $2 is the new password. I use "" because I do want bash to interpret them and to prevent a blank space from splitting the string.

s : Command search and replace.% @ : Separator of the parameters of the s command, it is usual to use / , but the text to be processed contains / with what it would not work, and thirst allows any character to be used as a separator for the s command. Note that if the new password contains @ this will not work. You must use a character that does not appear. What follows this is the regular expression of the text to be searched.
\( : Start of capture group. Everything that goes from here to) will be stored in \ 1.% <$1> : Parse the indicated tag.
\) : End of capture group.% .+ : 1 or more characters . If passwords of size 0 are acceptable use. *% \( : Start of capture group \ 2
</$1> : Parse the closing of the indicated tag.% \) : End of group of capture.
@ : Separator of command parameters s. What follows this is the text that will replace the one that matches the previous regular expression.
: The capture group 1. This writes the opening of the tag.
$2 : The new password.
: The capture group 2. This writes the closing of the tag.% @ : End of the command s.

    
answered by 04.01.2017 в 16:18
0

To replace strings, use sed -i 's/ORIGINAL/NUEVA/' archivo In the specific case you show, it would be:

sed -i 's/>PASSWORD</>NUEVAPASSWORD</' config.xml

As you will see, I have included the characters > and < , to ensure the match; would be part of the words to search / replace, not the command.

    
answered by 11.11.2016 в 14:57
0

I have the solution and put it in case someone is in a similar situation.

$ cat script.sh
#!/bin/sh
file=$1
sed -i "s/^\( *<enter>\)\([^>]*\)</$2</" "$1"

And the execution of the complete command would be such that:

$./script.sh config.xml NEWPASSWORD

In my case it works perfectly.

    
answered by 11.11.2016 в 15:46