88

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

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
Shweta
  • 5,198
  • 11
  • 44
  • 58

8 Answers8

139

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

Mikhail V.
  • 75
  • 8
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • 5
    [Sébastien](http://stackoverflow.com/questions/5026214/counting-no-of-characters-in-a-file-through-shell-script/5026237#5026237)'s answer on multibyte files is an important point. – Noufal Ibrahim Feb 17 '11 at 09:44
  • If I want to find count of specific word,without opening file,then ? – Sagar Nikam Feb 27 '13 at 09:43
  • 1
    @SagarNikam: You should ask that as a new question. It can't be done without opening the file, by the way, but I suppose you mean without using a file editor rather than without a program performing an `open()`. One way to do what you want would be along the lines of `grep -o '\' file | wc -l` – Dennis Williamson Feb 27 '13 at 11:59
  • if I have a file with text 'david', that is 5 characters. When I run wc -m or wc -c it outputs 6. To get correct count, use printf 'David' | wc -m (or -c) – OB7 Aug 07 '15 at 00:21
  • 1
    @OB7: As you're no doubt aware, the other character that's being counted in the file is the newline at the end of the line. – Dennis Williamson Aug 07 '15 at 01:00
  • @DennisWilliamson: I was not aware, but thanks to you, now I am. Thank you. – OB7 Aug 07 '15 at 01:02
  • what about number of characters per line – Amir Oct 10 '17 at 16:25
  • 1
    @Amir: `awk '{print length}' filename` - based on a comment [here](https://stackoverflow.com/a/8786674/26428) – Dennis Williamson Oct 10 '17 at 16:32
25
#!/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).

Sébastien Le Callonnec
  • 26,254
  • 8
  • 67
  • 80
7

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
OB7
  • 625
  • 1
  • 7
  • 11
user.py
  • 89
  • 2
  • 5
6
awk '{t+=length($0)}END{print t}' file3
Vijay
  • 65,327
  • 90
  • 227
  • 319
1

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
kurumi
  • 25,121
  • 5
  • 44
  • 52
0

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"
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Paresh
  • 1
  • 1
0

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • In my mind, you don't have to excuse yourself for adding to the answers. Both `stat` and `wc` spent 0.001s to tell me the number of bytes in a 1Gb file, btw. – andersoyvind Sep 29 '16 at 07:23
0

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

qräbnö
  • 2,722
  • 27
  • 40