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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

first line print problem 1

Status
Not open for further replies.

lgtshadow

Technical User
Dec 31, 2003
5
US
I'm quite new to awk and I'm having a problem i don't understand. Using the following I always seem to get the first like of my file printing out (which i don't want).

[tim]$ gawk '/Pre/{Pre=$0;i=0;next} NF{if(!i)print;++i;next}' test/30-b18-a.txt
invalid command name &quot;nu&quot; <-- first line of input file that i don't want
221.136
344.918
362.138
390.118

I think i've narrowed this down to that fact that NF is grabbing the first line in the file and outputting that and then moving on to do the search that i want and outputting my values. Is this correct? If so how do i fix it?


If it matters here's a short sampling of the input file:
[tim]$ more b20/30-b20-a.txt
invalid command name &quot;nu&quot;
You are now editing in 'ft'. 1 ft = 304.8 mm
ae 0 -30
Auto-tolerance is 5.000000e-03
Presented area from this viewpoint, square ft:
221.136

ae 45 -30
Auto-tolerance is 6.000000e-02
Presented area from this viewpoint, square ft:
344.918


I'd appreciate any help you guys could offer. So far I've found this place very helpful in learning, it always amazes me how many nice people there are out there that share their knowledge with other.

Thanks in advance
tim
 
The problem is that the initial value of i is zero, so the test (!i) returns true for the first line. One solution is to reverse the value of i
Code:
/Pre/{i=1;next} NF{if(i)print;i=0}
Setting the variable Pre to zero doesn't seem to have any use, so I removed it.


CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
> The problem is that the initial value of i is zero, so the test (!i) returns
> true for the first line. One solution is to reverse the value of i

Thank you that solves the problem.

> Setting the variable Pre to zero doesn't seem to have any use, so I
> removed it.
Yes sorry about that i meant to remove that but i forgot. That's for something else that is working correctly already. I left out parts of my original awk line that i already had working to try and simplify the problem that i was having. But i do appreciate you pointing that out aswell.

tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top