awk - storing data in variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - storing data in variables
# 1  
Old 04-15-2004
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.
# 2  
Old 04-15-2004
You can pipe the results through read, e.g. to get the first and second fields from the third line of a file....

awk 'NR==3{print $1, $2}' file1 | read var1 var2
# 3  
Old 04-15-2004
awk - storing multiple line data in variables

Hi Ygor,
I created the following file <<t1.awk>>
BEGIN{FS = "|"; RS = "\n"}
NR==2{print $1, $2} | read var1 var2
echo $var1 ' ' $var2

as my data is "|" delimited
then from $prompt I am writing:
awk -f t1.awk abc.flag (my source file)
it gives me an error:
awk: syntax error at source line 2 source file t1.awk
context is
NR==2{print $1, $2} >>> | <<< read var1 var2
awk: bailing out at source line 4

Please help
Thanks in advance
# 4  
Old 04-15-2004
The read command is a unix shell command NOT an awk command

$ cat abc.flag
1|2|3
e|g|3
1|2|3
$ cat t1.awk
BEGIN{FS = "|"; RS = "\n"}
NR==2{print $1, $2}
$ awk -f t1.awk abc.flag | read var1 var2
$ echo $var1 ' ' $var2
e g
# 5  
Old 04-15-2004
awk - storing multiple line data in variables

Hi Ygor,
Thanks for your prompt help.
Can we read the files form line 1 and 2 at one attempt through awk?
individually I am able to read the lines and redirect to variables.
Can I read both the lines together and redirect to 6 variables (I am refering to the file layout that you gave as example:
a|b|c
1|2|3
x|o|p

my t1.awk now looks as follows:
BEGIN{FS = "|"; RS = "\n"}
NR==2{print $1, $2 $3}
NR==1{print $1, $2}

then I wrote:
awk -f t1.awk abc.flag | read - and then I got confused as the $1 and $2 variables will overwrite the previous $1 and $2 variables.
How do I retain the values that I have read in the first line - (NR==2{print $1, $2 $3})
and then go over to the next line.

Hope I cud explain.

Regards,
Nilotpal.
# 6  
Old 04-15-2004
Not sure I understand entirely. Perhaps capture the output from awk in an array?

$ cat abc.flag
a|b|c
1|2|3
x|o|p
$ cat t1.awk
BEGIN{FS = "|"; RS = "\n"}
NR==2{print $1, $2 $3}
NR==1{print $1, $2}
$ set -A var -- `awk -f t1.awk abc.flag`
$ echo ${var[0]} ' ' ${var[1]} ' ' ${var[2]} ' ' ${var[3]}
a b 1 23

Or perhaps assign to variables in awk....

$ cat t1.awk
BEGIN{FS = "|"; RS = "\n"}
NR==1{j=$1; k=$2}
NR==2{print j, k, $1, $2, $3}
$ awk -f t1.awk abc.flag
a b 1 2 3

Last edited by Ygor; 04-15-2004 at 11:31 AM..
# 7  
Old 04-16-2004
awk - storing multiple line data in variables

hi Ygor,
Thanks for the assistance.
I am able to use the local variable approach (j=$1...) but not the array approach. The arracy approach is taking the entire string 'awk -f t1.awk abc.flag' as an argument and not the output of awk awk -f t1.awk abc.flag
My t1.awk now looks like:

BEGIN{FS = "|"; RS = "\n"}
NR==1{j=$1;k=$2}
NR==2{print j, k, $1, $2, $3}

then $ awk -f t1.awk abc.flag
gives me:
a b 1 2 3
on the console.
How can I capture this console output so that I can use them in a shell script to insert into a oracle table?

I guess I cannot do database activities (insert/select) within a AWK script.

My Insert shell script is:
sqlplus $USERNAME/$PASSWD@clbd << END
insert into nr_test (COL1, COL2, COL3, COL4, COL5 ) VALUES ('$var1', '$var2', $var3, $var4, '$var5);
EXIT;
END

so typically i need the data in the awk variables j,k,$1, $2, $3 to be acceptted into $var1', '$var2', $var3, $var4, '$var5

Please advice.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing the SQL results in array variables

Requirement 1) I need to execute 15 SQL queries in oracle through linux script. All these query results needs to be stored in array variables. Requirement 2) And these 15 queries needs to be executed in parallel. Requirement 3) Once all the queries executed then the shell script should... (3 Replies)
Discussion started by: Niranjancse
3 Replies

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

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

4. Shell Programming and Scripting

To read data word by word from given file & storing in variables

File having data in following format : file name : file.txt -------------------- 111111;name1 222222;name2 333333;name3 I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2). i.e val1=11111 &... (2 Replies)
Discussion started by: sjoshi98
2 Replies

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

6. Shell Programming and Scripting

perl: storing regex in array variables trouble

hi this is an example of code: use strict; use warnings; open FILE, "/tmp/result_2"; my $regex="\\ Starting program ver. (.*)"; my $res="Program started, version <$1> - OK.\n"; while (<FILE>) { if ($_ =~ /($regex)/) { print "$res"; } } close FILE; This finds $regex and print... (3 Replies)
Discussion started by: xist
3 Replies

7. Shell Programming and Scripting

Storing commands in $variables.

Hi I'm trying to store commands in variables... like so.. # lastcmd=" $t1 | $t2 | $t3 | $t4 | sort | uniq" t1="sed -e 's/http:/<li><a href=\"http:/'" t2="sed -e 's/http:.*/&\">&<\/a>Web Link<br>/'" t3="sed -e 's/.*. mailto:/<li><a href=\"mailto:/'" t4="sed -e... (7 Replies)
Discussion started by: Paulw0t
7 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

Splitting the data and storing it into 2 variables

Hi, I have a file in the given format: |aaa |hhh |fat hat chat |make sure I need to get store in a variable,say 'error' the value of the string 'fat hat chat' In short, echo $error should give the result fat hat chat Is this possible using awk? Also, can this be split.For... (10 Replies)
Discussion started by: jisha
10 Replies

10. UNIX for Dummies Questions & Answers

Storing data of files

Hi, How to store datas of the file into a string? Thanks in advance.. (1 Reply)
Discussion started by: Fr0z3n999
1 Replies
Login or Register to Ask a Question