2

I have a config file, with new version code, like a SAD10A_BNA_1234_123456_110011, and this number is write to config.txt, and this number I must place in 4 places on this xml file.

This is my script:


#!/bin/bash

NewNumber=`cat config.txt`

echo $NewNumber
#This number is: PLE31Z_BNE_1111_1121211_313131

awk '/"Parameter1"/ && !done++{sub(/Parameter1="[A-Z0-9]"/, "Parameter1=\"'$NewNumber'\"")}1' OldFileWithVersionNumeber.xml > temp.xml && mv -f temp.xml Newfile$NewNumber.xml

#I know, I must write 3 awk, but first one doesn't work for now

cat targettext.xml | grep Parameter1

XML with old parameter:


<OneSection Parameter1="SAD10A_BNA_1234_123456_110011" Parameter2="SAD10A_BNA_1234_123456_110011" Type="UWE-AD" date="05/01/2011">

 AND LOT OF VERY SIMILAR LINES
 AND TWO LINES WITH THE SAME NUMBER TO REPLACE

<xmlElement Name="" name="NameGB" version="_SAD10A_BNA_1234_123456_110011.xml" Unit="ERF" Blocks="1" params1="" params2="" Path1="/rom/" path2="" comp="" encrypted="">
<xmlElement KeyName="" name="NameGB" version="_SAD10A_BNA_1234_123456_110011.xml" Unit="ERFS" Blocks="1" params1="" params2="" Path1="/rom/" path2="" comp="" encrypted="">

Dzions
  • 35
  • 6
  • 1
    It looks to me like the purpose of `!done++` was to restrict the match to the first line. Is that correct? – John1024 Feb 02 '15 at 08:06
  • Its better to avoid using old and outdated back tics, use parentheses: `NewNumber=$(cat config.txt)`. Also do not use `cat` with program that can read data itself: `grep Parameter1 targettext.xml` – Jotne Feb 02 '15 at 08:32
  • Do not use variable inside `awk` expression. Look here: http://stackoverflow.com/questions/19075671/how-to-use-shell-variables-in-awk-script – Jotne Feb 02 '15 at 08:34

1 Answers1

1

Try this as the awk command:

$ awk -v new="$NewNumber" '/Parameter1/ && NR==1{sub(/Parameter1="[[:alnum:]_]*/, "Parameter1=\""new)} 1' OldFileWithVersionNumeber.xml
<OneSection Parameter1="PLE31Z_BNE_1111_1121211_313131" Parameter2="SAD10A_BNA_1234_123456_110011" Type="UWE-AD" date="05/01/2011">

 AND LOT OF VERY SIMILAR LINES
 AND TWO LINES WITH THE SAME NUMBER TO REPLACE

<xmlElement Name="" name="NameGB" version="_SAD10A_BNA_1234_123456_110011.xml" Unit="ERF" Blocks="1" params1="" params2="" Path1="/rom/" path2="" comp="" encrypted="">
<xmlElement KeyName="" name="NameGB" version="_SAD10A_BNA_1234_123456_110011.xml" Unit="ERFS" Blocks="1" params1="" params2="" Path1="/rom/" path2="" comp="" encrypted="">

How it works

  • -v new="$NewNumber"

    This defines an awk variable called new that contains the value of NewNumber.

  • /Parameter1/ && NR==1

    This selects lines that (1) contain Parameter1 and (2) are the first line of the file (NR==1).

  • sub(/Parameter1="[[:alnum:]_]*/, "Parameter1=\""new)

    This does the substitution. Note three changes to the regex:

    • By using [:alnum:] in place of [A-Z0-9], the regex is now safe for Unicode fonts.

    • The underline character was added to the allowed characters.

    • A * is added after [[:alnum:]_] so that zero or more characters can be matched. Previously, it matched only one.

    Note also that the substitution text now uses the variable new. This avoids shell-quoting issues and also is safer if NewNumber were to contain awk-active characters.

Changing both parameter1 and parameter2

The following code changes both parameter1 and parameter2 if they occur on the second line of the input file:

$ awk --posix -v new="$NewNumber" '/Parameter1/ && NR==2{sub(/Parameter1="[[:alnum:]_]*/, "Parameter1=\""new); sub(/Parameter2="[[:alnum:]_]*/, "Parameter2=\""new)} 1' OldFileWithVersionNumeber.xml

<OneSection Parameter1="PLE31Z_BNE_1111_1121211_313131" Parameter2="PLE31Z_BNE_1111_1121211_313131" Type="UWE-AD" date="05/01/2011">

 AND LOT OF VERY SIMILAR LINES
 AND TWO LINES WITH THE SAME NUMBER TO REPLACE

<xmlElement Name="" name="NameGB" version="_SAD10A_BNA_1234_123456_110011.xml" Unit="ERF" Blocks="1" params1="" params2="" Path1="/rom/" path2="" comp="" encrypted="">
<xmlElement KeyName="" name="NameGB" version="_SAD10A_BNA_1234_123456_110011.xml" Unit="ERFS" Blocks="1" params1="" params2="" Path1="/rom/" path2="" comp="" encrypted="">
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Thanks for You fast response. unfortunately You awk doesn't work :( i put You awk to my script – Dzions Feb 02 '15 at 08:27
  • What does "doesn't work" mean? If you run it by itself, as shown in answer, do you get the same result that I show? Is that the result that you want? Alternatively, if the problem only happens when the `awk` line is part of the script, what exactly are the symptoms? – John1024 Feb 02 '15 at 08:36
  • If i add this line to script, this awk doesn't replace parameter1, parameter1 is the same, parameter2 too. – Dzions Feb 02 '15 at 08:40
  • I Found error, You put in awk NR==1, but it is my second line:) and change number to 2, and now it works :) How I can change parameter2 in this xml ? – Dzions Feb 02 '15 at 08:51
  • @Dzions Very good. I added a new version that changes both parameters if they occur on the second line of the file. – John1024 Feb 02 '15 at 08:58