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!

awk file manipulation

Status
Not open for further replies.

rogers42

Technical User
Mar 30, 2007
64
CA
Hi,

I am new to awk and had the following questions w.r.t file manipulation

1, How do I read multiple files and search for common records (in the two or more files) ?

2, How can I exectue an awk utility with out having to specify an input file name at the command line ?

Thanks in advance.

rogers42
 
Hi

rogers42 said:
1, How do I read multiple files and search for common records (in the two or more files) ?
Code:
awk 'NR==FNR{v[$0]=1;next}v[$0]' /input/file1 /input/file2

[gray]# or[/gray]

gawk '{v[$0]++}END{for(l in v)if(v[l]==ARGIND)print l}' /input/file1 /input/file2 /input/filen
rogres42 said:
2, How can I exectue an awk utility with out having to specify an input file name at the command line ?
Code:
awk 'BEGIN{print"Hello World !"}'

Feherke.
 
Thanks faherke. Is it possible to read the contents of the first file (off the command line) while using "getline" function to read the second file ?

I am having problems with the following syntax

getline < file2

Thanks in advance.

rogers42
 
Hi

rogers42 said:
Is it possible to read the contents of the first file (off the command line) while using "getline" function to read the second file ?

I am having problems with the following syntax

getline < file2
Yes, it is possible. Would be better to see more from your code idea regarding where that line would placed. I think would be better to read those lines into another variable, not just in [tt]$0[/tt] :
Code:
getline [red]mystr[/red] < file2

Feherke.
 
Also make sure that the file is in the current dir or specify full pathname. In any case enclose file name in double quotes like so...

Code:
getline mystr < "file2"
or
Code:
getline mystr < "/path/to/file2"

... and also that you have read persmission on that file2.



HTH,

p5wizard
 
I have placed the following code in the file

{
while (( getline x < "file2.in") > 0)
print x
close("file2.in")
}

I am executing the utility as follows

awk -f multiple_files.awk

The utility complains of the following error

awk: syntax error near line 10
awk: illegal statement near line 10

The input file (file2.in) exists in the same directory and has read permission.

Any ideas as to what I might be doing wrong ?

Thanks in advance.

rogers42

 
Hi

rogers42 said:
I am executing the utility as follows

awk -f multiple_files.awk
And where is its input ?
Code:
[red]BEGIN[/red] {
   while (( getline x < "file2.in") > 0)
            print x
   close("file2.in")
}

[gray]# or[/gray]

awk -f multiple_files.awk [red]/input/file[/red]
By the way, your code works for me.

Feherke.
 
If you want to execute it without specifying an input file on the awk command line, you need to put your code in the BEGIN { } clause like in one of feherke's earlier examples. If you just put it in a { } section awk will assume you are providing some input from stdin.

Annihilannic.
 
And how an awk program of 5 lines (as you posted 9 May 07 12:58) may raise raise an error near line 10 ?
 
Hi PHV,

The line numbers mismatch is becuase I had cut & paste the actual code minus the comments at the begining of the file.

Nice catch though

BTW, I am still struggling with the same syntax error. Others suggestions are more than welcome.

Thanks

rogers42
 
same syntax error
Again, WHICH LINE RAISES THE ERROR ?
 
Hi,

while (( getline x < "file2.in") > 0)

Thanks

rogers42
 
No syntax error on this line for me.
Which flavor/version of awk ?
why not posting the whole content of multiple_files.awk ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
just leave out the "> 0" part

Code:
while ( getline x < "file2.in")


HTH,

p5wizard
 
p5wizard, what happen with your suggestion if file2.in doesn't exist or isn't readable ?
 
PHV,

It loops infinitely reading an empty string... :~/
Serves me right for not testing thoroughly enough.

I omitted some parentheses on my first attempt and also got a syntax error. I then started off on a wild goose chase...

You are right, code as posted by rogers42 should work fine (it does in awk under AIX):

Code:
while (( getline x < "file2.in") > 0)


HTH,

p5wizard
 
I am running awk on Solaris 8

Thanks

rogers42
 
Have you tried nawk instead ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yup, mystery partially solved !!!

"getline" in awk does not take an argument while the same function in nawk does. I.e. my code works in nawk.

I wonder why is the code working for almost all the other users in awk ? Maybe people are using a later version of awk ?

How can I check my awk version ?

Thanks

rogers42



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top