help need to find the length of a variable

Hi frnds,

i have a file which contains multiple lines. each line size may come around 64MB. and i want to read line by line and nee to find the length of each line,

at present am doing it in the following way,

while read LINE
do
len=`expr length "$LINE"`
# and using the variable len for some purpose
done < file

the problem is, when the line size is big, i am getting error like parameter is too long.
is there any other way to find the length?

Thanks in advance,

  • Raja.

Try:

echo "$LINE" | wc -c

With "-c" counts bytes, with "-m" counts characters.

Hi,

Instead of using a while,you can use awk to find the length of each line.

awk '{len=length($0);print len;}' file

Regards,
Chella

echo has a "newline". either use printf or echo -n

You're right :slight_smile:

yes, its working...
printf "$LINE" | wc -c
is there any size limit for this?

also pls look the following,

$ a="ttt fff"
$ echo "$a" | wc -c
8
$ echo -n "$a" | wc -c
11
$

# a="ttt fff"
# echo "$a" | wc -c
8
# echo -n "$a" | wc -c
7

that's a shell echo issue:

#  sh
$ a="ttt fff"
$ echo "$a" | wc -c
       8
$ echo -n "$a" | wc -c
      11
$ bash
#  a="ttt fff"
#  echo "$a" | wc -c
       8
#  echo -n "$a" | wc -c
       7

specifically in sh:

$ echo -n "$a"
-n ttt fff