Advanced Linux Text Processing Commands (Vim, Sed, Awk, Cut, Sort, Uniq)

Advanced Linux Text Processing Commands (Vim, Sed, Awk, Cut, Sort, Uniq)

In the final part of this Linux command series, we focus on advanced text editing and data manipulation tools that are widely used in real-world Linux environments. These commands help you edit files faster, analyze data efficiently, and automate text-based tasks with confidence.This guide covers practical usage of vim, advanced sed operations, powerful awk patterns, and supporting tools like cut, sort, uniq, and tr. These are essential skills for system administrators, DevOps engineers, and anyone working heavily with logs, CSV files, or configuration files.

Command Summary

CommandDescription
vim <file>Open a file using the Vim editor
iSwitch to insert mode
EscReturn to command mode
:wSave the file
:qQuit Vim
:wqSave and quit
:q!Quit without saving changes
yyCopy the current line
pPaste copied content
ddDelete the current line
uUndo the last action
Ctrl+rRedo the last undone action
/wordSearch forward for a word
?wordSearch backward for a word
:s/old/newReplace first occurrence in a line
:s/old/new/gReplace all occurrences in a line
sed -n '5,10p'Print specific line ranges
sed 's/old/new/g'Replace text in output
sed -iEdit file content in-place
awk '{print $1}'Print first column
awk '/pattern/'Filter lines matching a pattern
cut -d',' -f2Extract a column from CSV
sortSort file content
uniqRemove duplicate lines
trTranslate or delete characters

sed – Stream Editor

1.Replace ERROR with ISSUE

Input (logfile.txt):

INFO: System boot
ERROR: Disk full
WARNING: Low memory
ERROR: Network timeout

Command:

sed 's/ERROR/ISSUE/g' logfile.txt

Output:

INFO: System boot
ISSUE: Disk full
WARNING: Low memory
ISSUE: Network timeout

This replaces all occurrences of ERROR with ISSUE without modifying the original file.

2. Remove WARNING lines

Input (logfile.txt):

INFO: System boot
ERROR: Disk full
WARNING: Low memory
ERROR: Network timeout

Command:

sed '/WARNING/d' logfile.txt

Output:

INFO: System boot
ERROR: Disk full
ERROR: Network timeout

Deletes all lines containing the word WARNING.

awk – Pattern Scanning and Processing

1.Print first column

Input (data.csv):

id,name,score
1,Alice,85
2,Bob,92
3,Charlie,78

Command:

awk -F',' '{print $1}' data.csv

Output:

id
1
2
3

Prints the first column (IDs) from the CSV file.

2.Filter rows with score > 80

Input (data.csv):

id,name,score
1,Alice,85
2,Bob,92
3,Charlie,78

Command:

awk -F',' 'NR>1 && $3 > 80 {print}' data.csv

Output:

1,Alice,85
2,Bob,92

Skips the header and prints rows where the score column exceeds 80.

uniq – Remove Duplicate Lines

1. Remove adjacent duplicates

Input (names.txt):

Alice
Bob
Charlie
Alice
Bob

Command:

uniq names.txt

Output:

Alice
Bob
Charlie
Alice
Bob

Removes only consecutive duplicate lines.

2. Show only duplicates

Input (names.txt):

Alice
Bob
Charlie
Alice
Bob

Command:

sort names.txt | uniq -d

Output:

Alice
Bob

Shows only names that appear more than once.

tr – Translate Characters

1.Convert lowercase to uppercase

Input (names.txt):

Alice
Bob
Charlie

Command:

tr 'a-z' 'A-Z' < names.txt

Output:

ALICE
BOB
CHARLIE

Converts all lowercase letters to uppercase.

Mastering these Linux text processing tools will help you automate repetitive tasks, analyze logs efficiently, and work confidently with structured data. These commands are especially valuable in DevOps, system administration, and production troubleshooting workflows.

Be the first to comment

Leave a Reply

Your email address will not be published.


*