![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
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. |
|
|||
|
Quote:
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
|
|
|||
|
#! /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"; |
|||
| Google The UNIX and Linux Forums |