Search for string in a file, extract two another strings and concatenate to a variable

I have a file with
<suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux">
Need to find word "build" then
extract build number, which is 19921 also
release number, which is 6.1.5 then
concatenate them to one variable as "6.1.5_19921".

I understand how to do last step but how should I do with first two...:wall:
Please show me. Thank you.

Handling XML isn't trivial. It's not one "thing", it's a lot of things.

How I'd suggest doing it is breaking down the tag into multiple lines so you can read "date=Trump Tue 06/19/2012 11:41 AM EDT", then "machine=garg-ln", etc, etc, until the tag is done. xargs can do that easily.

Then you read one by one, splitting on = or < or > into two variables, catch the names you want and ignore the rest.

#!/bin/sh

# Remove temp file on exit
trap "rm -f /tmp/$$" EXIT

# Little-known incredibly useful property of xargs:  it handles quotes.
# We can use it to transform x='a' y='b c d' into lines like
#
#       x=a
#       y=b c d
#       ...
#
# Ordinary 'while read' loop can handle that easily, splitting on the first =.
xargs -n 1 < file > /tmp/$$

while IFS="=<>" read NAME VALUE
do
        case "$NAME" in
        build)          BUILD="$VALUE"          ;;
        release)        RELEASE="$VALUE"        ;;
        *)                                      ;;
        esac
done < /tmp/$$

echo "${RELEASE}_${BUILD}"

Thank you very much for quick response I'll try it, and let you know how it works! Thank you !!!

P.S. Looks like missed open brackets in loop, is it correct?

This code worked, nothing's missing AFAIK.

What brackets do you think I'm missing? Shell doesn't use brackets to denote code blocks. ( ) denote subshells but aren't the same thing; and if you don't know if you need a subshell, you probably don't.

Thank you,Corona688! One more question:
I have to change " < file > "to my real file name with path ( /usr/arch/suite.xml )
Should I change $$ in /tmp/$$ ?..
Sorry I just started to scripting and observe any information about.
Thank you again!

---------- Post updated at 09:47 AM ---------- Previous update was at 09:23 AM ----------

I found one more thing, from this thread:_http://www.unix.com/shell-programming-scripting/105016-search-string-file-extract-another-string-variable.html (since I don't have permission to publish URLs yet, I posted like that)

MYVAR=`awk -F'"' '/build/ { print $6 "_" $10 }' suite.xml`
export MYVAR
echo $MYVAR

It looks like

iway@garg-ln:~/scripts> ./new.sh
30772_6.1.5

Of cause it will work only for specific string,
and I still waiting for your advises, your code and idea I like better...
Thank you for your time and consideration!

Naturally you have to give it the real file's location one way or another. The < > must stay, though! That's redirection, not fluff.

Why change it? Does it not work?

$$ is the shell's PID, which I use to pick a unique temporary file. /tmp/ is the location for temporary files.

If the order of things changes, it will break down. If it uses ' instead of ", it will break down. If they split the tag across lines, it will break down.

What advice are you needing? You figured out the one thing you needed to change -- the file location -- yourself. Does it work or not?

If it doesn't work, post the complete script you have including all changes you've made.

1 Like

Yes! it works!
I just missed '<' suite.xml '>' - since I insert them it works perfect!
Thank you very much!
And previous code wasn't good enough you described what I thought.
Thank you for you help.

1 Like