Advanced Filters
GREP COMMAND
The grep command is an advanced filter used to search for a specific pattern or word in
a file and display matching lines.
It is mainly used for text searching.
Syntax
grep [options] pattern filename
File: [Link]
Error: Disk full
Warning: Low memory
Error: File not found
Info: Process started
error: Permission denied
Optio
Purpose Command Output
n
Error: Disk full
-i Ignore case grep -i "error" [Link] Error: File not found
error: Permission denied
1:Error: Disk full
-n Line number grep -n "Error" [Link]
3:Error: File not found
Error: Disk full
Exclude Warning: Low memory
-v grep -v "Info" [Link]
pattern Error: File not found
error: Permission denied
Count
-c grep -c "Error" [Link] 2
matches
Match whole
-w grep -w "Error" [Link] Error: Disk fullError: File not found
word
Show
-l grep -l "Disk" *.txt [Link]
filename
Using grep with Pipe
grep -i "error" [Link] | cut -d ":" -f 1 | sort | uniq
Output
Error
error
Regular Expression (BRE) – Meaning
A regular expression is a pattern used to match text in commands like grep and
sed.
File: [Link]
Amit Delhi 80
Anu Pune 75
Ravi Mumbai 90
Neha Delhi 85
Cat Tool 3.14
Symbol What it does Command Output
Any single
. grep "C.t" [Link] Cat Tool 3.14
character
Zero or more of Amit Delhi 80
* grep "De*" [Link]
previous char Neha Delhi 85
Amit Delhi 80
^ Start of line grep "^A" [Link]
Anu Pune 75
$ End of line grep "80$" [Link] Amit Delhi 80
[] One char from set grep "[AN]mit" [Link] Amit Delhi 80
Amit Delhi 80
[^ ] NOT in set grep "[^0-9]" [Link]
Anu Pune 75
Symbol What it does Command Output
Ravi Mumbai 90
Neha Delhi 85
Cat Tool 3.14
SED COMMAND
The sed command (stream editor) is used to modify file content such as replace, delete,
or print lines without opening the file.
Syntax
sed 'command' filename
File: [Link]
101 Ravi Delhi
102 Anu Mumbai
103 Ravi Pune
104 Neha Delhi
Option Purpose Command Output
Replace 101 Rahul Delhi
first 102 Anu Mumbai
s/old/new/ sed 's/Ravi/Rahul/' [Link]
match 103 Rahul Pune
per line 104 Neha Delhi
Replace 101 Ravi Bhopal
all 102 Anu Mumbai
s/old/new/g
matches sed 's/Delhi/Bhopal/g' [Link] 103 Ravi Pune
per line 104 Neha Bhopal
Delete 101 Ravi Delhi
d line sed '2d' [Link] 103 Ravi Pune
number 104 Neha Delhi
-n p Print line sed -n '3p' [Link] 103 Ravi Pune
101 Ravi Delhi
Delete
/pattern/d sed '/Mumbai/d' [Link] 103 Ravi Pune
by match
104 Neha Delhi
grep Ravi [Link] | sed 's/Ravi/Employee/'
Output
101 Employee Delhi
103 Employee Pune
AWK COMMAND
awk is used to process structured data (columns) and perform calculations.
Syntax
awk 'pattern { action }' filename
Files: [Link]
1 Amit 78
2 Riya 85
3 Neha 92
4 Suman 67
Option /
Purpose Command Output
Variable
1
2
$1 First field awk '{print $1}' [Link]
3
4
Amit 78
Multiple Riya 85
$2,$3 awk '{print $2,$3}' [Link]
fields Neha 92
Suman 67
Option /
Purpose Command Output
Variable
Line 1 1 Amit 78
NR awk '{print NR,$0}' [Link]
number 2 2 Riya 85…
NF Field count awk '{print NF}' [Link] 3
Riya
Condition Filter data awk '$3>80 {print $2}' [Link]
Neha
Using awk with Pipe
awk '{print $2}' [Link] | sort | uniq
Output
Amit
Neha
Riya
Suman
Formatting Output – printf
Meaning
printf is used to display output in a formatted way (aligned columns, fixed width), unlike echo.
It is mostly used with awk.
File: [Link]
Amit 30000
Riya 45000
Neha 52000
Printf – Format Specifiers Table
Format What it does
%s Print string
%d Print integer
%f Print floating number
%-10s Left-align string (width 10)
Format What it does
%10s Right-align string (width 10)
%.2f Print float with 2 decimals
\n New line
\t Tab space
Examples
Purpose Command Output
Amit 30000
Simple formatted
awk '{printf "%s %d\n",$1,$2}' [Link] Riya 45000
output
Neha 52000
Amit 30000
Column alignment awk '{printf "%-10s %d\n",$1,$2}' [Link] Riya 45000
Neha 52000
Amit 30000
Right alignment awk '{printf "%10s %d\n",$1,$2}' [Link] Riya 45000
Neha 52000
Amit 30000
Add tab space awk '{printf "%s\t%d\n",$1,$2}' [Link] Riya 45000
Neha 52000