Storing awk output into variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing awk output into variables
# 1  
Old 06-20-2009
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). [in shell scripting]

example of awk output: '{ print $43 }' assuming the field is at column 43.
10.10.10.10
12.1.1.2
10.0.0.1
10.0.0.25

however, i've a problem storing each line of ip_address to variables in shell scripting. eventually, i will need to retrieve each line of ip_address (variable) and process some functions to it.

Any suggestions will be much appreciated!
# 2  
Old 06-20-2009
A common way of handling this issue is to store the output of awk to a temporary file and then process that file.
# 3  
Old 06-20-2009
One way to do it would be to read each line into the array as piped from awk...

Code:
<awk output> | while read line
do
	iparray=($line)
	echo "Read $line into array."
done

Or even writing a file with the awk output to a file and then storing each line of the file into an array.

Code:
cat $file | while read line
do
	iparray=($line)
	echo "Read $line into array."
done

Then it is just a matter of operating on the $iparray.
# 4  
Old 06-20-2009
Thanks for all of your promptly replies~
will work them out soon
# 5  
Old 06-20-2009
(ksh)
Code:
set -A IP $(awk '{print $43}' infile)

(bash (and ksh - newer ones only, I think, and new probably means circa 1993!))
Code:
IP=($(awk '{print $43}' infile))

Code:
echo ${IP[0]}
10.10.10.10

echo ${IP[1]}
12.1.1.2

...


Last edited by Scott; 06-20-2009 at 04:10 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing command output in a variable and using cut/awk

Hi, My aim is to get the md5 hash of a file and store it in a variable. var1="md5sum file1" $var1 The above outputs fine but also contains the filename, so somthing like this 243ASsf25 file1 i just need to get the first part and put it into a variable. var1="md5sum file1"... (5 Replies)
Discussion started by: JustALol
5 Replies

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

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

4. Shell Programming and Scripting

Storing outputs into variables

I need to know how to store output from one command so that it can initiate another command. chktraf -s | cut -c1-4 output would look like 321 142 256 342 123 Then if the value of the output = 0, then initiate next command. if then for xx in 01 02 03 04 05 06 07 08 09 10 do ... (4 Replies)
Discussion started by: Shaun74
4 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. UNIX for Dummies Questions & Answers

Storing variables and using them..

Hello Apologies for not having the most accurate of thread titles.. I'm using IBM Rational Synergy CM software. I use the Synergy commands in tandem with Unix commands. I have a directory containing source code objects: bash-3.00$ ccm ls *.fmb *.rdf *.pll *.mmb cre_applications.fmb-1... (1 Reply)
Discussion started by: Glyn_Mo
1 Replies

7. UNIX Desktop Questions & Answers

problem while storing the output of awk to variable

Hi, i have some files in one directory(say some sample dir) whose names will be like the following. some_file1.txt some_file2.txt. i need to get the last modified file size based on file name pattern like some_ here i am able to get the value of the last modified file size using the... (5 Replies)
Discussion started by: eswarreddya
5 Replies

8. Shell Programming and Scripting

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

9. Shell Programming and Scripting

storing output of awk in variable

HI I am trying to store the output of this awk command awk -F, {(if NR==2) print $1} test.sr in a variable when I am trying v= awk -F, {(if NR==2) print $1} test.sr $v = awk -F, {(if NR==2) print $1} test.sr but its not working out . Any suggestions Thanks Arif (3 Replies)
Discussion started by: mab_arif16
3 Replies

10. Shell Programming and Scripting

awk - storing data in variables

In AWK script how do I store data in variables for later use. I have a multiline input and I do not want to print the data read on the console Thnaks in advance. Nilotpal. (7 Replies)
Discussion started by: 2nilotpal
7 Replies
Login or Register to Ask a Question