Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting number of characters in a file through shell script

Tags:

linux

shell

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

like image 527
Shweta Avatar asked Feb 17 '11 07:02

Shweta


3 Answers

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).

like image 181
Dennis Williamson Avatar answered Nov 09 '22 10:11

Dennis Williamson


#!/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).

like image 25
Sébastien Le Callonnec Avatar answered Nov 09 '22 09:11

Sébastien Le Callonnec


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
like image 6
user.py Avatar answered Nov 09 '22 11:11

user.py