For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2007 > Logic problem in script.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Logic problem in script.
Shelagh

2007-03-03, 3:57 am

I have a database file of the same form as the /etc/password file.
When I want to print it out I have an awk script which formats it for
me.
Mostly I have used the script to learn awk as much as anything, but
one thing I can't get right.
The last field is a "notes" field and can get quite long. I had a way
of parsing the words so that only a certain number of words were
printed on each line, but I have been trying to make the script be
more flexible. I works up to a certain point but misses one word out
It's probably just a logic thing, but I was wondering if someone could
pass an eye over the script to see what I've done wrong.

Actually, it's an awk script embedded in a bash script.

awkward='BEGIN { FS = ":" ; print "NAME" "\t\t" "CONTACT DETAILS" ;
print "****" "\t\t" "******* *******" }
{ print $2 }
$3 ~ /[0-9]+/ { print "\t\t Phone: " $3 }
$4 ~ /[0-9]+/ { print "\t\t Mob: " $4 }
$5 ~ /.+/ { print "\t\t Email: " $5 }
$6 ~ /.+/ {
printf( "\t\t Notes: " )
maxonline = 40
words = split( $6, notes, " " ) #make an array of $6 called notes
chars = ( length( notes[x] ) + 1 ) #count the number of chars in words
plus space
for ( x=1; x <= words; x++ ) {
if( chars >= maxonline ) {
printf( "\n\t\t " ) #start a newline with tabs
chars = 0 #and start the count over again.
}
else {
printf( notes[x] " " ) #print each element up to the xth.
chars += ( length( notes[x] ) + 1 )
}
}
printf "\n"
}'

awk -F"[: ]" '{ print $2 ":" $0 }' .addresses | sort |
awk "$awkward"


cat `/.address
Pleasant People:44444444:::
Wiley Coyote:42269709:06178091111:friend@email
.address:Short note.
Nice Person:22222222::someone@another.email.address:Very long sentence
which will test the script I wrote. I hope that it works! But if it
doesn't the old one will have to do.
Someone Else:::someone@another.email.address:no comment

shelagh@pandora:~$ hsnew
NAME CONTACT DETAILS
**** ******* *******
Wiley Coyote
Phone: 42269709
Mob: 06178091111
Email: friend@email.address
Notes: Short note.
Someone Else
Email: someone@another.email.address
Notes: no comment
Pleasant People
Phone: 44444444
Nice Person
Phone: 22222222
Email: someone@another.email.address
Notes: Very long sentence which will test the
I wrote. I hope that it works! But if it doesn't the
one will have to do.

If you notice the first word of the new line is missing. I've pored
over the script and just can't spot where the problem is.

Shelagh

Vassilis

2007-03-03, 6:57 pm


=CF/=C7 Shelagh =DD=E3=F1=E1=F8=E5:
> I have a database file of the same form as the /etc/password file.
> When I want to print it out I have an awk script which formats it for
> me.
> Mostly I have used the script to learn awk as much as anything, but
> one thing I can't get right.
> The last field is a "notes" field and can get quite long. I had a way
> of parsing the words so that only a certain number of words were
> printed on each line, but I have been trying to make the script be
> more flexible. I works up to a certain point but misses one word out
> It's probably just a logic thing, but I was wondering if someone could
> pass an eye over the script to see what I've done wrong.
>
> Actually, it's an awk script embedded in a bash script.
>
> awkward=3D'BEGIN { FS =3D ":" ; print "NAME" "\t\t" "CONTACT DETAILS" ;
> print "****" "\t\t" "******* *******" }
> { print $2 }
> $3 ~ /[0-9]+/ { print "\t\t Phone: " $3 }
> $4 ~ /[0-9]+/ { print "\t\t Mob: " $4 }
> $5 ~ /.+/ { print "\t\t Email: " $5 }
> $6 ~ /.+/ {
> printf( "\t\t Notes: " )
> maxonline =3D 40
> words =3D split( $6, notes, " " ) #make an array of $6 called notes
> chars =3D ( length( notes[x] ) + 1 ) #count the number of chars in words


Above there might be a problem, cause x has no value yet.
Maybe you intended, maybe you didn't.

> plus space
> for ( x=3D1; x <=3D words; x++ ) {
> if( chars >=3D maxonline ) {
> printf( "\n\t\t " ) #start a newline with tabs
> chars =3D 0 #and start the count over again.


Change the two lines above with:

printf ("\n\t\t %s ", notes[x])
chars =3D length( notes[x] ) + 1

You' re forgetting to print the element at hand.

> }
> else {
> printf( notes[x] " " ) #print each element up to the xth.
> chars +=3D ( length( notes[x] ) + 1 )
> }
> }
> printf "\n"
> }'
>
> awk -F"[: ]" '{ print $2 ":" $0 }' .addresses | sort |
> awk "$awkward"
>
> Shelagh


loki harfagr

2007-03-03, 6:57 pm

On Sat, 03 Mar 2007 00:37:56 -0800, Shelagh wrote:

> I have a database file of the same form as the /etc/password file. When
> I want to print it out I have an awk script which formats it for me.
> Mostly I have used the script to learn awk as much as anything, but one
> thing I can't get right.
> The last field is a "notes" field and can get quite long. I had a way of
> parsing the words so that only a certain number of words were printed on
> each line, but I have been trying to make the script be more flexible. I
> works up to a certain point but misses one word out It's probably just a
> logic thing, but I was wondering if someone could pass an eye over the
> script to see what I've done wrong.
>
> Actually, it's an awk script embedded in a bash script.
>
> awkward='BEGIN { FS = ":" ; print "NAME" "\t\t" "CONTACT DETAILS" ;
> print "****" "\t\t" "******* *******" } { print $2 }
> $3 ~ /[0-9]+/ { print "\t\t Phone: " $3 } $4 ~ /[0-9]+/ { print "\t\t
> Mob: " $4 } $5 ~ /.+/ { print "\t\t Email: " $5 } $6 ~ /.+/ {
> printf( "\t\t Notes: " )
> maxonline = 40
> words = split( $6, notes, " " ) #make an array of $6 called notes chars
> = ( length( notes[x] ) + 1 ) #count the number of chars in words plus
> space
> for ( x=1; x <= words; x++ ) {
> if( chars >= maxonline ) {
> printf( "\n\t\t " ) #start a newline with tabs chars = 0 #and start the
> count over again.
> }


There it is!
Reserve the word you're turning line on :
--------->>
if( chars >= maxonline ) {
# start a newline with tabs
printf( "\n\t\t " )
# start the count over again.
chars = 0
# and save the last dance for next round
x--
}
<<---------



> else {
> printf( notes[x] " " ) #print each element up to the xth. chars += (
> length( notes[x] ) + 1 )
> }
> }
> printf "\n"
> }'
>
> awk -F"[: ]" '{ print $2 ":" $0 }' .addresses | sort | awk "$awkward"
>
>
> cat `/.address
> Pleasant People:44444444:::
> Wiley Coyote:42269709:06178091111:friend@email
.address:Short note. Nice
> Person:22222222::someone@another.email.address:Very long sentence which
> will test the script I wrote. I hope that it works! But if it doesn't
> the old one will have to do.
> Someone Else:::someone@another.email.address:no comment
>
> shelagh@pandora:~$ hsnew
> NAME CONTACT DETAILS
> **** ******* *******
> Wiley Coyote
> Phone: 42269709
> Mob: 06178091111
> Email: friend@email.address
> Notes: Short note.
> Someone Else
> Email: someone@another.email.address
> Notes: no comment
> Pleasant People
> Phone: 44444444
> Nice Person
> Phone: 22222222
> Email: someone@another.email.address
> Notes: Very long sentence which will test the I wrote.
> I hope that it works! But if it doesn't the one will
> have to do.
>
> If you notice the first word of the new line is missing. I've pored over
> the script and just can't spot where the problem is.
>
> Shelagh

Shelagh

2007-03-03, 6:57 pm

On Mar 4, 2:23 am, loki harfagr <l...@DarkDesign.free.fr> wrote:
[snip]


>
> There it is!
> Reserve the word you're turning line on :
> --------->>
> if( chars >= maxonline ) {
> # start a newline with tabs
> printf( "\n\t\t " )
> # start the count over again.
> chars = 0
> # and save the last dance for next round
> x--}
>
> <<---------


Thank you both very much. As usual when logic is pointed out, it
becomes obvious.

Shelagh

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com