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!

USING DIFFERENT FILES WITHIN THE SAME AWK SENTENCE

Status
Not open for further replies.

holaqtal

Programmer
May 6, 2003
7
ES
all right, here's the problem

awk ' {
FILENAME=="myfile2"{
print $1
}
print $1
}' myfile1

so the two prints should be different. The main problem is that i don't know if i can modify the FILENAME variable like that or if the error is in the syntaxis, or what could i do to achieve this, help, help, thanks.
 
I think you want

awk ' {
print $1 > "myfile2"
print $1
}' myfile1


CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
sorry, that's not it. I'm pretty certain i have to use the FILENAME variable somehow.

what you're doing is sending the first field of "myfile1" and writing it into "myfile2" and then printing the first field of "myfile1".
What i want to do is be able to modify one field of "myfile2" inside the awk statement (which already has a file to work with).

Thanks anyway.
 
I don't really undrestand what you want. Maybe

{
a = $1
getline < &quot;myfile2&quot;
print $1
print a
}

If this is not it, post examples of myfile1 and myfile2 and the output you expect.

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Hey, thanks Cakiwi. That's exactly what i needed, i didn't realize that when calling getline I could print just a field from that line, i thought i had to print the whole thing

Thanks again
 
Glad it worked for you. By default, getline reads data into the $0 variable and splits it into fields but you can also read data into a variable and split it yourself. e.g.

{
getline a < &quot;myfile2&quot;
n = split(a,b,FS)
print b[1]
print $1
}

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top