Day-4 text processing cmnds In Linux

Table of contents
- π What is Text Processing?
- Example:
- Linux Text Processing Commands
- β 1. awk β Extract and process columns
- β 2. sed β Replace or edit text
- . sed β Stream editor for text substitution and filtering
- sed stands for stream editor, which is used to search a word in the file and replace it with the word required to be in the output
- Note: it will only modify the output, but there will be no change in the original file.
- 4. cut β Extract specific columns or characters
- 5. sort β Sort lines in a file
- 6. uniq β Filter duplicate adjacent lines
- 7. tr β Translate or delete characters
- 8. wc β Count lines, words, characters
- π§ Linux Filter Commands β Quick Reference Table

π What is Text Processing?
Text processing means working with text data to read, change, organize, or analyze it.
It involves manipulating the actual content of text files or streams.
You can extract parts of the text, replace words, count data, sort lines, or format output.
Itβs like editing or analyzing text to get useful information.
Example:
Extracting usernames from a file.
Changing all "error" words to "warning".
Counting how many words are in a documents
Linux Text Processing Commands
sed β Stream editor to find & replace text, filter lines
awk β Powerful column/field extraction and processing tool
cut β Extract specific columns or character ranges
sort β Sort lines alphabetically or numerically
uniq β Remove duplicate adjacent lines
tr β Translate or delete characters (e.g., case conversion)
wc β Count lines, words, characters
β
1. awk
β Extract and process columns
awk
is a powerful text-processing tool that works on column-based data.
Syntax:
awk '{print $1, $3}' filename
Example:
File: data.txt
Alice 24 Engineer
Bob 30 Doctor
Cara 28 Teacher
Command:
awk '{print $1, $3}' data.txt
Output:
Alice Engineer
Bob Doctor
Cara Teacher
π Explanation: Prints 1st and 3rd columns (Name and Job).
β
2. sed
β Replace or edit text
. sed β Stream editor for text substitution and filtering
sed stands for stream editor, which is used to search a word in the file and replace it with the word required to be in the output
Note: it will only modify the output, but there will be no change in the original file.
sed
is a stream editor used to find and replace text or edit in-place.
Syntax:
sed 's/old/new/' filename
Example:
echo "I like cats" | sed 's/cats/dogs/'
Output:
I like dogs
π Explanation: Replaces the word "cats" with "dogs".
To replace in a file permanently:
sed -i 's/cats/dogs/' file.txt
Syntax:
sed 's/old/new/g' filename
Example:
File: file.txt
Hello world
Hello everyone
Command:
sed 's/Hello/Hi/g' file.txt
Output:
Hi world
Hi everyone
- π Explanation:
Replaces all occurrences of "Hello" with "Hi" in each line.
4. cut β Extract specific columns or characters
Syntax:
cut -d'delimiter' -f field_numbers filename
Example:
File: /etc/passwd (sample line)
root:x:0:0:root:/root:/bin/bash
Command:
cut -d':' -f1 /etc/passwd
Output:
root
π Explanation:
Extracts and prints the first field (username) from colon-separated lines.
5. sort β Sort lines in a file
Syntax:
sort filename
Example:
File: names.txt
Bob
Alice
Cara
Command:
sort names.txt
Output:
Alice
Bob
Cara
π Explanation:
Sorts lines alphabetically in ascending order.
6. uniq β Filter duplicate adjacent lines
Syntax:
uniq filename
Example:
File: sorted.txt
Alice
Alice
Bob
Cara
Cara
Cara
Command:
uniq sorted.txt
Output:
Alice
Bob
Cara
π Explanation:
Removes duplicate consecutive lines (use sort
first for non-adjacent duplicates).
7. tr β Translate or delete characters
Syntax:
tr SET1 SET2
Example:
echo 'hello' | tr 'a-z' 'A-Z'
Output:
HELLO
π Explanation:
Converts all lowercase letters to uppercase.
8. wc β Count lines, words, characters
Syntax:
wc -l filename
Example:
File: file.txt
Hello world
This is Linux
Command:
wc -l file.txt
Output:
2 file.txt
π Explanation:
Counts the number of lines in the file.
π§ Linux Filter Commands β Quick Reference Table
Command | Purpose / Use Case | Example |
sed | Stream editor to find & replace, delete, or filter lines | sed 's/old/new/' file.txt |
awk | Powerful tool for field/column processing and pattern matching | awk '{print $1}' file.txt |
cut | Extract specific columns or character ranges | cut -d',' -f1 data.csv |
sort | Sort lines alphabetically or numerically | sort names.txt |
uniq | Remove duplicate adjacent lines | `sort names.txt |
tr | Translate or delete characters (e.g., case change) | tr 'a-z' 'A-Z' < file.txt |
wc | Count lines, words, characters in input | wc -l file.txt |
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 π»β¨