Counting and modifying lines, words and characters in Linux text files

Uncategorized

[]

Linux includes some useful commands for counting when it comes to text files. This post examines some of the options for counting lines and words and making changes that might help you see what you want.

Counting lines

Counting lines in a file is very easy with the wc command. Use a command like that shown below, and you’ll get a quick response.

$ wc -l myfile 132 myfile

What the wc command is actually counting is the number of newline characters in a file. So, if you had a single-line file with no newline character at the end, it would tell you the file has 0 lines,

The wc -l command can also count the lines in any text that is piped to it. In the example below, wc -l is counting the number of files and directories in the current directory.

$ ls -l | wc -l 1184

If you pipe text to a wc command with a hyphen as its argument, wc will count the lines, words and characters.

$ echo hello to you | wc – 1 3 13 –

The responses show the number of lines (1), words (3) and characters (13 counting the newline).

If you want to get the same information for a file, pipe the file to the wc command as shown below.

$ cat notes | wc – 48 613 3705 –

Counting words

For just a word count, use the w option as shown in the examples below.

$ wc -w notes 613 TT2 $ date | wc -w 7

Counting characters

To count the characters in a file, use the -c option. Keep in mind that this will count newline characters as well as letters and punctuation marks.

$ wc -c TT2 3705 TT2

Counting instances of particular words

Counting how many times a particular word appears in a file is a lot more complex. To count how many lines contain a word is considerably easier.

$ cat notes | grep the | wc -l 32 $ cat notes | grep [Tt]he | wc -l 40

The second command above counts lines containing “the” whether or not the word is capitalized. It still doesn’t tell you how many times “the” appears overall, because any line containing the word more than once gets counted only once.

Ignoring punctuation and capitalization

Some words (e.g., “The” and “the”) will appear in your word lists more than once. You’re also going to see strings like “end” and “end.” since the commands described above don’t separate words from punctuation. To move past these problems, some additional commands are added in the examples that follow.

Removing punctuation

In the command below, a file containing a long string of punctuation characters is passed to a tr -d command that removes all of them from the output. Notice how everything except the “Characters ” string is removed from the output.

$ cat punct-chars Characters .?,”!;:'{}[](): $ cat punct-chars | tr -d ‘[:punct:]’ Characters

Changing text to all lowercase

A tr command can turn all character to lowercase to ensure that words that start with a capital letter (often because they start the sentence) or contain all capitals aren’t listed separately from those appearing in all lowercase.

$ echo “Hello to YOU” | tr ‘[A-Z]’ ‘[a-z]’ hello to you

Using a script

The script below sets up three sets of commands for extracting the contents of a text file and extracting the words…

Source

Leave a Reply

Your email address will not be published. Required fields are marked *