Line with maximum no . of characters

Hey , I want to check the row in a file with maximum characters .
the output sud contain that row number along with the no of characters.

You could write a shell script which would have the algorithm to output maximum count

  1. Start reading each line. ( in for loop, or while loop.. or in awk what ever u feel easier and depening uopn size of file)
  2. use wc -c to count # of characters. store it in a variable
  3. compare with previous value.
  4. AT the end display the largest value.
    While storing # of charachters store its line # too in one variable and finally you would have ur maximum character with its line #.

use this sample code

cat txt1
2006007 20001 AR 2502_TXT
2006007 20001 AU 2502_TXT
2006007 20001 CL 2502_TXT
2006007 20001 CO 2502_TXT1
2006007 20001 ES 2502_txta

code

line_num=0
max_cnt=0
pre_cnt=0
pos=0
while read line
do
pre_cnt=`echo $line| wc -c`
pos=$(($pos+"1"))
if [[ $pos -eq 1 ]] then
max_cnt=$pre_cnt
line_num=$pos
else
if [[ $pre_cnt -gt $max_cnt ]] then
max_cnt=$pre_cnt
line_num=$pos
fi
fi
done < txt1

echo "Line # \t Maximum"
echo "$line_num \t $max_cnt"

known bug: If tow line has maximum count it will print only the last line

--Manish

awk ' length > max { max=length;row=NR } END{ print row" "max}' file

In Python:

num = 0;lnum = 0; l = ''
for linenumber , line in enumerate(open("mike.txt")):
     line = line.strip()
     if len(line) >= num:
        num = len(line)
        lnum = linenumber
        l = line
print  num, linenumber ,l

#! /usr/bin/perl
use strict;

$file='max_char'; #input file

open (FH, $file) || die "Error in Opening:$file file\n";
my $count;
my $len;
my $read;
my $max;
my $line;
while ($read = <FH>) {
$count++;
$len=length($read);
if ($len > $max) {
$max = $len;
$line = $count;
}
}
print "Max char found:$max in line number: $line\n";