Find the length of each line in the file

Hi,

I want to find the length of each line(including all the characters, spaces etc.) in a file and check if all the lines are of same length using a ksh script. Please help. Thanks in advance.

You can read the lines in file one by one using 'awk' and get the length using "length()" function.

Try this :: ( Lets say the fields are separated by | )

also,

$ while read line; do echo -n "$line" | wc -c; done< f1
5
6
7
$ cat f1
char5
char 6
char  7
$ 

Shell:

while IFS= read -r line; do
  echo ${#line}
done < infile

Including linefeed character:

while IFS= read -r line; do
  echo $(( ${#line} + 1 ))
done < infile
1 Like

Hi Anchal,

Thanks for your reply.

I have attached a sample file for which I am trying to find out the length. I tried the same code you have sent. I am not getting the correct output. This is the output I get:
562
576
The actual output is:
609
609

Thanks

Hi Scrutinizer,

Thanks a lot for your reply..the code works absolutely fine:)

awk '{print length+1}' checklength.txt
$ ruby -ne 'puts $_.size' file