Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running through file and storing each line in array[i]

Status
Not open for further replies.

Biggy2872

Programmer
Dec 23, 2008
3
CA
Hi, I have a fairly lengthy awk program I am writing for my personal media server.(unraid)

The awk program is contained completely within a BEGIN{} and is run as a part of a web interface.

I need a loop writen in awk for within BEGIN{} that will take each line from an input file and store it in an array.

line 1 goes into array[1], line 2 into array[2]... etc.

so something like this.... although this did not work

Code:
BEGIN {
#stuff

#Read lines in file
file_to_read = "/file/location"
cc = 0 
while (( getline line < file_to_read ) > 0 ) {
  # just in case written in windows
  gsub("\r","", line)
  # store line in array
  array[cc]   = line
  cc++;
}
close(file_to_read);

#more stuff
}

I have never used awk before this program. what am i doing wrong and how do i fix it?

Please help,

Matt
 
although this did not work
Could you elaborate ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The code posted above would run without error but when I would call the array, every element was null.

I figured out the solution after much trial and error and coming across a similar yet unrelated post on here....


Code:
file = "file_name"
  # Take each line from file and store in temp array
  while(getline < file){
  n=split($0,array,"\n")
  }

The webserver that is serving the interface is programed in awk (the original programmers choice for a number of reasons). The program I am writing is a plug-in for this interface to be able to enable/disable scripts on booting the server. This is important because the linux based OS is run from a flash drive and any add-on packages/modifications that differ from the original distribution need to be installed/invoked at every boot.
(look up unRAID (server OS) in google for more information on the linux os ditribution)

A variable "theHTML" is populated with the desired browser output and the details of each script are stored in .conf files. I needed a way to get the contents of a .conf into an array to be able to populate "theHTML" with the lines from the file (for viewing said file contents in the web interface).

I hope you followed all that. If not, just let me know, I could post/send the code to help you get a better idea

Cheers,
Matt
 
Hi

Matt said:
The code posted above would run without error but when I would call the array, every element was null.
I actually tried your code with [tt]gawk[/tt] and [tt]mawk[/tt] on one of my test files and worked correctly. I mean, the array was populated, each element containing a line from the test file.


Feherke.
 
hmmmm.... I figured it out.

In the original code
Code:
cc = 0
should have been
Code:
cc = 1

the test file i used was only a single line (i thought it was more.. should have opened it to check) and when I tested the array from 1 to 10, it returned all empty (because it was in array[0]).

Split starts the array at 1 and gave me the behaviour I was looking for.

Thank you for your help in figuring out what I had done wrong.

Cheers,

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top