Bird
Raised Fist0
Bash Scriptingscripting~5 mins

Why scripts often process text in Bash Scripting

Choose your learning style10 modes available
Introduction
Scripts often work with text because text files are simple and easy to read or change. Many system tasks, like logs or settings, use text, so scripts handle text to automate these tasks.
Checking system logs to find errors
Extracting information from configuration files
Changing parts of a text file automatically
Combining data from multiple text files
Filtering output from commands to show only needed details
Syntax
Bash Scripting
command [options] [arguments]
Most text processing in scripts uses simple commands like grep, sed, awk, or cut.
Commands often read text from files or other commands and output the result.
Examples
Finds lines containing 'error' in the file logfile.txt.
Bash Scripting
grep 'error' logfile.txt
Replaces all occurrences of 'old' with 'new' in file.txt.
Bash Scripting
sed 's/old/new/g' file.txt
Prints the first word from each line in data.txt.
Bash Scripting
awk '{print $1}' data.txt
Sample Program
This script searches the file system.log for the word 'fail' (case-insensitive), counts how many lines match, and prints the count.
Bash Scripting
#!/bin/bash
# This script finds lines with 'fail' in a log and counts them
fail_count=$(grep -i 'fail' system.log | wc -l)
echo "Number of failures found: $fail_count"
OutputSuccess
Important Notes
Text files are everywhere in computers, so knowing how to process text helps automate many tasks.
Commands like grep and sed are very powerful but easy to start using with simple examples.
Always test your text commands on sample files before running on important data.
Summary
Scripts process text because text files are common and easy to handle.
Text processing helps automate tasks like searching, replacing, and extracting data.
Simple commands like grep, sed, and awk make text processing straightforward.

Practice

(1/5)
1. Why do many bash scripts process text files instead of other file types?
easy
A. Because text files are easy to read and manipulate with simple commands
B. Because binary files are faster to process
C. Because text files take up less disk space
D. Because scripts cannot open images or videos

Solution

  1. Step 1: Understand file types in scripting

    Text files contain readable characters, making them easy to handle with commands like grep and sed.
  2. Step 2: Compare text and binary files

    Binary files need special tools and are harder to manipulate directly in scripts.
  3. Final Answer:

    Because text files are easy to read and manipulate with simple commands -> Option A
  4. Quick Check:

    Text files = easy manipulation [OK]
Hint: Text files are simple to handle with basic commands [OK]
Common Mistakes:
  • Thinking binary files are easier to process
  • Assuming scripts can't open non-text files
  • Confusing file size with ease of processing
2. Which of the following is the correct syntax to search for the word error in a file named log.txt using grep?
easy
A. grep 'log.txt' error
B. grep error log.txt
C. grep -f error log.txt
D. grep log.txt error

Solution

  1. Step 1: Identify grep command syntax

    The correct syntax is grep [pattern] [file].
  2. Step 2: Match pattern and file

    Here, error is the pattern and log.txt is the file to search.
  3. Final Answer:

    grep error log.txt -> Option B
  4. Quick Check:

    grep pattern file = correct syntax [OK]
Hint: Remember grep syntax: grep pattern filename [OK]
Common Mistakes:
  • Swapping pattern and filename
  • Using incorrect flags like -f
  • Putting filename before pattern
3. What will be the output of this bash command?
echo "apple banana cherry" | awk '{print $2}'
medium
A. apple
B. cherry
C. apple banana
D. banana

Solution

  1. Step 1: Understand the input and awk command

    The input string is "apple banana cherry". Awk splits input by spaces and $2 refers to the second word.
  2. Step 2: Identify the second word

    The second word is "banana".
  3. Final Answer:

    banana -> Option D
  4. Quick Check:

    awk $2 = second word = banana [OK]
Hint: awk $2 prints the second word from input [OK]
Common Mistakes:
  • Confusing $1 and $2 fields
  • Expecting full line output
  • Not splitting input by spaces
4. This script is intended to replace all occurrences of 'cat' with 'dog' in a file named pets.txt. What is wrong?
sed 's/cat/dog/g pets.txt'
medium
A. sed cannot replace text
B. File name should come before the command
C. Missing closing single quote after g
D. The command should use double quotes instead of single quotes

Solution

  1. Step 1: Check sed command syntax

    The substitution command must be enclosed in quotes fully: 's/cat/dog/g'
  2. Step 2: Identify the error in the given command

    The closing single quote is missing after the g, causing a syntax error.
  3. Final Answer:

    Missing closing single quote after g -> Option C
  4. Quick Check:

    Quotes must enclose entire sed expression [OK]
Hint: Check for matching quotes around sed expressions [OK]
Common Mistakes:
  • Forgetting to close quotes
  • Misplacing file name inside quotes
  • Thinking sed can't replace text
5. You want to extract all lines containing the word error from multiple log files and save them into a single file errors.txt. Which script snippet correctly does this?
hard
A. grep error *.log > errors.txt
B. grep error errors.txt > *.log
C. grep > errors.txt *.log
D. grep *.log error > errors.txt

Solution

  1. Step 1: Understand grep usage with multiple files

    Using grep error *.log searches for 'error' in all files ending with .log.
  2. Step 2: Redirect output to a file

    The > operator sends all matching lines into errors.txt.
  3. Final Answer:

    grep error *.log > errors.txt -> Option A
  4. Quick Check:

    grep pattern files > outputfile [OK]
Hint: Use grep pattern files > outputfile to save matches [OK]
Common Mistakes:
  • Placing > before files
  • Swapping pattern and files
  • Using incorrect order of arguments
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