Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

counting characters 1

Status
Not open for further replies.

tradntele

Programmer
Nov 7, 2001
14
US
I have a text file and I'd like to count the number of characters in each line. Ultimately, I want to sum up the number of lines by how many characters are in it, but first how do you get a count per line?
 


nawk -f count.awk myFile.txt

#-------- count.awk -------

{
len=length($0);
total+=len;
printf("%d %d\n", FNR, len);

}

END {
printf("\n\tTotal->[%d]\n", total);
}

#---------- vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
So I get the length of each line, but some of the lines seem to be too long... I get the following message...

nawk: input record `textofline...' too long
input record number 32840, file /directory/filename
source line number 1

So the script stops running at that record. What is the character limit of an input record and is there a workaround?
 
This is OS limitation of nawk - don't quite remembe rthe exact number . It might be kernel specific.

Here's the script work around.

count.ksh yourFile.txt

#--------------------- count.ksh -------------------
#!/bin/ksh

input=&quot;${1}&quot;

lineNum=$(wc -l < ${input} | sed -e 's/ //g')

typeset -i i=1;
typeset -i total=0;

while [ ${i} -le ${lineNum} ]
do
curLen=$(head -${i} ${input} | tail -1 | wc -c | sed -e 's/ //g')

echo &quot;line->[${i}] [${curLen}]&quot;
i=$((i + 1));
total=$((total + ${curLen}))
done;


echo &quot; Total->[${total}]&quot;

#-------------------------------------------------- vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Hi,

You might want to try the word count &quot;wc&quot; command in Unix.


wc [-c|-m] [-lw] [file]...

-c Report the number of bytes in each input file.

-l Report the number of newline characters in each input file.

-m Report the number of characters in each input file.

-w Report the number of words in each input file.


Good Luck

Mok
 
I agree with the `wc` command and would implement something like:

while read line
do
wc -c $line
done < $filename.txt

Of course, you could spruce it up some, but that is quick and dirty and gets the results you want.
 
cat filename | awk 'BEGIN \^J {FS=&quot; &quot;;OFS=&quot; &quot;}\^J {printf (&quot;%s \n&quot;,NF)}'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top