How to store multiple outputs from an awk command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to store multiple outputs from an awk command?
# 1  
Old 12-10-2009
How to store multiple outputs from an awk command?

Code:
x=`echo $line | awk -F "|" '{print $1;print NR}'`

How will I get the 2 return values ($1 and NR) from awk to variables?

Last edited by zaxxon; 12-10-2009 at 10:12 AM.. Reason: use code tags please
# 2  
Old 12-10-2009
I'm sure there are a million ways to do it, here's one...

Code:
x1=`echo $line | awk -F "|" '{print $1}'`
xNR=`echo $line | awk -F "|" '{print NR}'`

# 3  
Old 12-10-2009
If xNR is a big value, how to store the actual value instead of in exponential terms?
# 4  
Old 12-10-2009
Some more ways with only one awk:

ksh
Code:
echo $line |  awk -F "|" '{print $1; print NR}' | xargs | read x1 x2

Code:
echo $line |  awk -F "|" '{print $1,NR}' | read x1 x2

-or-
Use an array:
Code:
X=( $(awk -F "|" '{print $1,NR}' <<<$line ) )
echo ${X[0]} 
echo ${X[1]}

bash/ksh
Code:
read x1 x2 < <(echo $line |  awk -F "|" '{print $1,NR}'

)
Code:
read x1 x2 < <(awk -F "|" '{print $1,NR}' <<<$line)

bash/ksh/posix:
Code:
echo $line |  awk -F "|" '{print $1,NR}' |{ read x1 x2
echo $x1
echo $x2
}

Code:
set -- $(echo $line | awk -F "|" '{print $1,NR}')
x1=$1
x2=$2

-or-
Code:
x1="${line%%|*}"; x2=1

Smilie
# 5  
Old 12-10-2009
Another...
Code:
eval $(echo $line | awk -F "|" '{printf "x=%s y=%s", $1, NR}')
echo $x $y

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Consolidating multiple outputs in one file

Dears, i am stuck here i have 3 scripts running at one time and all the three scripts finish at different time and each script generate 1 file with different name. so i will have three files. i dnt know which script finish first i want to have a script which check if all the there files are... (6 Replies)
Discussion started by: mirwasim
6 Replies

2. Shell Programming and Scripting

Returning multiple outputs of a single line based on previous repeated lines

Hello, I am trying to return a time multiple times from a file that has varying output just before the time instance, i.e. cat jumped cat jumped cat jumped time = 1.1 cat jumped cat jumped time = 1.2 cat jumped cat jumped time = 1.3 In this case i would like to output a time.txt... (6 Replies)
Discussion started by: ryddner
6 Replies

3. UNIX for Dummies Questions & Answers

Writing multiple outputs in to separate cells of CSV file

Hi I am writing a script which has multiple awk statements and each statement gives me a numeric count as an output. I want those output to be stored in different cells of a csv file. say 12 awk statements give 12 output and i want them in diffrenet cells of csv file. Thank you guys..!! (4 Replies)
Discussion started by: prabhat.diwaker
4 Replies

4. Shell Programming and Scripting

ksh : need to store the output of a awk command to a array

I have awk command : awk -F ' ' '{ print $NF }' log filename And it gives the output as below: 06:00:00 parameters: SDS (2) no no no no doc=4000000000). information: (6 Replies)
Discussion started by: ramprabhum
6 Replies

5. Shell Programming and Scripting

Array in awk outputs multiple values

Disclaimer: OP is 100% Awk beginner. I use this code on ASCII files I need to report against. awk 'BEGIN { tokens = 0 tokens = 0 tokens = 0 } { for (token in tokens) { if ($1 == token){print $0; tokens++;}}} END {for (token in tokens){ if( tokens ==... (1 Reply)
Discussion started by: alan
1 Replies

6. Shell Programming and Scripting

simple join for multiple files and produce 3 outputs

sh script file1 filea fileb filec ................filez. >>output1 & output2 &output3 file1 z10 1873 1920 z_number1_E59 z10 2042 2090 z_number2_E59 Z22 2476 2560 z_number3_E59 Z22 2838 2915 z_number4_E59 z1 1873 1920 z_number1_E60 z1 ... (9 Replies)
Discussion started by: stateperl
9 Replies

7. UNIX for Dummies Questions & Answers

multiple variables assignement (stdout/stderr outputs)

Hi all, I've been looking around for this for a while and can't seem to find a satifactory way to do what I want: I would like to assign the output of stdout to a variable and that of stderr to another one, and this without using temporary files/named pipes. In other words be able to assign... (4 Replies)
Discussion started by: anthalamus
4 Replies

8. Shell Programming and Scripting

create outputs from other command outputs

hi friends, The code: i=1 while do filename=`/usr/bin/ls -l| awk '{ print $9}'` echo $filename>>summary.csv #Gives the name of the file stored at column 9 count=`wc -l $filename | awk '{print $1}'` echo $count>>summary.csv #Gives just the count of lines of file "filename" i=`expr... (1 Reply)
Discussion started by: rajsharma
1 Replies

9. UNIX for Dummies Questions & Answers

Multiple Substring Outputs

Hello, I am reading a file with millions of lines in it. Each line is big line containing several xml tags. I need to Output just the value of two tags in a seperate flat file. For eg- I need to output whats present in <ComponentName> something </ComponentName> and another tag is... (2 Replies)
Discussion started by: sunnybehl
2 Replies

10. Shell Programming and Scripting

How to store the outputs of a shell script inside a log file???

My shell script file name is test.sh and the contents of this test.sh file are ps_file="package1.ps" echo $ps_file ps_file1=`echo $ps_file| sed "s/.ps//g"` echo $ps_file1 ps2pdf -dSAFER -sPAPERSIZE=a4 /tmp/A380_RFS24/amm_r0_ps/$ps_file1.ps /tmp/A380_RFS24/amm_r0_pdf/$ps_file1.pdf Now i... (2 Replies)
Discussion started by: sunitachoudhury
2 Replies
Login or Register to Ask a Question