Awk return variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk return variable
# 1  
Old 08-31-2009
Awk return variable

Hi

I have 2 working script, now i'd like to get the return value from the first and give it to the 2 script (both script work correctly if I run it separately). so i think the problem is only the first line in the way i pass the variable.
Quote:
awk -v lst="$(awk 'END {a=NR}' temp2)" 'BEGIN{ap1=0; ap2=0}{
if( ($2>0) && ($4>0) ){
ap1=$2+ap1;
ap2=$4+ap2;
print $1 " " ap1 " " ap2;
ap1=0; ap2=0;}
else{
ap1=ap1+$2;
ap2=ap2+$4;
if( (ap1>0) && (ap2>0) ) {
print $1 " " ap1 " " ap2;
ap1=0;ap2=0;
}
}
} END {print lst;}' joined
in the final the "print lst", is just to check the variable, but i get an empty value.
Can anyone suggest a way to make it work please?

thanks

D.
# 2  
Old 08-31-2009
what you are assigning to lst variable is nothing. so it will give you nothing. May be you try this if you want to get the number of records in temp2...

Code:
 
awk -v lst="$(awk 'END {print NR}' temp2)" ....

# 3  
Old 08-31-2009
now I understand.

Thanks malcomex999
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Carriage return with cat variable

Hi, I wish to know how to works the carriage return with cat.. As a picture often speaks better than words, my code below : #te=`cat text.txt` (I tried this, but the same..) te=$(cat text.txt) echo $te My file text.txt below: BLABLABLABLABLALBLA BLABLABLABLABLALBLA ... (9 Replies)
Discussion started by: Arnaudh78
9 Replies

2. Shell Programming and Scripting

Assign the return value of the SQL to a variable in UNIX

Hi I am new to UNIX. I am trying the below and getting the error. I am trying to assign the variable with the value of the query result. I want this value to use in the next steps. Created UNIX file (Batch.sh) as below #!/bin/ksh sqlplus callidus/callidus4u@attstcal @Batch.sql ... (2 Replies)
Discussion started by: KrishBalu
2 Replies

3. Shell Programming and Scripting

Remove carriage return from the variable

Hi, I try to handle very large numbers with a bash script. I run ssh command in a remote server and store the output in a local variable. But this output contains a return carriage at the end. So I try to remove it by tr But I can't figure out the right notation with printf. So my problem... (6 Replies)
Discussion started by: Meacham12
6 Replies

4. Shell Programming and Scripting

How to store the return value of a command into a variable?

I want to store the return value of grep -c string filename into a variable, say count. How do I do that? For example if grep -c "string" "filename" shows 0 on executing it in the sh shell then I want to store this 0 in a variable. Is it possible? :D (5 Replies)
Discussion started by: navienavnav
5 Replies

5. Shell Programming and Scripting

variable value return of multiple command

I wrote this script #!/bin/bash var=`du -sch /var/log/messages;du -sch /var/log/maillog` echo $var I am getting result as follows. # sh my.sh 2.1M /var/log/messages 2.1M total 296K /var/log/maillog 296K total I need it like below 2.1M /var/log/messages 296K... (3 Replies)
Discussion started by: anilcliff
3 Replies

6. Shell Programming and Scripting

How to input the return value (1 or 0) ping cmd to a variable

Hi I would like to ask about my plan script I have this several workstation want to monitor and execute a command without logging it we use "rsh $host "<command>" i create csh script using foreach to loop my several workstation, my problem with the rsh command is if it encounter a... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. Shell Programming and Scripting

Return Awk Variable to Shell

I'm a bit stuck in getting variable from awk to shell. I tried searching but most of them showing to assign to shell variable via.. VAR=`echo $line | awk -F: '{print $1}'` which is correct ofcourse My problem is multiple assignments of variable like this one. The above solution will give... (10 Replies)
Discussion started by: ryandegreat25
10 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. UNIX for Dummies Questions & Answers

Get Oracle fuction return value in a variable

Hi All, :confused: I have the following code. var=' ' sqlplus user/pass@DB <<EOF whenever sqlerror exit 1 select package.func() into $var from dual; EOF echo $var But, this code does not work to display the value returned by the oracle function. Do we have to bind variables before... (3 Replies)
Discussion started by: rahulrathod
3 Replies

10. Shell Programming and Scripting

return variable from PL/SQL procedure to shell

Hi i'm calling a pl/sql procedure which is returning one variable. i'm trying to assing this value to variable in shell script the code i wrote is ** in shell script** var= 'sqlplus user/pass @ret.sql' echo $var ** and variable dum_var number exec rt_test(:DUM_VAR); exit; in... (4 Replies)
Discussion started by: ap_gore79
4 Replies
Login or Register to Ask a Question