ใช้ Powershell แบบ Linux Shell Script

รวบรวมการทำงานกับ Powershell บน Windows โดยเทียบเคียงกับการใช้ ShellScript บน Linux
(ทะยอยเขียนบันทึก โปรดติดตามเรื่อยๆ)

  • เลือกบางบรรทัดจากไฟล์ที่มีคำที่ต้องการอยู่
    shellscript: grep someword textfile.txt
    powershell: select-string “someword” textfile.txt
  • เลือกบางบรรทัดจากไฟล์ แล้วแยกด้วยเครื่องหมาย : เพื่อเอาฟิลด์ที่ 3
    shellscript: grep someword textfile.txt | awk -f ‘{FS=”:”}{print $3}’
    powershell: select string “someword” textfile.txt | % { $_.line.split(‘:’)[2];  }
  • เลือกบางบรรทัดจากไฟล์ แล้วเอาลงไฟล์
    shellscript: grep someword textfile.txt  > output.txt
    powershell: select string “someword” textfile.txt | foreach-object {$_.line} > output.txt
  • ดูท้ายไฟล์ตลอดเวลา (ไม่รู้จะใช้คำว่าอะไรดี) และเริ่มดูโดยเอา 3 บรรทัดสุดท้าย ไม่ใช่เริ่มทั้งไฟลฺ์
    shellscript: tail -f mydata.log
    powershell: get-content mydata.log -wait -tail 3