Word count files linux

Count number of words in file, bash script

Thats 6 words! 😉 cyberciti.biz/faq/finding-bash-shell-array-length-elements stackoverflow.com/questions/5382712/… These 2 links should help you greatly. The first one is how to tell the length of an array, the second is how to tokenize a string variable into an array

why not just have your bash script execute wc -w ? This is NOT the kind of task that shell scripting is a good tool for, and while it is possible to drive a screw with a hammer that doesn’t make it worth doing.

This is part of a script that counts how many words are on each line of a given file then at the end counts the total number of words in the file.

6 Answers 6

if given a stream of input as shown:

while read -a words; do (( num += $ )); done echo Word count: $num. 

Extending from the link @FredrikPihl gave in a comment: this reads from each file given as an argument or from stdin if no files given:

for f in "$"; do while read -a words; do (( num += $ )); done < "$f" done echo Word count: $num. 
for f in "$"; do words=( $( < "$f") ) (( num += $)) done echo Word count: $num. 

Try this:
wc -w *.md | grep total | awk ''

#!/bin/bash word_count=$(wc -w) echo "Word count: $word_count." 

As pointed by @keshlam in the comments, this can be easily done by executing wc -w from the shell script, I didn't understand what could be its use case.

Although, the above shell script will work as per your requirement. Below is a test output.

enter image description here

I believe what you need is a function that you could add to your bashrc:

function script1() < wc -w $1; >script1 README.md 335 README.md 

You can add the function to your .bash_rc file and call it what you want upon next console or if you source your .bashrc file then it will load in the function . from then on you can call function name like you see with file and it will give you count

Источник

Linux wc command for getting the word count of files

The Linux wc command is one of many commands that I have become aware of when looking thru the \/user\/bin path for things to check out, and maybe write a thing or two about. This wc command can be used to get a word count of a text file where a word is a non-zero length string of characters between whitespace.

Читайте также:  Создание раздела на флешке linux

1 - Linux wc command basic Example

For a basic example of the Linux wc command a good way to get started with a command like this is to use the Linux echo command to feed it some example text via the standard input via a pipe. Sp here I create some text with Linux echo and then pipe that to the Linux wc command.

$ echo 'Linux wc can count words' | wc
1 5 25

The result if a few numbers the center of which as you can see is the word could. However what if you want to feed wc a file, or a whole bunch of files in a folder? Also what if I just want the word count, and not those other numbers for line breaks and byte size? With that said maybe just a few more examples on this command is called for then.

2 - Just get word count

So just get the word count only from some standard input I just need to pass the w option.

$ echo 'Linux wc can count words' | wc -w
5

For some text coming from the standard input this will work just file, but when passing one or more files in place of this I will get a file name next to each word count. Speaking of files lets get to some basic and not so basic examples of dealing with files and the Linux wc command.

3 - Get word count of a file

This might very well may be the most typical use case of the wc command. I have a text file, and I want to know how many words are in it. There are two general ways to go about doing so, one of which would involve piping the output of a file that was opened with something like the cat command, and the other word be to just pass the file name as an option directly tio Linux wc.

3.1 - use the cat command and pipe the output to wc

The cat command is one way to go about just opening up a file and then creating some output that is the content of that file to the standard output. Form there it can be piped to a command like the wc command.

$ echo 'hello world this is some text' > ~/hw.txt
$ cat hw.txt | wc -w
6

3.2 - pass the file name as an argument

To get the word count of a file I just need to pass the file that I want the count for to wc. So just like before I will and to use the -w option in order to just get the word count, and then pass the path to the file that I want a word count for.

$ echo 'hello world this is some text' > ~/hw.txt
$ wc -w ~/hw.txt
6 /home/dustin/hw.txt

This will give the word count only because of the w option, but it will also give the path to the file also. In addition what if I have a whole bunch of files in a folder? I can enter each file name one after another, but what if there is well over one hundred file names? There should be a way to just generate that and inject it right?

4 - Get word count of a bunch of files

So if you are not familiar with piping in Linux ut would be a good idea to look into it. With piping I can use a command like the find command to get a list of file names. I can then pipe this list of file names to a command like xargs that will use the file names from the standard input as arguments for another command. I could use xargs with Linux wc directly, but why not pipe the file names to cat to create one large text file from my large collection of text files, and then pipe that to Linux wc?

$ pwd
/home/dustin/Documents/github_dustinpfister/blog_posts/_posts
$ find *.md | xargs cat | wc -w
820041

Okay great now I have my grand word count total, or do I? These are markdown files after all, and as such there is much markdown code in the from of code examples and so forth that I might not wanted counted. So I might need to pipe things threw one or two more commands that will convert my markdown to plain text first to get another count that might be what I am actually looking for.

Читайте также:  Linux list all sudo users

5 - Get a count of how man files there are in a folder with the linux ls and wc commands

The -l option of wc is a way to count the number of new lines in a volume of text. The text suppled to the wc command does not have to be from a file though, it can be piped in from another command such as the Linux ls command. If you are not aware of what the ls command is, it is a way to go about listing the contents of a folder. When doing so each item is separated with a new line character which of course can be counted with the wc command if the output of the Linux lc command can be piped to wc.

So in my posts folder if I use the linux ls command to find out how many mark down files I have I end up with a list like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ ls linux*
linux-aspell.md
linux-curl.md
linux-echo.md
linux-find.md
linux-grep.md
linux-keep-process-running.md
linux-kill.md
linux-ls.md
linux-nodejs-cli-tools-getting-started.md
linux-pipe.md
linux-ps.md
linux-raspberry-pi-os-swap-file.md
linux-raspberry-pi-os-turrn-off-screen-blanking.md
linux-raspbian-lite-getting-started.md
linux-raspbian-lite-xserver-xorg.md
linux-redirection.md
linux_shebang.md
linux-tee.md
linux-wc.md
linux-xargs.md

If I take a moment to count them there are 20 posts on Linux at the time of this writing. However how can I end up getting a count with Linux command? Simple I just need to pipe this text to the Linux wc command and use the -l option to count the new lines rather than words.

I can then change the options for the ls command to get different counts including for things like hidden files by using the -a option with the ls command. However you get the general idea here, the wc command is useful for not just counting words, but also lines, and the size of text.

6 - Conclusion

So the Linux wc command is useful for just getting the word count of a file, but it is also using for getting counts of other metrics such as line count, and size. It can be used with a file, but also it can be used with standard output from any other command.

Источник

Linux “wc” Command

The Linux “wc” command is an abbreviation for word count. The command is used to count the number of lines, words, bytes, and even characters and bytes in a text file. In this tutorial, we look at the Linux “wc” command and demonstrate practical examples of its usage.

Читайте также:  Linux ограничить заряд батареи ноутбука

Basic Syntax

The Linux “wc” command takes the following syntax:

The “wc” command, in its basic form, displays the output in a columnar format, as shown in the snippet below.

Let’s briefly examine what each column represents:

Column 1: Displays the number of lines existing in the text file. As observed from the output, the file has seven lines. Be advised that this accounts for both the blank and the non-blank lines.

Column 2: This prints the word count.

Column 3: This displays the number of bytes in the file.

Column 4: This is the file name of the text file.

Pass Multiple Files as Arguments in One Command

The “wc” command can also take multiple files in one command and display the statistics of each file on a separate file. Suppose you have two files, like in our example where we have two text files — fruits.txt and vegetables.txt.

Instead of using the “wc” command twice to view the statistics of each file, you can use the following syntax to accept both files as arguments.

For our example, to count the number of lines, words, and bytes in each file, run the following command:

From the output, you can see that the “wc” command displays the output of both files in a columnar output. The result of each file is placed on a separate row, and the very last row provides the total count for the lines, words, and bytes of both files.

Count the Number of Lines Only in a File

Let’s consider the text file hello.txt that we started with. Let’s recap the line, word, and character count as follows:

The -l option is used to count the number of lines only.

To display the word count in a text file, use the -w option as follows. This is relatively straightforward, and as you can see, it counts the number of words only contained in the file.

Count the Number of Bytes only in a File

To print the number of bytes only in a file, use the -c option as provided in the command below:

Count the Number of Characters Only in a File

Additionally, you can count the number of characters by passing the -m option as provided in the command below:

For more command options and usage of the “wc” command, be sure to check the man pages:

If you are interested in checking the version, simply invoke the simple command:

Conclusion

The Linux “wc” command is a really simple and easy-to-use command that gives you a clue on the number of lines, words, bytes, and characters contained in a file. For any queries, do get in touch. We will endeavor to give a prompt response.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.

Источник

Оцените статью
Adblock
detector