The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Storing commands in $variables. Paulw0t Shell Programming and Scripting 7 08-08-2008 05:42 PM
Splitting the data and storing it into 2 variables jisha Shell Programming and Scripting 10 02-17-2008 10:40 PM
Help - Setting variables equal to data from another file Crios121 UNIX for Dummies Questions & Answers 1 03-05-2007 12:40 PM
Storing data of files Fr0z3n999 UNIX for Dummies Questions & Answers 1 12-09-2006 09:47 PM
first row data into variables u263066 Shell Programming and Scripting 1 08-14-2006 07:54 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-15-2004
2nilotpal 2nilotpal is offline
Registered User
  
 

Join Date: Aug 2002
Posts: 6
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 (permalink)  
Old 04-15-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
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 (permalink)  
Old 04-15-2004
2nilotpal 2nilotpal is offline
Registered User
  
 

Join Date: Aug 2002
Posts: 6
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 (permalink)  
Old 04-15-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
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 (permalink)  
Old 04-15-2004
2nilotpal 2nilotpal is offline
Registered User
  
 

Join Date: Aug 2002
Posts: 6
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 (permalink)  
Old 04-15-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
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 10:31 AM..
  #7 (permalink)  
Old 04-16-2004
2nilotpal 2nilotpal is offline
Registered User
  
 

Join Date: Aug 2002
Posts: 6
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0