how to fix a unix shell script to work on linux
Kurt Granroth
kurt+plug-discuss at granroth.com
Sat Jan 12 21:59:09 MST 2008
Josef Lowder wrote:
> The following script used to work on my old unix system,
> to remove blank lines from a text file. It no longer works
> on linux, but now it gives an error message (below):
>
> sed '/[!-~]/!d' $1
>
>
> This is the error message:
>
> sed: -e expression #1, char 7: Invalid range end
>
> How can I fix this?
Hmm... this is a puzzle. First, as others have noted, this snippet
doesn't just delete blank lines... it deletes any line that doesn't
contain at least one 'graph' character (all printable chars except a space).
Given that as a constraint, this new-fangled sed syntax does exactly
what you have above:
sed '/[[:graph:]]/!d' $1
But let's examine what you do have. When I run this exact command using
GNU sed 4.1.5, *all* lines are deleted, not just blank ones. I then
played around with it a bit and substituted chars for the ! going up ("
then # then $, etc). All deleted all lines until I got to this:
sed '/0-~/!d'
sed: -e expression #1, char 7: Invalid range end
Interesting! Likewise, working backwards from the ~, I get the exact
same error message until I have this:
sed '/0-z/!d'
That finally works. What gives, though? That implies that non
alphanumeric chars cannot be used in a range... which is just wrong
since, for instance, [[:graph:]] is a direct substitute for [!-~]. Or
maybe it really is just those final chars:
sed '/[!-z]/!d'
That worked just fine. So it's {, |, }, and ~ that are messing with the
range.
I then tried this:
sed '/!-\x7a/!d'
That worked just like !-z as I would expect. I then replace the tilde
with 7e (its hex):
sed '/!-\x7e/!d'
Nothing... it deletes every line. Replacing ! with 0:
sed '/0-\x7e/!d'
sed: -e expression #1, char 10: Invalid range end
That really smacks of a GNU sed bug to me. I just tend to think that
something like this would have been discovered a long time ago and so
perhaps we're just using it incorrectly.
Any sed expert want to comment?
Kurt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : http://lists.PLUG.phoenix.az.us/pipermail/plug-discuss/attachments/20080112/44a80143/attachment.pgp
More information about the PLUG-discuss
mailing list