Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-17-2011, 02:34 PM   #1
LQ Newbie
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
how to use array in awk

#!/bin/awk -f
/sifs/ { print $4 > "crack" }
/sifs/ { getline;print $1,"\t",$2,"\t\t",$3,"\t",$4 > "SIFS" }
/Crack Length/ {i=1;next} /Determining/ {i=0} i {print > "Crack_Length"}

this my script..i hav send some data to SIFS and Crack_Length i want to call this data in this script and store it into an array1 and array2...and then i hav to print this arrays....please help me out to right a script for this...thanks in advance........
Old 10-17-2011, 04:18 PM   #2
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
It doesn't look like you've done anything with arrays in your script, but perhaps this will get you started in the direction; it's adapted from Aho, Kernighan and Weinberger The AWK Programming Language's chapter on arrays in AWK.

AWK provides one-dimensional arrays for storing strings and number; for example, the statement
x[NR] = $0
assigns the current input line to element NR of the array x.

It's possible to read the entire input into an array; this program prints its input in reverse line order:
{ x[NR] = $0 }
END { for (i = NR; i > 0; i--) print x[i] }
The first action reads the input line in the array x, using the line number as a subscript; the read work is done in the END statement. To start are the first line of input and go to the last, you would revers the for tests.

You can also do multi-dimensional arrays; e.g., they're not supported directly but are simulated using a one-dimensional array:
for (i = 1; i <= 10; i++)
     for (j = 1; j <= 10; j++)
          arr[i, j] = 0
this creates an array of 100 elements whose subscripts appear to have the form 1,1, 1,2 and so on. Internally, though, are stored as strings of the form 1 SUBSEP 1, 1 SUBSEP 2, and so on. The built-in variable SUBSEP contains the value of the subscript-component separator; its default value is not a comma but "\034", a value unlikely to appear in normal text.

The test for array membership with multidimensional subscripts uses a parenthesized list of subscripts, such as
if ((i,j) in arr) ...
To loop over such an array, you would write
for (k in arr) ...
and use split(k,x,SUBSEP) if access to the individual subscript components is needed.

You don't need to declare an array, it will spring into existence by mentioning it (as with variables).

Hope this helps some.
Old 10-17-2011, 04:25 PM   #3
LQ Newbie
Registered: Oct 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
BEGIN {print "crack length" }
/crack length/ {i=1;next} /Determining/ {i=0}
i {j++;
array1[j] = $1}
how to print this array using a for loop...please help me on this....
Old 10-17-2011, 07:30 PM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,530

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
I suggest you re-read the second example provided by tronayne


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] storing array from awk in bash ghantauke Linux - Newbie 20 11-25-2010 09:59 AM
[SOLVED] printing array in awk ghantauke Linux - Newbie 3 11-24-2010 09:16 AM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
Best awk array tutorial bijit1709 Programming 1 01-25-2008 01:04 AM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:22 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration