[H-GEN] sed query

Scott Pullen spullen at optusnet.com.au
Wed May 7 08:24:40 EDT 2003


[ Humbug *General* list - semi-serious discussions about Humbug and     ]
[ Unix-related topics. Posts from non-subscribed addresses will vanish. ]


If the placement of the newlines you want inserted is not important
you can probably just use fmt(1):

        $ wc scrlt11.txt
              1   88612  508856 scrlt11.txt
        $ fmt scrlt11.txt | wc
           7160   88612  508086

Using GNU sed I was able to do the following:

        $ wc scrlt11.txt
              1   88612  508856 scrlt11.txt
        $ sed --version
        GNU sed version 4.0.5
        [..]
        $ sed -re's/(.{65}[^ ]*) /\1\n/g' scrlt11.txt | wc
           7384   88612  508856

The -r option turns on extended regular expressions so that the {65}
quantifier will work.  This is undoubtedly an evil GNU extension, so
here is the same thing in a language you can depend on:

        $ perl -e'$_ = <>;
                  while(m/(.{0,65}\S*)\s*/g) {
                      print "$1\n"
                  }' scrlt11.txt

My guess is that your sed pattern is not matching your input, and that
sed is then reading a line, noticing it does not match, and writing
the string unchanged to the output.




Thanks for the replies.  I finally found the newline answer in the O'Reilly
sed & awk book.  It goes something like this.

sed -e 's/SOMETEXT/\
SOMETEXT/g' AFILE > OUTPUTFILE

The newline must be escaped and then entered.  Of course the shell will wait
until the quotes are finished before executing so all is well.  The above
answer worked well on the example text that I created but the real file
still crashes it.  I say crashes because I am getting no output.

I have talked to a number of experienced Admins and all say that this
shouldn't be possible but I can assure you all that it is happening.

To answer one question, the box is a Sunblade 150 running Solaris 8.  I
tried running it on the Sun V880 with 4 Gb RAM running the same OS and it
still didn't work.  I wish that I could provide the file but that would mean
instant dismissal.  Secret Squirrels and all that stuff.

I can only think that there must be some special characters in the file that
I haven't noticed yet that cause this behaviour.  I will try the other
suggestions and see if I can find where in the file sed crashes.  Hopefully
not till the very end, well a couple of bytes short anyway.

Cheers,

Scott.




--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.  See http://www.humbug.org.au/



More information about the General mailing list