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!

sed is eating my null. 1

Status
Not open for further replies.

motoslide

MIS
Oct 30, 2002
764
US
I've got a script (I didn't create) that is being used as an Output filter to convert NL to CRNL. It works fine, but I don't understand it enough to fix an issue.
I have another script that will send a single-byte file to a printer just to wake it up. That single byte is a hex "00" (null).
The CRNL filter below eats up the null and creates a completely EMPTY print stream, of which the spooler refuses to act upon.

Here's the filter/script:
Code:
:
# addcr - filters to add CARRIAGE RETURN to LINEFEED in text
#
#       addcr adds CR to each LF
#
CR=`echo | tr '\012' '\015'`
sed "s/$/$CR/" $*

When i pipe my nullfile to the script, the output is completely empty (as verified using "hd").

Is there a way I can either allow the null to pass unaffected, or set a conditional so this script is only invoked if the data in the pipe is > 2 bytes long?

"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
Hi

I think you will have to give us more details about the circumstances, because that code works for me. Or am I misunderstanding something ?
Code:
[blue]master[/blue] CR=`echo | tr '\012' '\015'`

[blue]master[/blue] perl -e 'print chr 0' | sed "s/$/$CR/" | od -a
0000000 nul  cr
0000002

[blue]master[/blue] perl -e 'print chr 0,"\n"' | sed "s/$/$CR/" | od -a
0000000 nul  cr  nl
0000003

Feherke.
 
Thanks, Feherke.
What is actually happening, is that I have this specified in my /etc/printcap file:

p4tst:\
:lp=:rm=p4a:rp=raw1:sd=/usr/spool/lpd/p4tst:mx#0:eek:f=/usr/local/bin/lp_addcr

This is "lp_addcr":
#!/bin/sh
exec /usr/local/bin/addcr_lpd

The script in my original post is the contents of "addcr_lpd".

This is my experiment:
Code:
$ hd nullfile
0000    00                                                 .
0001
$ cat nullfile |addcr_lpd|hd
0000
$ cat nullfile|hd
0000    00                                                 .
0001


I'm aware of the UUOC infraction(s). It's Friday, I'm feeling exempt.


"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
I'm running SCO (mostly) and little Linux. Are you saying I could incorporate "xtod" (SCO's version of unix2dos) as an output script?
I'll have to give that a try tomorrow.

The script I listed in my first post came from SCO's knowledge-base, so I just figured xtod wouldn't fly for some reason.



"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
AFAIK, xtod adds a Ctrl-Z at the end of the converted file...
 
I end up with the same issue (as PHV expected). When I run the job through xtod, it adds a trailing Ctrl-Z, which I can chop off using sed, but, again, sed eats my null.

I tried piping the output to awk using "^Z" as the field seperator. The result is a loss of the null character, plus the addition of an unwanted LF.

Code:
cat nullfile|xtod|awk -F "^Z" '{ print $1 '}|hd
0000     0a

I came up with a way to accomplish my needs, but it's pretty clunky. I've replace the original "addcr_lpd" with this:

Code:
xtod > /usr/tmp/testfile.$$
SIZE=`wc -m /usr/tmp/testfile.$$|awk '{ print $1 '}`
SIZE=`expr $SIZE - 1`
dd if=/usr/tmp/testfile.$$ bs=1 count=$SIZE 2>/dev/null
rm /usr/tmp/testfile.$$

This adds the "CR" to text files (which I need) and it also allows my 1-byte NULL file to pass through, which is used to restart a hung lpd printer.

I hate the idea of creating an interrim file just to chop off the last byte. Any other ideas?



"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
If perl is available on those systems, why not just use that to do the sediting, since it seems to honour the nulls it is given?

Code:
$ perl -e 'print "hello\nw " . chr(0) . "rld\n";' | perl -pe 's/\n/\r\n/'| od -c
0000000    h   e   l   l   o  \r  \n   w      \0   r   l   d  \r  \n
0000017

Annihilannic.
 
The perl option works perfectly.
Thanks!



"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top