1753868 Members
7469 Online
108809 Solutions
New Discussion юеВ

Re: AWK getline help

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

AWK getline help

Hello all,

I am using awk to filter a file, below is syntax that has been given to me in a previous thread however I cannot seem to get it to work:

awk 'BEGIN{while (getline LINE < "allservers.csv" > 0) {a[++$i]=LINE}}' serverlistfinal.txt

error message is:
awk: Field $() is not correct.

any help is greatly appreciated.


hello
9 REPLIES 9
Hein van den Heuvel
Honored Contributor

Re: AWK getline help

This is a snippet from a larger program right?

So what is the "$i" supposed to do?

I suspect you just want 'i', no $

In PERL the variablea are $i and such

in AWK the $x referer to auto-split fields sucht as $1 for the first, $2 for teh second, $NF for the last, from the last line processed in $0.

hth,
Hein.
James R. Ferguson
Acclaimed Contributor
Solution

Re: AWK getline help

Hi Chris:

# awk 'BEGIN{while ((getline LINE < "/etc/hosts") > 0) {a[++i]=LINE}}' serverlistfinal.txt

...as for example:

# awk 'BEGIN{while ((getline LINE < "/etc/hosts") > 0) {a[++i]=LINE}};END{for (i=1;i<10;i++) {print a[i]}}' /dev/null

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: AWK getline help

Ah, I think I found 'the other thread'. # 1167461 right?

Yeah, that example was wrong in syntax and possibly also in intent.

What you really want os to build a use 'associative array' , once you found a good 'key' from the files involved.
Rigth not, the tables listed in the other topic do NOT show an obvious match fields.
But lets assume, that field #4 (just a random pick, not the right pick!) in the first input file is a lun number, and field#6 there provide useful data.

And assume that for example field 2 in the second file is lun
Now you want a STUCTURE (not exact code) like:

BEGIN {
while (getline f1_line < "file") {
split (f1_line,f1_fields,",");
f1_luns[f1_fields[2]] = f1_fields[6];
}
}
{
:
if ($2 in f1_luns) { ...

Clear?

Hein.
lawrenzo
Trusted Contributor

Re: AWK getline help

hello guys,

Thanks for the input ...

Hein, I am using the same code for a different purpose - I could use grep etc but still attempting to improve my awk skills.

I have got James example to work ...

Chris
hello
lawrenzo
Trusted Contributor

Re: AWK getline help

I should elaborate on the task at hand though ....

I have a file serverlistfinal.txt that contains 700 + servers in our environment.

the file allservers.csv is a file that needs updating and has over 1000 entries therefor I need to print all rows from allservers.csv that are in serverlistfinal.txt.

I figure I change the syntax "i=1;i<10;i++" as this displays the top 10 and also > 0 is not relevant in this code?

Chris.
hello
James R. Ferguson
Acclaimed Contributor

Re: AWK getline help

Hi (again) Chris:

> I figure I change the syntax "i=1;i<10;i++" as this displays the top 10 and also > 0 is not relevant in this code?

You could count the elements read by the 'getline' and use that value as the upper bound for your later 'for' loop (of course).

As for the:

while (getline LINE < "file" > 0)

...'getline' returns one (1) if it successfully reads a record; A zero (0) is returned for EOF (end-of-file). A -1 is returned if for instance, the file cannot be opened.

Regards!

...JRF...
lawrenzo
Trusted Contributor

Re: AWK getline help

and I believe that

awk 'BEGIN{while (getline LINE < "allservers.csv") {a[++i]=LINE}};{print $0}' serverlistfinal.txt

is the solution

:-)
hello
Hein van den Heuvel
Honored Contributor

Re: AWK getline help

Yes, that's a fine solution IF the problem is to create a simple array with all the line from that file.

But that's so boring!

What are you going to do with the data from that array later? Run it through a loop 'looking' for stuff?

>> Hein, I am using the same code for a different purpose - I could use grep etc but still attempting to improve my awk skills.

Exactly! That's precisely why I hinted you towards picking a useful key for the array, not just a list of numbers 1..end.
With a good key/index you can do a direct lookup suing the 'i in array' construct. You can also readily 'delete' specif array elements which are (no longer) useful.

Check it out. It's useful and will bring you to a whole new level in awk usage, getting you ready for... perl

:-)


Hein.

lawrenzo
Trusted Contributor

Re: AWK getline help

ok thanks for the tip Hein,


the solution I suggested above does not actaully do what i need it to ....

I have some good idea's here so will continue to work through my book and come up with what I need ...

I finally want all rows found in serverlistfinal.txt to be printed with an extra field (delimited with ",")supported to another file and if the line does not exist then print not supported ie:

Server,DNSNAME,Deployed,Active,United Kingdom,ADDRESS,REU-Unix,REU-Config,support
or
Server,DNSNAME,Deployed,Active,United Kingdom,ADDRESS,REU-Unix,REU-Config,unsupport

make sense?

anyhow I shall continue with this and let you know.

cheers

chris
hello