Is it possible to pass variable from awk to shell back


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is it possible to pass variable from awk to shell back
# 1  
Old 11-09-2007
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.
# 2  
Old 11-10-2007
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
# 3  
Old 11-10-2007
Hi,

I tried the following code:

Quote:
echo $CurrentLine | awk -F: '{ print $1 }'
]

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.

Quote:
set DataLine = echo $CurrentLine | awk -F: '{ print $1 }'
Please advice what am I doing incorrectly in this case.

Thanks and Best Regards
Aditya
# 4  
Old 11-10-2007
Quote:
Originally Posted by adichand
But when I redirect this command output to a variable, the value of the variable is not set.
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
# 5  
Old 11-10-2007
Thanks for the quick reply.
I tried out what you suggested:

Code:
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
# 6  
Old 11-10-2007
What is the value of CurrentLine?
# 7  
Old 11-10-2007
CurrentLine is a some data that has : as seperator.

Quote:
For Example:

S:C200SmilieISCRETE:1:A`<`0`>`:UNSPEC:G1:CAPACITOR_N-CAPACITOR_X7R,220NF,10/-10%,0402_N-V6-0,0402,CAPACITOR_220NF-0402
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from awk script to shell?

Hi, Please need to print the Rej variable outsite the awk script which is given below...please advised how to achieve it. #!/bin/bash echo "Enter DMU Pipe delimited File name for the Feed to be validated" read DMU_File echo "Enter Pre-DMU File name for the Feed" read Predum_file ... (3 Replies)
Discussion started by: pelethangjam
3 Replies

2. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

3. UNIX for Dummies Questions & Answers

Pass shell Variable to awk

Hello, May i please know how do i pass the shell variable to awk expression in the below script. It is returning null #!/bin/bash UNINUM=720922 UNINUM_DESC=`awk -F'|' -v UNINUM=$2 '/UNINUM/ {print $4}' datafile` echo $UNINUM_DESC datafile 4|First|720194|asdasdad 4|First|720735|asdasdsa... (8 Replies)
Discussion started by: Ariean
8 Replies

4. Shell Programming and Scripting

Pass awk array variable to shell

Hi, all suppose I have following myfile (delimited by tab) aa bb cc dd ee ffand I have following awk command: awk 'BEGIN{FS="\t"}{AwkArrayVar_1=$1;AwkArrayVar_2=$2};END{for(i=0; i<NR; i++) print i, AwkArrayVar_1, AwkArrayVar_2,}' myfileMy question is: how can I assign the awk array... (7 Replies)
Discussion started by: littlewenwen
7 Replies

5. UNIX for Dummies Questions & Answers

Pass value back to unix variable

i had this unix korn shell code that connects to oracle database and execute the oracle procedure. i need to add a variable that indicates the oracle procedure failed. basically the variable is to check if the oracle procedure failed it will assign 1 and when the variable is equal to 1 it will not... (4 Replies)
Discussion started by: wtolentino
4 Replies

6. UNIX for Dummies Questions & Answers

How to pass a variable from shell to awk

I know this topic has been dealt with previously, but the solutions I've seen don't work for me apparently. I need to pass a variable defined in the shell to one in awk: $ echo $var1 3 $ cat aaa aaa 1 bbb 2 ccc 3 ddd 4 eee 5I've tried this, without success: $ awk... (2 Replies)
Discussion started by: metaltree
2 Replies

7. Shell Programming and Scripting

pass variable from awk to shell script

Hello Experts, Actually I was searching for a solution here in this forum , but didn't get what exactly I want . Is this possible to do in awk ? I am trying to do some thing like below in ksh script . Upto my knowledge I can pass shell script to awk with "-v " option. But I... (3 Replies)
Discussion started by: user_prady
3 Replies

8. Shell Programming and Scripting

Is it possible to pass variable from awk to shell script

Hello experts, can I return a value from gawk to a shell script ? My script as follows, #Here I want the num value to shell script so that I can use later gawk ' { split($0,num,","); print num }' gawk -v no=$number '{print no}' file1 ... (3 Replies)
Discussion started by: user_prady
3 Replies

9. UNIX for Dummies Questions & Answers

How to pass Shell script variable to awk

Hi, I have a shell script with an ambedded awk script. i need to pass a script variable to the awk script. Please help. Thanks in advance Himani (3 Replies)
Discussion started by: HIMANI
3 Replies

10. UNIX for Dummies Questions & Answers

How to pass a oracle variable back to the shell script

Hi, I am calling an oracle function that returns a number (either 0 or 2), how do I pass that pass to the wrapping shell script as I would like to do other things based on the value returned by the oracle function. Your help will be appreciated. -------------------------- sqlplus / <<... (3 Replies)
Discussion started by: Jtrinh
3 Replies
Login or Register to Ask a Question