Length of Longest Line

  • Follow


Given a file with lines of text of different length:

How to find out the length of the LONGEST line in the file ?
(no need to know WHICH)

Thanks
<5.8 sun4u sparc>
0
Reply klabu 7/29/2008 2:07:03 AM

klabu wrote:
> Given a file with lines of text of different length:
> 
> How to find out the length of the LONGEST line in the file ?
> (no need to know WHICH)
> 
> Thanks
> <5.8 sun4u sparc>

Why do I suspect that this is a homework problem?
0
Reply Richard 7/29/2008 2:14:49 AM


On Jul 28, 10:14 pm, "Richard B. Gilbert" :
> Why do I suspect that this is a homework problem?


no no Rich I swear..
it's not HW...(LOL I wish I were that young !)
100% real-life need-to-know. I know Linux's "wc -L" - too bad this
version of our Solaris doesn't have it.
I'm just pretty good at keeping Internet postings as short and as
precise as possible.

0
Reply klabu 7/29/2008 2:46:04 AM

On Mon, 28 Jul 2008 19:07:03 -0700 (PDT), klabu <klabu76@gmail.com> wrote:
|
| Given a file with lines of text of different length:
|
| How to find out the length of the LONGEST line in the file ?
| (no need to know WHICH)
|
| Thanks
| <5.8 sun4u sparc>


This works in bash on Linux and Solaris (at least the versions I tested
it on).


cat filename | while read Line
do
    echo $Line | wc -c
done | sort -n | tail -1


Note: This will count the trailing newline character as well as all the
text.


-- 
Reverend Paul Colquhoun, ULC.    http://andor.dropbear.id.au/~paulcol
     Asking for technical help in newsgroups?  Read this first:
        http://catb.org/~esr/faqs/smart-questions.html#intro
0
Reply Paul 7/29/2008 5:49:06 AM

klabu wrote:
> On Jul 28, 10:14 pm, "Richard B. Gilbert" :
>> Why do I suspect that this is a homework problem?
> 
> 
> no no Rich I swear..
> it's not HW...(LOL I wish I were that young !)
> 100% real-life need-to-know. I know Linux's "wc -L" - too bad this
> version of our Solaris doesn't have it.
> I'm just pretty good at keeping Internet postings as short and as
> precise as possible.
> 


/usr/gnu/bin/wc ???


/Thommy M.
-- 
/"\  Join the ASCII    | Thommy M. Malmstr�m
\ /  ribbon campaign   | AB Hardeberga IT
 X   against HTML mail | Skifferv�gen 23, 224 78 Lund
/ \  and postings      | +46 70 969 57 93
=======================================================================
http://www.at-hardeberga.com             thommym (at) at-hardeberga.com
=======================================================================
"The box said 'Windows 2000 Server or better', so I installed Solaris."
=======================================================================
0
Reply ISO 7/29/2008 6:14:15 AM

klabu wrote:
> Given a file with lines of text of different length:
> 
> How to find out the length of the LONGEST line in the file ?
> (no need to know WHICH)

do you have perl ?

    BugBear
0
Reply bugbear 7/29/2008 9:23:25 AM

klabu wrote:
> On Jul 28, 10:14 pm, "Richard B. Gilbert" :
>> Why do I suspect that this is a homework problem?
> 
> 
> no no Rich I swear..
> it's not HW...(LOL I wish I were that young !)
> 100% real-life need-to-know. I know Linux's "wc -L" - too bad this
> version of our Solaris doesn't have it.
> I'm just pretty good at keeping Internet postings as short and as
> precise as possible.
> 

If you only need to do it once, eyeball it!  There may be some tool that 
does this but, if so, I don't know what it is.

If it's a recurring problem you'll have to write a script or a program 
to read the file, one line at a time, count the characters, and save the 
  value if the length is greater than the previous longest line.  That's 
very standard find the minimum/maximum and why I suspected it was a 
student problem.  Don't forget to initialize your maximum to zero.

0
Reply Richard 7/29/2008 11:42:17 AM

klabu <klabu76@gmail.com> wrote in news:7e70f1ae-d8ef-4a45-b50d-
585409f52a86@27g2000hsf.googlegroups.com:

> On Jul 28, 10:14 pm, "Richard B. Gilbert" :
>> Why do I suspect that this is a homework problem?
> 
> 
> no no Rich I swear..
> it's not HW...(LOL I wish I were that young !)
> 100% real-life need-to-know. I know Linux's "wc -L" - too bad this
> version of our Solaris doesn't have it.
> I'm just pretty good at keeping Internet postings as short and as
> precise as possible.
> 

Assuming that the host has perl on it, you could do:

  perl -ne '$ll = length if length > $ll; END {print $ll}' file

where 'file' is the file whose longest line you would like to 
determine.

....Steve

-- 
Steve van der Burg
Technical Analyst, Information Services
London Health Sciences Centre
London, Ontario, Canada
Email: steve.vanderburg@lhsc.on.ca
0
Reply Steve 7/29/2008 12:51:00 PM

On Jul 29, 2:14 am, "Thommy M. Malmstr=F6m" <thom...@at-hardeberga.com>
wrote:
> /usr/gnu/bin/wc ???


bash-2.03$ ls /usr/gnu
/usr/gnu: No such file or directory
0
Reply noobuntu 7/29/2008 2:59:37 PM

On Jul 29, 8:51 am, Steve van der Burg wrote:
> Assuming that the host has perl on it, you could do:
>
>   perl -ne '$ll = length if length > $ll; END {print $ll}' file


bash-2.03$ perl -ne '$ll = length if length > $ll; END {print $ll}'
cp_3233884.lst
4191340bash-2.03$

thanks! that's correct the length is 4191340 --- how to put the shell
prompt on newline?
0
Reply noobuntu 7/29/2008 3:05:52 PM

On Tue, 29 Jul 2008 07:59:37 -0700, noobuntu wrote:
> On Jul 29, 2:14 am, "Thommy M. Malmstr�m" <thom...@at-hardeberga.com>
> wrote:
>> /usr/gnu/bin/wc ???

> bash-2.03$ ls /usr/gnu
> /usr/gnu: No such file or directory

Present starting with Solaris Express (Nevada).

0
Reply Dave 7/29/2008 3:38:20 PM

noobuntu <webtourist@gmail.com> wrote:
> On Jul 29, 8:51 am, Steve van der Burg wrote:
>> Assuming that the host has perl on it, you could do:
>>
>>   perl -ne '$ll = length if length > $ll; END {print $ll}' file
> 
> 
> bash-2.03$ perl -ne '$ll = length if length > $ll; END {print $ll}'
> cp_3233884.lst
> 4191340bash-2.03$
> 
> thanks! that's correct the length is 4191340 --- how to put the shell
> prompt on newline?

Either Add a -l to perl   $ perl -lne 'blah....     or

change ->print $ll<-  to ->print "$ll\n"<-

perl -lne 'blah...

And your answer depends on whether or not you want to count the newline
on the input file as part of the length.  If you use -l, it does not
count the newline.  Without the -l it does.

-- 
Darren
0
Reply ddunham 7/29/2008 4:15:51 PM

11 Replies
707 Views

(page loaded in 0.574 seconds)

Similiar Articles:


















7/20/2012 2:29:51 PM


Reply: