-1

I have the following line in my file

<field name="cd_version" type="string" size="16" id="-1" sequence="1" defaultValue="14.8.21.1"/>

I am attempting to grab and update the version number 14.8.21.1

I.E.

bash-3.2$ grep cd_version market_rules_cd.reader.xml | awk -F '"'  '{print $12}'
14.8.21.1

Script is attempting to echo the current version and replace it with user input

# Finds the version number in the file and changes it
#!/bin/bash
# 2014

#
# Usage: ./versionupdate <file>
#
# Arguments:
# 1. File name to have the version number replaced
#
# Finds the version number in the file and changes it
# to the one specified at the prompt.

FNAME=$1
echo $OLD_NO
#Get the version of the
OLD_NO=$(grep "_version=" | awk -F '"' '{print $12}' $FNAME)
echo "What do you want to update release number to?"
REPLACEMENT="_version="$NEW_NO
echo $REPLACEMENT
sed -i ''s/$OLD_NO/$REPLACEMENT/g'' $FNAME

~ ~

It appears to be hanging here

OLD_NO=$(grep "_version=" | awk -F '"' '{print $12}' $FNAME)
Super_Py_Me
  • 169
  • 1
  • 4
  • 14
  • I asked because I gathered more information and my original question was rolled back. – Super_Py_Me Sep 18 '14 at 13:38
  • 2
    Yes, I just saw that. However you aren't asking the new question here. You are asking the same question since that is the original broken `grep`/`awk` combination. – Etan Reisner Sep 18 '14 at 13:38
  • @tvm He is using sed. He just isn't getting to that point because of the `grep`/`awk` problem he asked about in his other question that he has failed to fix here. – Etan Reisner Sep 18 '14 at 13:40
  • Ah. Anyways - i see XML. It can be done with sed, but i recommend tools such as xmlstarlet for proper xml manipulation. – tvm Sep 18 '14 at 13:45
  • thats correct. I dont get to the sed portion – Super_Py_Me Sep 18 '14 at 13:45
  • 1
    I think the first line should have the shebang, please move the comment underneath it. Then add a `set -x` after the shebang to enable debugging. Check what you get. Also, wrap your vars in double quotes, `FNAME="$1"` etc. – xificurC Sep 18 '14 at 13:45
  • If you don't get the sed part, ask for clarification in [your previous question](http://stackoverflow.com/questions/25913575/bash-using-grep-and-awk-inside-a-variable). –  Sep 18 '14 at 13:50
  • Isn't it just that grep is waiting for input on STDIN, since no other inpiut is specified? – Biffen Sep 18 '14 at 13:51

1 Answers1

0

A safer way to extract the version number would be to interpret the field tag itself, rather than simply split by quotes.

$ sed -nr 'H;${;x;s/.*<field[^>]+name="cd_version"[^>]+defaultValue="([0-9.]+)"[^>]*\/>.*/\1/;p;}' input.txt
14.8.21.1

You can put the version in a variable using notation you're already familiar with:

$ v=$(sed -nr 'H;${;x;s/.*<field[^>]+name="cd_version"[^>]+defaultValue="([0-9.]+)"[^>]*\/>.*/\1/;p;}' input.txt)
$ echo $v
14.8.21.1

This sed script perhaps bears some explanation...

  • H; - append the "current" line to sed's "hold space"
  • ${; - once we hit the last line, do the following...
    • x; - eXchange the hold space for the pattern space (so we can work on it),
    • s/.*<field...([0-9.]+)...\/>.*/\1/; - extract defaultValue from the correct field,
    • p;} - and print the result.

This should be safe for cases where multiple <field>s appear on one line (as long as there's only one cd_version), or where the attributes of a <field> are split over multiple lines.

Note that THIS IS A HACK, and you should probably be using tools which actually interpret fields like this correctly. It is widely thought that you cannot parse HTML with regex, and the same reasoning applies for other SGML.

Once you've got the version number, you can use sed -i to replace it.

$ newver="foobar"
$ grep -o 'defaultValue="[^"]*"' input.txt
defaultValue="14.8.21.1"
$ sed -i '' 's/defaultValue="14.8.21.1"/defaultValue="'"${newver}"'"/' input.txt
$ grep -o 'defaultValue="[^"]*"' input.txt
defaultValue="foobar"

Note that I'm using FreeBSD, whose sed may behave differently from the one in the operating system you're using. Even if this works, you should probably read the documentation for each tool and option used here to make sure you understand why it's doing what it's doing. In particular, the replacement regex I used was shortened for easier reading. To be safe, you should expand it in the same way it was expanded in the initial sed script which found the version number, so that you don't change defaultValue for other fields besides cd_version.

Community
  • 1
  • 1
ghoti
  • 45,319
  • 8
  • 65
  • 104