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.