Text Processing
Text processing is an essential skill when working with the command line. This section covers the basic commands for searching, filtering, and manipulating text.
wc
wc is used to print newline, word, and byte counts for each file.
wc file.txt

Here are some useful flags you can use with wc:
| Flag | Description |
|---|---|
-l |
Print the newline counts. |
-w |
Print the word counts. |
-c |
Print the byte counts. |
head and tail
head and tail are used to output the first or last part of files.
head file.txt
tail file.txt
Here are some useful flags you can use with tail:
| Flag | Description |
|---|---|
-n |
Number of lines to show. |
-f |
Follow the file (tail only). |
grep
grep is used to search for patterns in files.
grep "pattern" file.txt

Here are some useful flags you can use with grep:
| Flag | Description |
|---|---|
-i |
Ignore case. |
-r |
Recursively search directories. |
-l |
List filenames only. |
-n |
Show line numbers. |
awk
awk is a powerful text processing language used for pattern scanning and processing.
awk '{print $1}' file.txt

Here we print the first column of file.txt.
Examples
- Print specific columns:
awk '{print $1, $3}' file.txt - Filter based on pattern:
awk '/pattern/ {print $0}' file.txt
sed
sed is a stream editor used for filtering and transforming text.
sed 's/old/new/' file.txt
Examples
- Replace all occurrences of a string:
sed 's/old/new/g' file.txt - Delete lines containing a pattern:
sed '/pattern/d' file.txt
cut
cut is used to remove sections from each line of files.
cut -d' ' -f1 file.txt
Examples
- Extract specific fields:
cut -f1,3 file.txt
sort
sort is used to sort lines of text files. By default, it sorts alphabetically.
sort file.txt
Here are some useful flags you can use with sort:
| Flag | Description |
|---|---|
-r |
Reverse order. |
-n |
Numerical sort. |
-u |
Print unique lines. |
uniq
uniq is used to report or omit repeated lines.
sort file.txt | uniq
Here are some useful flags you can use with uniq:
| Flag | Description |
|---|---|
-c |
Count occurrences. |
-d |
Only show duplicate lines. |
Next Step
Now that you know how to perform text processing, check out the next section on Process Management to learn how to manage running processes in Bash.