Need to assign value to a variable and export it at the same time

I have a file a.xml contaning:

<customerId>999</customerId>
<aaa>09876</aaa>

Now I want to extract the value '999' and save it in a variable and then export the variable. Something like the below:

export CUSTOMER_ID=cat a.xml |grep customerId |awk -F ">" '{print $2}' |awk -F "<" '{print $1}'

Whereas
cat a.xml |grep customerId |awk -F ">" '{print $2}' |awk -F "<" '{print $1}' is giving the expected result i.e. 999

But when I want to save it in a variable, it is not working :(. Please help me urgently. My Operating System is SUSE Linux.

Hi

Your command is correct. You just need to use backticks:

export CUSTOMER_ID=`cat a.xml |grep customerId |awk -F ">" '{print $2}' |awk -F "<" '{print $1}'`

Also, you can simplify this command like this:

 export CUSTOMER_ID=`awk -F "[><]" '/customerId/{print $3}' a.xml`

Guru.

backtick operator missing.
(also avoid useless cat usage :slight_smile: )

export CUSTOMER_ID=`grep customerId a.xml|awk -F ">" '{print $2}' |awk -F "<" '{print $1}'`

Thank you Guru and Ningy. I can now save and export it.

But how do I use the value of this exported variable from outside of the script.

i.e. when I exit the script and do a echo $CUSTOMER_ID, I do not see '999'.

---------- Post updated at 02:15 AM ---------- Previous update was at 01:57 AM ----------

Well .. now I can use the exported value globally by running script like below

. tool.sh