ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
sed (stream editor) ရဲ့ အသုံးအများဆုံး pattern က sed 's/old/new/' file ပါ — file ထဲက 'old' ဆိုတဲ့ text ကို 'new' နဲ့ ပြောင်းပေးတာပါ (s = substitute)။ g flag (sed 's/old/new/g') ထည့်ရင် line တစ်ကြောင်းစီထဲက ကြိမ်ဖန်များစွာ ကိုက်ညီတဲ့အခါ အားလုံးကို ပြောင်းပေးပါတယ်, g မပါရင် line တိုင်းရဲ့ ပထမကိုက်ညီမှုပဲ ပြောင်းပါတယ်။ awk ကတော့ column-based data (CSV, log file) အတွက် — awk '{print $1}' file က line တိုင်းရဲ့ ပထမ column ($1) ကိုပဲ ထုတ်ပါတယ်, $2, $3 စသည်ဖြင့် column နံပါတ်အလိုက် access လုပ်လို့ရပါတယ်။
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
config ဖိုင်ထဲက localhost ကို production-server.com နဲ့ ပြောင်းချင်ရင် sed 's/localhost/production-server.com/g' config.txt (output ကို ကြည့်ရုံပဲ, -i flag ထပ်ထည့်ရင် ဖိုင်ကို တကယ်ပြင်) — sed -i လုပ်ခင် backup ယူထားတာ ပိုလုံခြုံပါတယ်။ ps aux | awk '{print $2, $11}' က process list ထဲက PID (column 2) နဲ့ command name (column 11) ကိုပဲ ခွဲထုတ်ပြပါတယ် — log/CSV ဖိုင်ကနေ column အတိအကျ ထုတ်ချင်တဲ့အခါ awk ကို pipe နဲ့ တွဲသုံးလေ့ရှိပါတယ်။
အတူတူ terminal ထဲ စမ်းမယ်
echo "hello world" | sed 's/world/linux/'
sed 's/localhost/prod.example.com/g' config.txt
ps aux | awk '{print $2, $11}'
echo "a,b,c" | awk -F',' '{print $2}'hello world ကို sed နဲ့ run ရင် 'hello linux' အဖြစ် ပြောင်းသွားသည်ကို terminal ပေါ်တွင် တွေ့ရမည်။၅ မိနစ် စမ်းကြည့်
'I like cats' ဆိုတဲ့ line တစ်ကြောင်းကို echo နဲ့ ဖန်တီးပြီး sed 's/cats/dogs/' ကို pipe နဲ့ တွဲ run ကြည့်ပါ။
သတိလေးတစ်ချက်
sed -i (in-place edit) ကို production config ဖိုင်ပေါ်မှာ run ခင် cp file file.backup လုပ်ထားတာ လုံခြုံဆုံးပါ။