1. wc (Word Count)
The wc command is used to count the number of lines, words, and bytes (or characters) in a text file. This is very useful when you need some basic statistics about a file’s content.
Basic Usage:
wc filename.txtThis will display three things by default:
- The number of lines
- The number of words
- The number of bytes (characters, including spaces)
Example Output:
15 100 1200 filename.txt
This means:
- 15 lines in the file
- 100 words in the file
- 1200 bytes (characters) in the file
Variations of wc:
-
Lines Only (
-l): To count only the number of lines in a file:wc -l filename.txt -
Words Only (
-w): To count only the number of words in a file:wc -w filename.txt -
Bytes Only (
-c): To count only the number of bytes (characters) in a file:wc -c filename.txt -
Characters Only (
-m): To count the number of characters (not bytes), use:wc -m filename.txt -
Multiple Options: You can also combine options. For example, to count lines, words, and bytes:
wc -l -w -c filename.txt
2. uniq — Display Unique Lines
The uniq command is used to filter out duplicate lines from a text file, showing only the unique lines. By default, uniq only works on consecutive duplicate lines.
Basic Usage:
uniq filename.txtThis command will display the file content but will remove any consecutive duplicate lines.
Example:
If filename.txt contains:
apple
banana
banana
cherry
apple
After running uniq, the output will be:
apple
banana
cherry
apple
Here, “banana” only appears once, as it was duplicated consecutively.
Variations of uniq:
-
-c(Count Occurrences): To show the number of occurrences of each unique line:uniq -c filename.txtOutput:
1 apple 2 banana 1 cherry 1 appleThis shows how many times each unique line appears.
-
-d(Show Duplicate Lines Only): To display only the lines that appear more than once:uniq -d filename.txtOutput:
banana -
-u(Show Unique Lines): To display only the lines that appear once:uniq -u filename.txtOutput:
apple cherry -
Sorting Before Using
uniq: If the file is not sorted and you wantuniqto work properly, you can combine it withsort. For example:sort filename.txt | uniq
3. nl — Add Line Numbers to a File
The nl command is used to add line numbers to a file’s content. It’s useful for viewing large files with line numbers, especially for debugging or references.
Basic Usage:
nl filename.txtThis will display the contents of filename.txt, but each line will be prefixed with a line number.
Example Output:
1 apple
2 banana
3 cherry
4 apple
Variations of nl:
-
Custom Line Number Format (
-n): You can control how the line numbers are formatted with the-noption:-
-n lnto left-align the line numbers:nl -n ln filename.txt -
-n rzto add leading zeros for the line numbers:nl -n rz filename.txt -
-n rnto right-align the numbers:nl -n rn filename.txt
-
-
Skipping Empty Lines (
-sand-b): The-soption adds a separator between the line number and the line content, and the-boption controls how blank lines are numbered.For example, if you don’t want empty lines numbered, you can use:
nl -b a filename.txt -
Starting from a Specific Number (
-v): You can start the line numbering from a specific number using the-voption:nl -v 100 filename.txtThis will start numbering lines from 100.
Summary:
wc: Counts the number of lines, words, and bytes/characters in a file. You can specify which to count using options like-l,-w,-c.uniq: Filters out duplicate lines, displaying only unique lines. You can show counts, duplicates, or unique lines only with options like-c,-d,-u.nl: Adds line numbers to each line of a file for reference or debugging. You can control formatting, start from a custom number, and even skip blank lines.