How to redirect the output of awk to different variables?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to redirect the output of awk to different variables?
# 1  
Old 01-14-2013
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.

Code:
$ 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`

It is possible to achieve the output by running the awk once ?
# 2  
Old 01-14-2013
Code:
awk -F"'" '/can be any/ { print $4, $6}' myFile

# 3  
Old 01-14-2013
If your shell supports the syntax below (process substitution):

Code:
bash-4.2$ _pattern='can be any'
bash-4.2$ read SRG VER < <( awk -F\' -v p="$_pattern" '$0 ~ p { print $4, $6 }' infile)  
bash-4.2$ printf 'SRG is %s, VER is %s\n' "$SRG" "$VER"
SRG is many, VER is 123
bash-4.2$

If the values contain special shell characters (white spaces for example),
you can use something like this:

Code:
bash-4.2$ cat infile
<value ='can be any' string='many many' version='123 456'/>
<value ='xyz' string='less' version='456'/>
bash-4.2$ _pattern='can be any'
bash-4.2$ IFS=, read SRG VER < <( awk -F\' -v p="$_pattern" '$0 ~ p { print $4, $6 }' OFS=, infile )
bash-4.2$ printf 'SRG is %s, VER is %s\n' "$SRG" "$VER"
SRG is many many, VER is 123 456


Last edited by radoulov; 01-14-2013 at 06:26 PM..
This User Gave Thanks to radoulov For This Post:
# 4  
Old 01-14-2013
Code:
awk -F"'" '/can be any/ { print $4, $6}' myFile

Given one liner does not store the output to different variables.
Just I gave sample data. Those variables & its values are needed for rest of the part in script.

radoulov solution is what i wanted.
Will give a try.
# 5  
Old 01-14-2013
Code:
eval $(awk -F"'" '/can be any/ { print "SRG="$4 " VER=" $6}' myFile)

This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Redirect output to the same input file in awk

Hi, I want to compare a value from test file and redirect the o/p value to the same file input file 250 32000 32 128 Below is my code awk '{ if ($1 < "300") print $1 > /tmp/test}' test want to compare 250 < 300 then print 300 to the same place below is the... (24 Replies)
Discussion started by: stew
24 Replies

2. Shell Programming and Scripting

Redirect script output to a file and mail the output

Hi Guys, I want to redirect the output of 3 scripts to a file and then mail the output of those three scripts. I used below but it is not working: OFILE=/home/home1/report1 echo "report1 details" > $OFILE =/home/home1/1.sh > $OFILE echo... (7 Replies)
Discussion started by: Vivekit82
7 Replies

3. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

4. Shell Programming and Scripting

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 testval,USA,loc2,testing02 testval1,GB,loc4,testing01 awk statement awk -F , '{print $2,$3}' USA loc2 GB loc4 I need a method where... (6 Replies)
Discussion started by: duckeggs01
6 Replies

5. 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

6. Shell Programming and Scripting

redirect an awk string output to a script input with pipes

Hi, I have a function in a bash script that returns a string after some operations using awk. The following code returns 555 $VARIABLE="EXAMPLE" get_number $VARIABLE this value I'd like to pass it as a second argument of another script with the following usage myscript.sh <param1>... (7 Replies)
Discussion started by: rid
7 Replies

7. Shell Programming and Scripting

output redirect

Hi i am compiling a source code by make command. i want to redirect the output of make to a file but at the same time i want to see the output in terminal. how to do this ?. please suggest your idea. thanks in advance. Saravana ---------- Post updated at 05:24 PM ----------... (2 Replies)
Discussion started by: tsaravanan
2 Replies

8. 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

9. Shell Programming and Scripting

Redirect Output

Hi, I would like to list files: ls *.hdf But I would like a copy of the output directed to the screen, but also APPENDED to a text file: test.txt I have tried: ls *.hdf | tee test.txt However, that will just write over everything already existing in test.txt. How can I append the... (1 Reply)
Discussion started by: msb65
1 Replies

10. Shell Programming and Scripting

awk redirect output to shell variable

I'm trying to redirect awk's output to a BASH variable. Currently the statement looks like this options=`awk '{ while (i < NF) { print $i; ++i } }' answer but for some reason that makes $options look like this: 1 2 3 4 1 2 3 Now, the input file 'answer' is taken from a dialog... (3 Replies)
Discussion started by: dhinge
3 Replies
Login or Register to Ask a Question