Day-5 Regular Expressions, Pipelines & 1/O Redirections

Grep: Global Regular Expression Print

Purpose:
grep is used to search for a specific pattern or expression inside files or outputs of other commands and display matching lines.


Basic Examples

  1. Search for a word in a file:

  2. Examples of Grep: Let us pickthe information about root from the file /etc/passwd (/etc/passwd contains information about all the users present in the system)

grep root /etc/passwd

This prints lines containing "root" from /etc/passwd, which stores system users info.

Output example:

root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
  1. Case-insensitive search:
    Use -i to ignore case differences (match Kernel, kernel, KERNEL, etc.).
grep -i kernel ktfile

Output example:

Welcome to Kernel Tech
Welcome to kernel Tech
Welcome to KERNEL TECH
  1. Show lines matching the word and 2 lines after it:
    Use -A option (After context).
grep -nA2 wheel /etc/group

-n shows line numbers; -A2 shows 2 lines after match.

  1. Show lines matching the word and 2 lines before it:
    Use -B option (Before context).
grep -nB2 wheel /etc/group
  1. Exclude lines containing a word:
    Use -v to invert the match (show lines not containing the word).
grep -v Kernel ktfile

Additional options

  • Show matched words in color:
grep --color root /etc/passwd

Combining grep with other commands using pipes (|)

What is the pipe (|)?

  • The pipe operator takes the output of one command and passes it as input to another command

How to use grep with pipe — basic syntax

command1 | grep 'pattern'
  • command1 produces output.

  • grep filters that output to only show lines containing 'pattern'.


Example 1: Search running processes with ps and grep

ps aux | grep firefox
  • Lists all processes and filters only those with the word firefox.

Example 2: Show only .txt files in a directory listing

ls -l | grep '.txt'
  • Lists directory files, filters only .txt files.

Example 3: Filter system log for errors

tail -n 100 /var/log/syslog | grep 'error'
  • Shows last 100 lines of syslog, filters lines containing error.

  • Example -4

cat ktfile | grep -i kernel

More practical examples:

ls -l | grep -i ktfile
ifconfig | grep -i eth0

Summary of grep options used:

OptionMeaning
-iCase-insensitive search
-nShow line numbers
-A2Show 2 lines after the matched line
-B2Show 2 lines before the matched line
-vInvert match (show lines that do not match)
--colorHighlight matched strings in color

🔁 I/O Redirection in Linux

🟥 1. Using > and >>

SymbolFunctionExample
>Redirects and overwrites filels > files.txt
>>Redirects and appends to filedate >> log.txt
  • > will create the file if it doesn't exist, or overwrite it if it does.

  • >> will create the file if it doesn't exist, or append to it if it does.

🧪 Examples:

echo "Hello" > test.txt       # Creates test.txt with "Hello"
echo "World" >> test.txt      # Appends "World" to test.txt

🟩 2. Using the tee Command

  • The tee command reads from standard input and writes to both standard output and one or more files.

✅ Syntax:

command | tee filename
  • Use -a to append instead of overwrite.

🧪 Example:

ls | tee files.txt
  • Displays output of ls on the screen and also saves it to files.txt.
ls | tee -a files.txt
  • Appends output of ls to files.txt.

📝 Summary Table:

TaskCommand Example
Overwrite file with outputecho "hi" > file.txt
Append output to fileecho "hi again" >> file.txt
Display and write to file`ls
Append with display & write`ls
0
Subscribe to my newsletter

Read articles from mounika pogakula directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

mounika pogakula
mounika pogakula

Hi, I'm Mounika Pogakula, a passionate DevOps enthusiast transitioning into tech with a strong foundation in Linux, Networking, and Cloud fundamentals. I hold a B.Sc. in Computers and a Networking Essentials certification. I’m diving deep into tools like Git, Docker, Jenkins, Kubernetes, and AWS, while sharing my real-world learning journey, hands-on practice, and beginner-friendly insights here on Hashnode. 📚 I'm especially focused on simplifying complex topics, documenting my progress, and building high-impact projects that help me—and others—grow in the DevOps space. Let’s learn, build, and grow together 💻✨