A question for the AWK wizards

2006-07-25 Thread Murray Taylor
Hi all,

I have a shell script which is called with an arbitrary 
message argument. Punctuation excludes * ?  |   chars.

It processes it via an AWK command line 'script' and dumps the 
result in a file for the SMS sender...

Nice and simple.

Except that the AWK script seems to duplicate the last character or two
in the message. Everything else in the 200 odd lines of shell scripts
surrounding this function run just fine, and this bit runs too, 
but this tiny thing is _VERY_ annoying.

The shell code is listed below.

Please teach me what bit I missed  (C and TCL are my forte, not AWK)

cheers
mjt

--8--
# sourced into other scripts that need to SMS
# !! 4 space indents, NOT tabs !!
#
# generate the sms message
# the awk code forces the message to be  160 chars
sendsms() {
msg=$1

case ${sms_enable} in
[Yy][Ee][Ss])
for phone in ${phonelist}
do
tmpfile=`mktemp -t sms`
echo ${phone}  ${tmpfile}
${AWK} '{ printf %-0.159s, $0 }'  ${tmpfile}  EOF2
`echo $msg`
EOF2

mv ${tmpfile} ${gsmspool_dir}
done
;;
*)
;;
esac
}
--8--

Murray Taylor

Special Projects Engineer
Bytecraft Systems

P: +61 3 8710 2555
F: +61 3 8710 2599
D: +61 3 9238 4275
E: [EMAIL PROTECTED] 


--

Any intelligent fool can make things bigger and more complex... It
takes a
touch of genius - and a lot of courage to move in the opposite
direction.
--Albert Einstein 

---
The information transmitted in this e-mail is for the exclusive
use of the intended addressee and may contain confidential
and/or privileged material. Any review, re-transmission,
dissemination or other use of it, or the taking of any action
in reliance upon this information by persons and/or entities
other than the intended recipient is prohibited. If you
received this in error, please inform the sender and/or
addressee immediately and delete the material. 

E-mails may not be secure, may contain computer viruses and
may be corrupted in transmission. Please carefully check this
e-mail (and any attachment) accordingly. No warranties are
given and no liability is accepted for any loss or damage
caused by such matters.
---

***This Email has been scanned for Viruses by MailMarshal.***
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: A question for the AWK wizards

2006-07-25 Thread Giorgos Keramidas
On 2006-07-25 21:43, Murray Taylor [EMAIL PROTECTED] wrote:
 Hi all,

 I have a shell script which is called with an arbitrary message
 argument. Punctuation excludes * ?  |   chars.

 It processes it via an AWK command line 'script' and dumps the result
 in a file for the SMS sender...

 Nice and simple.

 Except that the AWK script seems to duplicate the last character or
 two in the message. Everything else in the 200 odd lines of shell
 scripts surrounding this function run just fine, and this bit runs
 too, but this tiny thing is _VERY_ annoying.

 The shell code is listed below.

 Please teach me what bit I missed  (C and TCL are my forte, not
 AWK)

 --8--
 # sourced into other scripts that need to SMS
 # !! 4 space indents, NOT tabs !!
 #
 # generate the sms message
 # the awk code forces the message to be  160 chars
 sendsms() {
 msg=$1

 case ${sms_enable} in
 [Yy][Ee][Ss])
 for phone in ${phonelist}
 do
 tmpfile=`mktemp -t sms`
 echo ${phone}  ${tmpfile}
 ${AWK} '{ printf %-0.159s, $0 }'  ${tmpfile}  EOF2
 `echo $msg`
 EOF2
 mv ${tmpfile} ${gsmspool_dir}
 done
 ;;
 *)
 ;;
 esac
 }

The above has a weird construct which can be simplified a bit:

| ${AWK} '{ printf %-0.159s, $0 }'  ${tmpfile}  EOF2
| `echo $msg`
| EOF2

You can write this as:

|  echo ${msg} | ${AWK} '{printf %-0.159s, $0}'  ${tmpfile}

Are you deliberately avoiding to append a newline character to the
output of ${AWK} above?  See the output of the two commands below,
as it's filtered through hd(1) utility.

| $ echo foo | awk '{ printf %-0.159s, $0 }' | hd
|   66 6f 6f  |foo|
| 0003
| $ echo foo | awk '{ printf %-0.159s\n, $0 }' | hd
|   66 6f 6f 0a   |foo.|
| 0004
| $

There is no problem with this part of the scripts you posted though.
They should work as expected.  I'd probably look elsewhere for a bug
that causes the character duplication.

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: A question for the AWK wizards

2006-07-25 Thread Parv
in message [EMAIL PROTECTED],
wrote Murray Taylor thusly...

 # generate the sms message
 # the awk code forces the message to be  160 chars
...
tmpfile=`mktemp -t sms`
echo ${phone}  ${tmpfile}
${AWK} '{ printf %-0.159s, $0 }'  ${tmpfile}  EOF2
 `echo $msg`
 EOF2

As it is, any line longer than 159 characters will just overflow.
You need to use substr() not awk to shorten a line.  Even after that
modification, that won't solve your actual problem as the awk script
will just shorten EACH line (when record separator is newline), not
the whole output.

There are ports which seems to do what you want to do.


  - Parv

-- 

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: A question for the AWK wizards

2006-07-25 Thread David Kelly
On Tue, Jul 25, 2006 at 10:51:02AM -0400, Parv wrote:
 
 As it is, any line longer than 159 characters will just overflow.
 You need to use substr() not awk to shorten a line.  Even after that
 modification, that won't solve your actual problem as the awk script
 will just shorten EACH line (when record separator is newline), not
 the whole output.
 
 There are ports which seems to do what you want to do.

Ports? How about dd? Should be as simple as piping it thru
dd count=159 but then again I admit to having not paid full attention
to this thread.

-- 
David Kelly N4HHE, [EMAIL PROTECTED]

Whom computers would destroy, they must first drive mad.
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: A question for the AWK wizards

2006-07-25 Thread Parv
in message [EMAIL PROTECTED],
wrote Parv thusly...

 You need to use substr() not awk to shorten a line.
` ^ ^ ^ ^ ^ ^ ^ ^
` ^ ^ ^ ^ ^ ^ ^ ^
Dang it!  I meant to use the substr() function in awk.



  - Parv

-- 

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]