lf/cr affecting awk script?

  • Follow


dear all,

i have come across a strange/unexpected behaviour of my awk script and i 
was hoping maybe someone could shed a bit of light on this issue.

i have a large number of files from which i extract data using the 
following script:

(FNR==1) {found=0}
($1=="Hold") {output=$3}
($1 ~ /\*/ && found==0) {print output; found=1}

this works well and produces 1 line of output for every file. now i'd 
like to have the filename in addition to the $3 (number). so i changed 
the last line to

($1 ~ /\*/ && found==0) {print output FILENAME; found=1}

which again seems to work nicely. for the first half of the files it 
produces nicely the expected output. however, suddenly it stops printing 
$3 and each time i only get the the filename printed. i checked by hand 
and it should work on all files (=should print $3 for all files).

to verify this i changed the last line, this time to

($1 ~ /\*/ && found==0) {print output; print FILENAME; found=1}

and then i get the result for all files, but now on two lines.

the only difference i was able to find between the input files is that 
some seem to have cr/lf (or any combination thereof) at the end of the 
lines. as this is the only difference i could find i assume this to be 
the cause for this.

could anybody explain why the cr/lf so strongly affects the behaviour of 
the script?

thanks for any help,

tom



0
Reply Thomas 6/23/2005 6:53:33 AM

ok, i've found the problem.

awk was actually doing everything fine, just as it should.

the problem was that my editor interpreted the cr/lf in the middle of 
the line as beginning of the line, not showing the $3 value printed 
before it.

....and awk still stands strong...

cheers,

tom

Thomas Toth wrote:
> dear all,
> 
> i have come across a strange/unexpected behaviour of my awk script and i 
> was hoping maybe someone could shed a bit of light on this issue.
> 
> i have a large number of files from which i extract data using the 
> following script:
> 
> (FNR==1) {found=0}
> ($1=="Hold") {output=$3}
> ($1 ~ /\*/ && found==0) {print output; found=1}
> 
> this works well and produces 1 line of output for every file. now i'd 
> like to have the filename in addition to the $3 (number). so i changed 
> the last line to
> 
> ($1 ~ /\*/ && found==0) {print output FILENAME; found=1}
> 
> which again seems to work nicely. for the first half of the files it 
> produces nicely the expected output. however, suddenly it stops printing 
> $3 and each time i only get the the filename printed. i checked by hand 
> and it should work on all files (=should print $3 for all files).
> 
> to verify this i changed the last line, this time to
> 
> ($1 ~ /\*/ && found==0) {print output; print FILENAME; found=1}
> 
> and then i get the result for all files, but now on two lines.
> 
> the only difference i was able to find between the input files is that 
> some seem to have cr/lf (or any combination thereof) at the end of the 
> lines. as this is the only difference i could find i assume this to be 
> the cause for this.
> 
> could anybody explain why the cr/lf so strongly affects the behaviour of 
> the script?
> 
> thanks for any help,
> 
> tom
> 
> 
> 
0
Reply Thomas 6/23/2005 8:01:59 AM


1 Replies
167 Views

(page loaded in 0.037 seconds)

Similiar Articles:













7/30/2012 10:15:53 AM


Reply: