James Bond
James Bond

Reputation: 3017

Unix Export Variables from Tokenised String

Say I have a string:

'EffTimeStamp="20110203-210000"#InputCtxNm="Forigen Exchange Today"#RunType="EOD"#Age="90"

Using:

echo 'EffTimeStamp="20110203-210000"#InputCtxNm="Forigen Exchange Today"#RunType="EOD"#Region=US#Age="90"' \ |  awk -F# -v OFS="\n" '{for (i=1;i<=NF; i++) printf("%s%s", $i, (i==NF ? "\n" : OFS)) }'

I convert it to :

EffTimeStamp="20110203-210000"
InputCtxNm="Forigen Exchange Today"
RunType="EOD"
Region=US
Age="90"

I want export these as Shell Variables.

Note : Number of Variable in string may vary upon users discretion.

Please help.

Upvotes: 0

Views: 263

Answers (2)

glenn jackman
glenn jackman

Reputation: 247002

What's your shell?

Assuming bash, try this:

source <(awk -v RS="#" '{print "export " $0}' <<< "$string")

For ksh, this should work

. <(printf "%s\n" "$string" | awk -v RS="#" '{print "export " $0}')

Upvotes: 1

William Pursell
William Pursell

Reputation: 212374

In your awk, use

printf("export %s%s"

and then replace 'echo' with 'eval'.

Upvotes: 0

Related Questions