Bird
Raised Fist0
Linux CLIscripting~5 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which Linux command is mainly used to search for specific text patterns within files?
easy
A. sed
B. awk
C. grep
D. cat

Solution

  1. Step 1: Understand the purpose of grep

    grep searches files for lines matching a text pattern.
  2. Step 2: Compare with other commands

    sed edits text streams, awk processes text fields, cat displays file content.
  3. Final Answer:

    grep -> Option C
  4. Quick Check:

    Search text patterns = grep [OK]
Hint: Remember: grep means 'global regular expression print' [OK]
Common Mistakes:
  • Confusing sed with grep
  • Thinking awk is for simple text search
  • Using cat to find patterns
2. Which of the following is the correct syntax to replace all occurrences of 'apple' with 'orange' in a file named fruits.txt using sed?
easy
A. sed 'apple=orange' fruits.txt
B. sed 'replace apple with orange' fruits.txt
C. sed -r 'apple -> orange' fruits.txt
D. sed 's/apple/orange/g' fruits.txt

Solution

  1. Step 1: Recall sed substitution syntax

    The correct form is sed 's/old/new/g' to replace all occurrences.
  2. Step 2: Match syntax to options

    Only sed 's/apple/orange/g' fruits.txt uses the correct s/apple/orange/g pattern.
  3. Final Answer:

    sed 's/apple/orange/g' fruits.txt -> Option D
  4. Quick Check:

    sed substitution = s/old/new/g [OK]
Hint: Use s/old/new/g for global replace in sed [OK]
Common Mistakes:
  • Missing 's/' at start of substitution
  • Using wrong delimiters or syntax
  • Forgetting the 'g' flag for global replace
3. What will be the output of the following command?
echo -e "apple\nbanana\napple" | grep -c apple
medium
A. 2
B. 1
C. 3
D. 0

Solution

  1. Step 1: Understand the input lines

    The input has three lines: 'apple', 'banana', and 'apple'.
  2. Step 2: Count lines matching 'apple' with grep -c

    grep -c apple counts lines containing 'apple'. Two lines match.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Count lines with 'apple' = 2 [OK]
Hint: grep -c counts matching lines, not occurrences [OK]
Common Mistakes:
  • Counting total lines instead of matches
  • Confusing occurrences with line count
  • Ignoring the -c flag meaning
4. The command below is intended to print only lines containing 'error' from log.txt. What is wrong?
grep error log.txt | sed 's/error//'
medium
A. It filters lines but removes 'error' text from output
B. It causes a syntax error in sed
C. It removes 'error' but prints all lines, not filtered
D. It does not read the file

Solution

  1. Step 1: Analyze the pipeline commands

    grep error log.txt filters lines with 'error'. Then sed 's/error//' removes 'error' from those lines.
  2. Step 2: Understand the effect on output

    The output shows lines containing 'error' but with the word 'error' removed, which may be unintended.
  3. Final Answer:

    It filters lines but removes 'error' text from output -> Option A
  4. Quick Check:

    grep filters, sed edits text [OK]
Hint: Pipes chain commands; sed edits output after grep filters [OK]
Common Mistakes:
  • Thinking sed filters lines
  • Expecting grep to remove text
  • Assuming syntax error without checking
5. You want to extract the second column from a file data.csv where columns are separated by commas. Which command pipeline correctly does this?
hard
A. grep ',' data.csv | sed 'print $2'
B. cat data.csv | awk -F',' '{print $2}'
C. sed -n '2p' data.csv
D. awk '{print $2}' data.csv

Solution

  1. Step 1: Identify the field separator and extraction tool

    Columns are comma-separated, so awk with -F',' sets comma as delimiter.
  2. Step 2: Check command correctness

    cat data.csv | awk -F',' '{print $2}' uses awk -F',' '{print $2}' to print second column correctly.
  3. Final Answer:

    cat data.csv | awk -F',' '{print $2}' -> Option B
  4. Quick Check:

    awk -F',' prints columns by field [OK]
Hint: Use awk -F',' to split CSV columns [OK]
Common Mistakes:
  • Using sed to print columns incorrectly
  • Forgetting to set field separator in awk
  • Using grep which filters lines, not columns
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard