Is it possible to pass variable from awk to shell back

I am passing a varaible to from Shell to awk then I am doing some maniplation for that variable inside awk. I want that maniplated variable value back to shell , Is this possible .Please let me know.

Yep. it is always possible.
for eg:

a =`ls -l | awk '{print $8}'

should work.
if the output is more than 1 line. you can redirect the output to a intermediate file and use it later.

thx,
Siva

Hi,

I tried the following code:

]

This runs well, and provide me with the correct answer.

But when I redirect this command output to a variable, the value of the variable is not set.

Please advice what am I doing incorrectly in this case.

Thanks and Best Regards
Aditya

The reason is you have to tell the shell to feed the result - the stdout of the command - back to the variable:

variable="$( command1 | command2 | command3 )"

This will open a subshell, execute the pipeline and put the final output of command3 into the variable.

Btw. you will often see backticks recommended:

var=`command1 | command2 | command3`

This does basically the same, but is only possible in modern shells because they try to be compatible. Do NOT use this but the above mentioned construction.

bakunin

Thanks for the quick reply.
I tried out what you suggested:

set DataLine = "$( echo $CurrentLine | awk -F'[ :]' '{ print $1 }' )"
echo $DataLine

set DataLine = "$( echo $CurrentLine | awk -F'[ :]' '{ print $1 }' )"
echo $DataLine

But still the output for the DataLine variable is empty.
I hope I understood it correctly.

Thanks and Best Regards
Aditya

What is the value of CurrentLine?

CurrentLine is a some data that has : as seperator.

There are no special charaters in the line data.
I gues some of the combination of data charaters are converted to different smileys out here.

Try:

DataLine=$(echo $CurrentLine | awk -F'[ :]' '{ print $1 }')

without spaces around the "=" sign!

Regards

Thanks a ton Franklin52. It worked real well.

Thanks and Best Regards
Aditya

Another way than may be useful for setting multiple variables:

$ CurrentLine="The_Datas:Rest_of_line"
$ eval $(echo $CurrentLine | awk -F'[ :]' '{ print "DataLine=\"" $1 "\"\nRestLine=\"" $2 "\""}')
$ echo "<$DataLine> <$RestLine>"
<The_Datas> <Rest_of_line>
$

The eval command with debug trace :

$ set -x
$ eval $(echo $CurrentLine | awk -F'[ :]' '{ print "DataLine=\"" $1 "\"\nRestLine=\"" $2 "\""}')
++ echo The_Datas:Rest_of_line
++ awk '-F[ :]' '{ print "DataLine=\"" $1 "\"\nRestLine=\"" $2 "\""}'
+ eval 'DataLine="The_Datas"' 'RestLine="Rest_of_line"'
++ DataLine=The_Datas
++ RestLine=Rest_of_line
$

Jean-Pierre.

eval $(echo $CurrentLine | awk -F'[ :]' '{ print "DataLine=\"" $1 "\"\nRestLine=\"" $2 "\""}')

When I execute the script as you mentioned , I get blank spaces. I am using Korn Shell. Please let me know if any other way doing this.

Thanks
SHivannad

Execute the command with the -x option of the KSH like in my previous post and show us the output.

Jean-Pierre.