It is currently Tue, 07 Dec 2021 01:41:58 GMT



 
Author Message
 a SED script to find length of the longest line in a file

Hi,

  The following SED script will find

  the length of the longest line in a file.

  If you find any flaw, please tell me.

  Thanks you very much.

                                                Sincerely,
                                                 Roger Chang

#--------------------------------------------------------
# Hold Space = the length of the longest line examined
#              so far, where number N is represented as
#              N consecutive '#'s
#--------------------------------------------------------
x
G
#------------------------------------------------
# substitute all characters except '\n' with '#'
#------------------------------------------------
s/[^
]/#/g
#----------------------------------------------------------
# Pattern Space = (max length) \n (length of current line)
#----------------------------------------------------------
# keep the larger one of (max length so far) and
# (length of current line)
#------------------------------------------------
s/\(#*\)\n\1/\1/
x
$!d
#---------------------------------------------------------
# If this line is the last one,
#  then convert (max length) from unary to decimal
#  by simulating division-by-ten. You can visit
#   http://www.**-**.com/ ~gis84806/sed-script-e.html
#  for details.
#---------------------------------------------------------
x
:loop1
/^##########/{
s/^/\
/
:loop2
s/\n##########/#\
/
/\n##########/b loop2
s/\n/ /
b loop1
s/#####/5/g
s/####/4/g
s/###/3/g
s/##/2/g
s/#/1/g
s/54/9/g
s/53/8/g
s/52/7/g
s/51/6/g
s/$/ /
s/ /0/g
s/\([1-9]\)0/\1/g



 Fri, 09 Jun 2000 03:00:00 GMT   
 a SED script to find length of the longest line in a file

Great! How about a simple awk script ?
like
awk -f len.awk <yrfile>

len.awk:
{ #printf "%5d [%3d] %s\n",  NR, length($0), $0;
if ( length($0) > n ) {
          n=length($0); nl=NR; line=$0;
  }
END { printf "Longest line is:\n%5d [%3d] %s\n", nl, n, line; }

--
bye, nram



 Fri, 09 Jun 2000 03:00:00 GMT   
 a SED script to find length of the longest line in a file

Even simpler... keep it all in shell :

cat datafile | script

Where script is :

#!/bin/sh
while read line ; do
   cline=`echo $line | wc -c`
   [ "$cline" -gt "$mline" ] && mline=$cline
done
echo $mline

--
Ian

======================================================================
Ian Goldstein                               | E-Mail nny...@ny.ubs.com
Enterprise Systems Management               |
On assignment at Union Bank of Switzerland  |
======================================================================



 Sat, 10 Jun 2000 03:00:00 GMT   
 a SED script to find length of the longest line in a file

[deleted...]

 Why I wrote this script is just to show that SED can do it.

 Writing scripts like this will make me much more familiar with

 regular expressions.

 SED can do a lot of jobs beyond your imagination.

 You can visit http://www.wollery.demon.co.uk

 the seder official page for more information.

                                                       Roger Chang



 Sun, 11 Jun 2000 03:00:00 GMT   
 a SED script to find length of the longest line in a file

Yao-Jen Chang (Roger) <gis84...@cis.nctu.edu.tw> wrote:

:>  Why I wrote this script is just to show that SED can do it.
:>  
:>  Writing scripts like this will make me much more familiar with
:>  regular expressions.
:>  SED can do a lot of jobs beyond your imagination.
:>  You can visit http://www.wollery.demon.co.uk
:>  the seder official page for more information.

That's all fine and dandy but please don't start to become like
    af...@torfree.net (Al Aab)
You would then become the second in my killfile.

There is no need to spam newsgroups with how great sed is.  It's much more
productive to offer a sound sed answer to questions posted - but only
when sed is the correct tool for the job.  I would not consider sed to
be the proper tool to figure out the length of the longest line in a file,
and I think most would agree with me on that one.

Shawn

--
I feel like a wet parking meter on Darvon!
                                -- Zippy the Pinhead



 Sun, 11 Jun 2000 03:00:00 GMT   
 a SED script to find length of the longest line in a file

Hi Roger,

One can use nawk for finding the length of the longest line in the file
"input_file" as follows.

nawk -v max_length=0 -v cur_rec_num =0 \
          '{ cur_rec_num++ ;
             temp=length($0) ;
             if ( temp > max_length ) max_length = temp ;
             if ( cur_rec_num == NR )  print max_length ;
           }'  input_file

Regards,

Atul



 Sun, 11 Jun 2000 03:00:00 GMT   
 a SED script to find length of the longest line in a file

In article <#4pZSKNE9GA.57@upnetnews03>,

Rather than dealing with NR, use END {print max_length}
and since variables start at 0, forget the initialization.
I don't know what the -v does, it's not on my man page for nawk.

try this:

awk '{t=length($0);if (t>m) m=t} END {print m}' input_file

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,
  Stay Fit       |   @ @   | If a man waited to do it so well,
  Die Anyway     |    v    | That no one could find fault with it.
  de...@tiac.net |  \___/  | http://www.tiac.net/users/demas



 Mon, 12 Jun 2000 03:00:00 GMT   
 a SED script to find length of the longest line in a file

Hi Cahrles,

-v is a mechanism for passing variables to nawk or initializing the
varaibles (the way I have used).

Agreed this is compact.

Happy Christmas and Happy Holiday.

Regards,

Atul



 Mon, 12 Jun 2000 03:00:00 GMT   
 a SED script to find length of the longest line in a file

 If combined with some shell tools, the sed script can be simplified to

 s/./#/g
 x
 G
 s/\(#*\)\n\1/\1/
 x
 $!d
 x

 This will output length of the longest line in unary,

 that is, number N is expressed as N consecutive '#'s.

 Save this script in s,

 expr `sed -f s file_to_be_examined | wc -c` - 1

 we get the length.

                                                          Roger Chang



 Mon, 12 Jun 2000 03:00:00 GMT   
 
   [ 9 post ] 

Similar Threads

1. find length longest line in a file

2. SED: Converting 5 line script into 1 line script

3. sed expression for truncating line length

4. sed - same length for all lines

5. SED & line lengths

6. sed Max line length?

7. long line length causes grep crash

8. . sed script: keep 1 line, delete next 7 lines

9. : split lines in long 'sed'-pattern ?


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software