Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


awk output to multiple variables


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
awk output to multiple variables

Hi

I need to assign the ouput of a awk statement to two variables; below is a example of the txt file i have which I use awk against

sample file
Code:
testval,USA,loc2,testing02
testval1,GB,loc4,testing01

awk statement

Code:
awk -F , '{print $2,$3}'
USA loc2
GB loc4

I need a method where i can store the output into two separate variables one for the $2 and one for $3. I would then use the variables in my script for further operations. Any ideas anyone

Cheers

Last edited by joeyg; 07-16-2012 at 12:58 PM.. Reason: Please wrap data and sripts with CodeTags
# 2  
Need more info

What are you trying to do?
Need to see an example of your desired output.
# 3  
First problem, all your data is separated by white space so you'll get two things, not four. Easily solved though:

Code:
$ awk -F, -v ORS="," '{print $2, $3}' data

USA loc2,GB loc4,

$

Now it's just a matter of letting the shell do splitting on ",".

Code:
$ OLDIFS="$IFS"
$ IFS=","
$ set --  $(awk -F, -v ORS="," '{print $2, $3}' data)
$ IFS="$OLDIFS"
$ echo $1

USA loc2

$ echo $2

GB loc4

$

# 4  
Hi

With the two variables created from the awk statement I would like to feed them into another command set eg lpadmin -L $var1 -D$var2.

I was attempting to do it with the following ;
Code:
awk -F ,  '{print $2 $3}' | while read var1 var2
do
    echo $var1 $var2
done


Last edited by Franklin52; 07-17-2012 at 09:39 AM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Quote:
Originally Posted by duckeggs01

I was attempting to do it with the following ;

awk -F , '{print $2 $3}' | while read var1 var2
do
echo $var1 $var2
done
That will work fine (assuming you provide a file to awk or have redirected its standard input, and that the missing comma between $2 and $3, which is present in your original post, is a typo in this one), so I assume that your real code is different.

Regards,
Alister
# 7  
Keep in mind that if your shell runs that while-loop in a subshell, you will need to do all the work with those variables within the body of the while-loop.

Regards,
Alister
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple variables using awk and for loop for web form submission

Hi My goal is to fill an HTML form and submit. What I have managed to do: 1. curl command to fill up the form and submit 2. a file which has the input curl command: curl -v -b cookie.txt -d __CSRFToken__=dc23d5da47953b3b390ec68d972af10380908b14 -d do=create -d a=open -d... (10 Replies)
Discussion started by: zorrox
10 Replies

2. UNIX for Dummies Questions & Answers

Read in Multiple log files and output selected variables and values to cvs file

I have several problems with my problems: I hope you can help me. 1) the If else statement I am getting an error message. My syntax must be incorrect because the entire statement is throwing an error. For example in filew.log if these items don't exist Memsize, SASFoundation and also if... (0 Replies)
Discussion started by: dellanicholson
0 Replies

3. UNIX for Advanced & Expert Users

awk With multiple print variables

Hi, I have a parameter which will be having the fields which needs to be filtered or derived. But This is not working which is mentioned below. I am using the below mentioned OS uname -a SunOS udora310 5.10 Generic_150400-11 sun4v sparc sun4v param='$1$2' nawk -v para="$param" 'BEGIN... (11 Replies)
Discussion started by: Mohammed Rafi
11 Replies

4. Shell Programming and Scripting

Format output into columns, variables with multiple entries

Hello all, I've got a script that collects data on file systems and prints out specific data about each. I've formatted headers w/ printf like so. printf "\033 and I had the content of the varibles printed out beneath those columns like so: printf... (5 Replies)
Discussion started by: awreneau
5 Replies

5. Shell Programming and Scripting

How to redirect the output of awk to different variables?

Hi, I search for a string "can be any" and print the fields $4,$6 in to variables. $ cat input <value ='can be any' string='many' version='123'/> <value ='xyz' string='less' version='456'/> $ cat tried_code SRG=`awk -F\' '{ print $4 }' input` VER=`awk -F\' '{ print $6 }' input` ... (4 Replies)
Discussion started by: greet_sed
4 Replies

6. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

7. Shell Programming and Scripting

Variables into SED or AWK and multiple commands

Hello I am hoping you may help. I am not sure how to go about this exactly, I know the tools but not sure how to make them work together. I have two SED commands that I would like to run in a shell script. I would like to take the manual input of a user (types in when prompted) to be used... (4 Replies)
Discussion started by: lostincashe
4 Replies

8. Shell Programming and Scripting

awk output to variables

Im trying to read the awk output to two variables. In the below code i need to store the if part output to a variable (NEW) and else part output to another variable (BAD). Pls shed some light on this snippet or if there is any other way to achieve it wud be appreciated (only condition is it needs... (7 Replies)
Discussion started by: michaelrozar17
7 Replies

9. Shell Programming and Scripting

sql output from multiple rows and columns as variables in a script

This is for an Oracle journal import. I was using a pl/sql package and oracle API's. Oracle added invoker rights to their API's and now my package won't run. I didn't want to use their API's anyway. The only reason i was using pl/sql and the API's (just a package) was to utilize a cursor. How... (2 Replies)
Discussion started by: lmu
2 Replies

10. Shell Programming and Scripting

Storing awk output into variables

Hi all, Currently, i have a log file seperated by 'tab' and each record starting with a new line. i managed to retrieve the column that i'm interested in. (source_ip_address: xxx.xxx.xxx.xxx). example of awk output: '{ print $43 }' assuming the field is at column 43. 10.10.10.10... (4 Replies)
Discussion started by: faelric
4 Replies

Featured Tech Videos