0
0
Linux CLIscripting~5 mins

Why text processing is Linux's superpower in Linux CLI - Why It Works

Choose your learning style9 modes available
Introduction
Linux is great at handling text because many tasks involve reading, searching, and changing text files. This makes it easy to automate jobs like finding information, filtering data, or changing file contents quickly.
When you want to find specific words or lines inside a big file quickly.
When you need to count how many times a word appears in a document.
When you want to extract certain parts of text from logs or reports.
When you want to replace words or phrases in many files at once.
When you want to sort or organize lines of text based on certain rules.
Commands
This command searches the syslog file for lines containing the word 'error'. It helps you quickly find error messages in system logs.
Terminal
grep error /var/log/syslog
Expected OutputExpected
Apr 26 10:15:32 myserver kernel: [12345.678901] error: device not found Apr 26 10:16:45 myserver systemd[1]: error: failed to start service
-i - Makes the search case-insensitive
-r - Searches recursively in directories
This command counts the number of lines in the syslog file. It helps you know how big the file is or how many entries it contains.
Terminal
wc -l /var/log/syslog
Expected OutputExpected
1500 /var/log/syslog
-w - Counts words instead of lines
-c - Counts bytes instead of lines
This command extracts the fifth word from each line of syslog, sorts them, and counts unique occurrences. It helps find how many times each word appears in that position.
Terminal
cut -d ' ' -f 5 /var/log/syslog | sort | uniq -c
Expected OutputExpected
300 kernel: 200 systemd[1]: 100 CRON[1234]:
-d - Sets the delimiter to split fields
-f - Selects which field to extract
This command replaces all occurrences of 'error' with 'ERROR' in the syslog file and shows the first 5 lines. It helps highlight errors in uppercase for easier reading.
Terminal
sed 's/error/ERROR/g' /var/log/syslog | head -5
Expected OutputExpected
Apr 26 10:15:32 myserver kernel: [12345.678901] ERROR: device not found Apr 26 10:16:45 myserver systemd[1]: ERROR: failed to start service Apr 26 10:17:00 myserver CRON[5678]: (root) CMD (run-parts /etc/cron.hourly) Apr 26 10:18:12 myserver kernel: [12346.789012] INFO: device connected Apr 26 10:19:23 myserver systemd[1]: Started Daily Cleanup.
s/old/new/g - Substitutes all occurrences of 'old' with 'new'
Key Concept

If you remember nothing else, remember: Linux commands can quickly find, count, extract, and change text, making automation and troubleshooting fast and easy.

Common Mistakes
Using grep without quotes around the search word when it contains special characters
The shell may interpret special characters causing unexpected results or errors
Always put the search pattern in single or double quotes if it contains special symbols
Forgetting to specify the delimiter in cut when fields are separated by spaces or tabs
cut may not extract the correct field if the delimiter is wrong or missing
Use the -d flag to set the correct delimiter matching your text format
Using sed substitution without the 'g' flag to replace all occurrences on a line
Only the first occurrence on each line is replaced, missing others
Add the 'g' flag at the end of the substitution command to replace all matches
Summary
Use grep to search for specific words or patterns inside files.
Use wc to count lines, words, or bytes in text files.
Use cut, sort, and uniq together to extract and count unique text fields.
Use sed to find and replace text patterns in files or streams.