Linux Text Processing — vim, grep, sed, awk

লেসন ৮ / ৩৮

📝 Vim Survival

i              # insert mode
ESC            # normal mode 🔥
:w             # save
:q             # quit
:wq            # save+quit 🔥
:q!            # force quit

gg             # file start
G              # file end
:50            # line 50
/error         # search

dd             # delete line
yy             # copy line
p              # paste
u              # undo
Ctrl+r         # redo

:%s/old/new/g       # global replace
:%s/old/new/gc      # confirm each

🎯 Nano (easier)

Ctrl+O   save
Ctrl+X   exit
Ctrl+W   search
Ctrl+K   cut

🔍 grep

grep -i "error" log.txt
grep -n "TODO" *.php
grep -r "API_KEY" /var/www/
grep -A 3 -B 2 "ERROR" log
grep -c "404" access.log

✏️ sed — Replace

sed -i "s/old/new/g" file.txt
sed -i.bak "s/old/new/" file       # backup
sed -i "/^#/d" config              # delete comments
sed -n "10,20p" file               # print 10-20

# Real
sed -i "s/APP_DEBUG=true/APP_DEBUG=false/" .env
sed -i "s|http://|https://|g" config.php

📊 awk

awk "{print $1}" file
awk -F"," "{print $2}" data.csv
awk "$3 > 100" data.txt

# Top IP visitors
awk "{print $1}" access.log | sort | uniq -c | sort -rn | head -10

# Top URLs
awk "{print $7}" access.log | sort | uniq -c | sort -rn | head

# Status code count
awk "{print $9}" access.log | sort | uniq -c | sort -rn

# Bandwidth
awk "{sum+=$10} END {print sum/1024/1024 \"MB\"}" access.log

🎓 মূল কথা ⭐

vim 10 command যথেষ্ট। sed/awk = log analysis 1 minute। Pipeline thinking = Unix philosophy।