Operating System - HP-UX
1832726 Members
2913 Online
110043 Solutions
New Discussion

need to check the character length of each line

 
sathis kumar
Frequent Advisor

need to check the character length of each line

Hello,

Requirement is:
1) each line should have a character count of 1825
2) If not then it should display the line no which is not having the count-1825

I have written a following script for that:
-------------------------------------------
count=1;
while read line
do
if [ ${#line} != 1825 ]
then
echo "Line No:$count length is not 1825" >> TEMPFILE
exit 99;
fi
count=`expr $count + 1`
done < testing

In the above case, 'testing' is the i/p file name and it has got around '876875' lines and so when I tried to execute the script, it is taking minimum of 10 minutes. Could you please let me know how the performance can be improved?

Thanks,
Sathish

6 REPLIES 6
Eric SAUBIGNAC
Honored Contributor

Re: need to check the character length of each line

Bonjour,

You can do it in a more efficient way with basic program in C

Eric
Matti_Kurkela
Honored Contributor

Re: need to check the character length of each line

How about this:

grep -nvE '^.{1825}$' filename | cut -d : -f 1

MK
MK
James R. Ferguson
Acclaimed Contributor

Re: need to check the character length of each line

Hi Sathish:

Since you don't specify anything other than the generic "character":

# awk 'length!=1825' file

This excludes newline characters.

The following includes newline characters too:

# perl -ne 'print if length!=1826' file

Regards!

...JRF...
Michael Mike Reaser
Valued Contributor

Re: need to check the character length of each line

sathis:>If not then it should display the line no which is not having the count-1825

JRF:> awk 'length!=1825' file

JRF, to fulfill sathis's second requirement, shouldn't the awk be something similar to

awk 'length!=1825{print $NR, $0}' file

or

awk 'length!=1825{print $NR}' file

?



There's no place like 127.0.0.1

HP-Server-Literate since 1979
James R. Ferguson
Acclaimed Contributor

Re: need to check the character length of each line

Hi (again) Sathis:

I missed the addition of the line-number on which the length isn't met (as Michael pointed out):

For 'awk':

# awk 'length!=1825{print NR,$0}' file

...notice that the record-number is 'NR" not '$NR'.

For Perl:

# perl -ne 'print "$. $_" if length!=1826' file

...which again we note _includes_ counting the newline character at the end of each line.

Regards!

...JRF...

Fredrik.eriksson
Valued Contributor

Re: need to check the character length of each line

You can also use wc -m or wc --chars to count number of characters in a string.
so something like this might work:

if [ $(wc --chars ${#line}) -ne 1825 ]; then
echo "Line No:$count length is not 1825" >> TEMPFILE
fi

Best regards
Fredrik Eriksson