Counting number of characters in a file through shell script

LinuxShell

Linux Problem Overview


I want to check the no of characters in a file from starting to EOF character. Can anyone tell me how to do this through shell script

Linux Solutions


Solution 1 - Linux

This will do it for counting bytes in file:

wc -c filename

If you want only the count without the filename being repeated in the output:

wc -c < filename

This will count characters in multibyte files (Unicode etc.):

wc -m filename

(as shown in Sébastien's answer).

Solution 2 - Linux

#!/bin/sh

wc -m $1 | awk '{print $1}'

wc -m counts the number of characters; the awk command prints the number of characters only, omitting the filename.

wc -c would give you the number of bytes (which can be different to the number of characters, as depending on the encoding you may have a character encoded on several bytes).

Solution 3 - Linux

awk '{t+=length($0)}END{print t}' file3

Solution 4 - Linux

To get exact character count of string, use printf, as opposed to echo, cat, or running wc -c directly on a file, because using echo, cat, etc will count a newline character, which will give you the amount of characters including the newline character. So a file with the text 'hello' will print 6 if you use echo etc, but if you use printf it will return the exact 5, because theres no newline element to count.

How to use printf for counting characters within strings:

$printf '6chars' | wc -m
6

To turn this into a script you can run on a text file to count characters, save the following in a file called print-character-amount.sh:

#!/bin/bash
characters=$(cat "$1")
printf "$characters" | wc -m

chmod +x on file print-character-amount.sh containing above text, place the file in your PATH (i.e. /usr/bin/ or any directory exported as PATH in your .bashrc file) then to run script on text file type:

print-character-amount.sh file-to-count-characters-of.txt

Solution 5 - Linux

awk only

awk 'BEGIN{FS=""}{for(i=1;i<=NF;i++)c++}END{print "total chars:"c}' file

shell only

var=$(<file)
echo ${#var}

Ruby(1.9+)

ruby -0777 -ne 'print $_.size' file

Solution 6 - Linux

The following script is tested and gives exactly the results, that are expected

\#!/bin/bash

echo "Enter the file name"

read file

echo "enter the word to be found"

read word

count=0

for i in \`cat $file`

do

if [ $i == $word ]

then

count=\`expr $count + 1`

fi

done

echo "The number of words are $count"

Solution 7 - Linux

I would have thought that it would be better to use stat to find the size of a file, since the filesystem knows it already, rather than causing the whole file to have to be read with awk or wc - especially if it is a multi-GB file or one that may be non-resident in the file-system on an HSM.

stat -c%s file

Yes, I concede it doesn't account for multi-byte characters, but would add that the OP has never clarified whether that is/was an issue.

Solution 8 - Linux

Credits to user.py et al.


echo "ää" > /tmp/your_file.txt
cat /tmp/your_file.txt | wc -m

results in 3.

In my example the result is expected to be 2 (twice the letter ä). However, echo (or vi) adds a line break \n to the end of the output (or file). So two ä and one Linux line break \n are counted. That's three together.

Working with pipes | is not the shortest variant, but so I have to know less wc parameters by heart. In addition, cat is bullet-proof in my experience.

Tested on Ubuntu 18.04.1 LTS (Bionic Beaver).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShwetaView Question on Stackoverflow
Solution 1 - LinuxDennis WilliamsonView Answer on Stackoverflow
Solution 2 - LinuxSébastien Le CallonnecView Answer on Stackoverflow
Solution 3 - LinuxVijayView Answer on Stackoverflow
Solution 4 - Linuxuser.pyView Answer on Stackoverflow
Solution 5 - LinuxkurumiView Answer on Stackoverflow
Solution 6 - LinuxPareshView Answer on Stackoverflow
Solution 7 - LinuxMark SetchellView Answer on Stackoverflow
Solution 8 - LinuxqräbnöView Answer on Stackoverflow