using the getline to populate an array from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using the getline to populate an array from a file
# 1  
Old 03-02-2005
CPU & Memory using the getline to populate an array from a file

Hello,

I am trying to use the awk getline command to populate an array from a flat file.

Has anyone done this before?

So we have file1:

a
b
c
d

I want to create an array like index[x] that contains these four elements
# 2  
Old 03-02-2005
Why use awk? I can't test it right now but wouldn't :

set -A index $(cat yourfile)

do the trick or do you want to load yourfile while inside an awk program ?
# 3  
Old 03-02-2005
I wouldn't bother with getline...

Code:
$ cat > awk_script
#!/usr/bin/awk -f
{
  a[i++]=$0
}
END {
  for ( x = 0; x < NR; x++  ) {
    print "Element is..." , a[x]
  }
}
$ chmod +x ./awk_script
$ ./awk_script ./file1
Element is... a
Element is... b
Element is... c
Element is... d

Cheers
ZB
# 4  
Old 03-02-2005
when executing this is get: i++: bad number
# 5  
Old 03-02-2005
Quote:
Originally Posted by domivv
Why use awk? I can't test it right now but wouldn't :

set -A index $(cat yourfile)

do the trick or do you want to load yourfile while inside an awk program ?

I need to load the file whilst inside the awk program
# 6  
Old 03-02-2005
Quote:
Originally Posted by penfold
when executing this is get: i++: bad number
if on Solaris, try using nawk like so:
Code:
#!/usr/bin/nawk -f

# 7  
Old 03-02-2005
i'm running on AIX, i've tried so many variations but can't get to the bottom of this , any help appriciated
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Populate field in File with it's manipulated Filename?

Hi All, I need to create a script to process on 10 files. Mentioned below is one of those file and the requirement. Input File DCIA_GEOG_DATA_OCEAN.TXT Sample Record "Terr","TerrName","Dist","DistName","REGION","RgnName","BCName" "A0010000","Abilene TX A 1","A0010957","Dallas... (5 Replies)
Discussion started by: Arun Mishra
5 Replies

2. Shell Programming and Scripting

awk getline t file

I want to import a textfile with getline into var t which has several lines. How do import all lines, since it only imports the last line: while < ((getline t "textfile") > 0) (7 Replies)
Discussion started by: sdf
7 Replies

3. Shell Programming and Scripting

Shell script to populate an array from a csv file

Hi Guys, I've got a script that runs and collects statistics from one of our machines and outputs the data into a csv file. Now, that script runs fine and I managed to create another one (with a lot of help from this forum!!) to trim the csv file down to all the data that I need, rather than all... (9 Replies)
Discussion started by: jimbob01
9 Replies

4. Shell Programming and Scripting

GetLine fn always giving last line of the file

Hi, Its bit urget for me to complete. pls help me. I am parsing 2 file 1. INDATA(Data file) 2. GRPFIL(Reference) every record in INDATA should be verified with GRP_DATA. I am seeing the output from only the last line of ref file, its not searching all the lines. INDATA sample... (1 Reply)
Discussion started by: hyperion.krish
1 Replies

5. Shell Programming and Scripting

awk print header as text from separate file with getline

I would like to print the output beginning with a header from a seperate file like this: awk 'BEGIN{FS="_";print ((getline < "header.txt")>0)} { if (! ($0 ~ /EL/ ) print }" input.txtWhat am i doing wrong? (4 Replies)
Discussion started by: sdf
4 Replies

6. Shell Programming and Scripting

getline from another file

Here is I want to do: find lines in file filteredtrace.txt which is not continued as a multiply of 4 and strip them from file original_trace_framno. problem is awk used the ' symbol so pipe of getline has to use the " symbol while agument of sed has to use the " symbol, it doesn't... (8 Replies)
Discussion started by: lzq420241
8 Replies

7. Shell Programming and Scripting

Using eval to populate an array in the background

Hi. I am trying to populate an array using eval in the background within a function. So this function will create a list of 3 character directories from SVN and store in an array. I want to call this function when the script starts in the background unknown to the user. The problem is that... (2 Replies)
Discussion started by: jmiaebrown
2 Replies

8. Shell Programming and Scripting

awk - getline from parametric file name

Hi! I have an input file for an awk script that I need to split into several files and the process them separately line by line. I have splitted the input file into the other files, that have been created correctly. But, since their names are parametric (i.e. output_1.txt, output_2.txt..... (2 Replies)
Discussion started by: Alice236
2 Replies

9. Shell Programming and Scripting

shellscript.query Oracle table..populate in a text file

Hi Guys, I'm new to this forum as well as to UNIX shell scripting. I'm looking for a shellscript to query an Oracle database table and populate the result set of the query in a text file. Could you someone help me out with a sample code? Thanks, Bhagat (7 Replies)
Discussion started by: bhagat.singh-j
7 Replies

10. Shell Programming and Scripting

acessing awk array element while getline < "file"

I am attempting to write a awk script that reads in a file after awk array elements are assigned and using those elements while reading in the new file. Does this make sense? /pattern/ {tst=$3} (( getline < "file" ) > 0 ) { x=x " "tst } When I print tst in the END statement it... (9 Replies)
Discussion started by: timj123
9 Replies
Login or Register to Ask a Question