The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
handling maximum number characters in an input file chrysSty UNIX and Linux Applications 1 05-12-2008 07:19 AM
help on ksh and sql..getting error as is too long. maximum size is 240 characters." pooga17 UNIX for Advanced & Expert Users 1 02-06-2008 01:56 AM
what is the maximum length of th os-command line in Unix. kumardesai UNIX for Dummies Questions & Answers 2 11-28-2007 03:37 AM
Print the line containing the maximum value in a column kingkong UNIX for Advanced & Expert Users 22 02-03-2006 06:38 AM
What is the Maximum number of characters that i can store in a variable? pcshan UNIX for Advanced & Expert Users 9 08-11-2004 02:25 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-11-2006
Registered User
 

Join Date: Oct 2006
Posts: 8
Stumble this Post!
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-11-2006
Registered User
 

Join Date: Dec 2005
Location: Boston, USA
Posts: 65
Stumble this Post!
try with script

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

Last edited by Manish Jha; 10-11-2006 at 01:11 PM.
Reply With Quote
  #3 (permalink)  
Old 10-11-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Stumble this Post!
Code:
awk ' length > max { max=length;row=NR } END{ print row" "max}' file
Reply With Quote
  #4 (permalink)  
Old 10-11-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,540
Stumble this Post!
Quote:
Originally Posted by mohapatra
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.
In Python:
Code:
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
Reply With Quote
  #5 (permalink)  
Old 10-11-2006
Registered User
 

Join Date: Oct 2006
Posts: 2
Stumble this Post!
#! /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";
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:03 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0