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

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 💻✨
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
Search for a word in a file:
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
- Case-insensitive search:
Use-ito ignore case differences (matchKernel,kernel,KERNEL, etc.).
grep -i kernel ktfile
Output example:
Welcome to Kernel Tech
Welcome to kernel Tech
Welcome to KERNEL TECH
- Show lines matching the word and 2 lines after it:
Use-Aoption (After context).
grep -nA2 wheel /etc/group
-n shows line numbers; -A2 shows 2 lines after match.
- Show lines matching the word and 2 lines before it:
Use-Boption (Before context).
grep -nB2 wheel /etc/group
- Exclude lines containing a word:
Use-vto 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'
command1produces output.grepfilters 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
.txtfiles.
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:
| Option | Meaning |
-i | Case-insensitive search |
-n | Show line numbers |
-A2 | Show 2 lines after the matched line |
-B2 | Show 2 lines before the matched line |
-v | Invert match (show lines that do not match) |
--color | Highlight matched strings in color |
🔁 I/O Redirection in Linux
🟥 1. Using > and >>
| Symbol | Function | Example |
> | Redirects and overwrites file | ls > files.txt |
>> | Redirects and appends to file | date >> 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
teecommand reads from standard input and writes to both standard output and one or more files.
✅ Syntax:
command | tee filename
- Use
-ato append instead of overwrite.
🧪 Example:
ls | tee files.txt
- Displays output of
lson the screen and also saves it tofiles.txt.
ls | tee -a files.txt
- Appends output of
lstofiles.txt.
📝 Summary Table:
| Task | Command Example |
| Overwrite file with output | echo "hi" > file.txt |
| Append output to file | echo "hi again" >> file.txt |
| Display and write to file | `ls |
| Append with display & write | `ls |
