Line with maximum no . of characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Line with maximum no . of characters
# 1  
Old 10-11-2006
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.
# 2  
Old 10-11-2006
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 06:11 PM..
# 3  
Old 10-11-2006
Code:
awk ' length > max { max=length;row=NR } END{ print row" "max}' file

# 4  
Old 10-11-2006
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

# 5  
Old 10-12-2006
#! /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";
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove first 2 characters and last two characters of each line

here's what im trying to do. i have a file containing lines similar to this: data.txt: 1hsRmRsbHRiSFZNTTA1dlEyMWFkbU5wUW5CSlIyeDFTVU5SYjJOSFRuWmpia0ZuWXpKV2FHTnRU 1lKUnpWMldrZFZaMG95V25oYQpSelEyWTBka2QyRklhSHBrUjA1b1kwUkJkd3BOVXpWM1lVaG5k... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Programming

Maximum length of a line

How can I change the maximum length of a programming line in fortran and C (specifically in fortran 77)? Seems the default maximum length is 72 in fortran 77. Thanks. (4 Replies)
Discussion started by: hbar
4 Replies

3. Shell Programming and Scripting

Maximum command line arguments

Hi, Can anyone please help me to know what is the maximum number of command line arguments that we can pass in unix shell script? Thanks in advance, Punitha.S (2 Replies)
Discussion started by: puni
2 Replies

4. Shell Programming and Scripting

Maximum number of characters in a line.

Hi, Could any one please let me know what is the maximum number of characters that will fit into a single line of a flat file on a unix. Thanks. (1 Reply)
Discussion started by: Shivdatta
1 Replies

5. Shell Programming and Scripting

Maximum value of each line entry

Dear power users, I have a file like this: AA 8 AA 6 AA 5 AA 4 AA 3 BB 9 BB 4 BB 3 BB 2 ZZ 5 ZZ 3 ZZ 1 ... The characters in colum one are variously different until the end of the file. I want to extract the maximum value of each entry in... (3 Replies)
Discussion started by: kecopx
3 Replies

6. UNIX and Linux Applications

handling maximum number characters in an input file

Hi, Can anyone help me? An input file has three lines. Each line should only be 2098 as number of characters however line 2 of the input file has more than the maximum number of characters, it exceeded up to 4098. What should I do so that could handle maximum number of characters? that it could... (1 Reply)
Discussion started by: chrysSty
1 Replies

7. UNIX for Advanced & Expert Users

help on ksh and sql..getting error as is too long. maximum size is 240 characters."

Hi, In my script i am executing sql file and there are some variables in SQL files which are assigned at run time. one of the variable value is having more than 240 characters and at time of execution its getting failed with error as "is too long. maximum size is 240 characters." ... (1 Reply)
Discussion started by: pooga17
1 Replies

8. UNIX for Dummies Questions & Answers

what is the maximum length of th os-command line in Unix.

Hi All, I didn't find any thread that match this question so I hope it's not redundant. I am totally new to Unix. I want to know what is the maximum length of the os-commandline in Unix. Will it cause any problem if I run any application whose total path length is much longer than 256... (2 Replies)
Discussion started by: kumardesai
2 Replies

9. UNIX for Advanced & Expert Users

Print the line containing the maximum value in a column

Dear all! I want to find the maximum value in two specific columns with numbers, and then print the entire line containing this value. The file may look like: 1001 34.5 68.7 67 1002 22.0 40.1 32 1003 11.3 34.8 45 I want to find the maximum value within column 2... (22 Replies)
Discussion started by: kingkong
22 Replies

10. UNIX for Advanced & Expert Users

What is the Maximum number of characters that i can store in a variable?

Hi, Can any one tell me, what is the maximum number of characters that can be stored in a variable? Thanks in Advance, Shan (9 Replies)
Discussion started by: pcshan
9 Replies
Login or Register to Ask a Question

Featured Tech Videos