OK -- if there *IS* a longest line and you're not determined to use
wc -L and regexps, you could instead do:
perl -ne '$max = length($long = $_) if length($_) >$max; END{print $long}' file
If two lines have the same length, this would print the first of them. If
you changed the condition to ">=", it would print the second of them.
Printing all the lines with the max length would be a bit trickier,
but not difficult -- you'd need both an equality test and a greater-than
test. ... something like this:
--------------------
#!/usr/bin/env perl
while (<>) {
if (($len = length($_)) > $lmax) {
$lmax = $len;
@longest =($_);
}
elsif ($len == $lmax) {
push(@longest, $_);
}
}
print @longest;
--------------------
Note that this scheme makes only one pass through the file in either case.
pete
pete peterson
GenRad, Inc.
7 Technology Park Drive
Westford, MA 01886-0033
[EMAIL PROTECTED] or [EMAIL PROTECTED]
+1-978-589-7478 (GenRad); +1-978-256-5829 (Home: Chelmsford, MA)
+1-978-589-2088 (Closest FAX); +1-978-589-7007 (Main GenRad FAX)
> Date: Thu, 31 Aug 2000 21:02:59 -0600
> To: [EMAIL PROTECTED]
> From: Eric Sisler <[EMAIL PROTECTED]>
> Subject: Re: Finding & printing the longest line - thanks
>
>
> rpjday <[EMAIL PROTECTED]> wrote:
>
> >On Thu, 31 Aug 2000, Eric Sisler wrote:
> > > I'm trying to find & print (dump to a file) the longest line in a text
> > > file. I've been able to determine the *length* of the longest line by
> > > using the 'wc -L' command, but is there any way using find or some other
> > > command to *output* the longest line?
>
> >
> >maxlen=$(wc -L filename)
> >grep "^.\{$maxlen\}$" filename
> >
> >note that this prints *all* lines of the max length.
>
> I had to use egrep instead of grep and although the "maxlen" variable is a
> neat idea, wc puts the filename at the end of the results, so the value of
> "maxlen" wound up being "2096722 adcontroller.txt". Oh well, simple enough
> to just plug the number into grep's regular expression, once I figured out
> what you were doing with it. I've been reading "mastering regular
> expressions" but apparently haven't gotten to using {}'s yet. (That or it
> didn't sink in.) I think my biggest problem now will be the length of the
> line in question (2,096,772 chars) and the sheer size of the file
> (~314Mb). I may see if re-arranging the regex helps speed thing up a bit.
>
> Thanks for pointing me in the right direction - I thought their *must* be a
> way to do it with grep (rather than find which I initially wrote by
> mistake), I just didn't have the syntax. Time to read more of the regular
> expressions book.
>
> -Eric
>
>
> Eric Sisler
> Library Computer Technician
> Westminster Public Library
> Westminster, CO, USA
> [EMAIL PROTECTED]
_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list