ตัวอย่างการดึงข้อมูลจากไฟล์ Log

wk
2 min readJan 10, 2020

--

ตัวอย่าง 1

แสดงเฉพาะบรรทัดที่มีคำว่า format has not been recognized

macOS / Linux

cat log-20200108.log | grep "format has not been recognized"

Windows

type log-20200108.log | find "file format has not been recognized"

ตัวอย่าง 2

นับจำนวนบรรทัดที่มีคำว่า format has not been recognized

macOS / Linux

cat log-20200108.log | grep -c "format has not been recognized"

Windows

type log-20200108.log | find /c "file format has not been"

ตัวอย่าง 3

ดึงชื่อไฟล์ pdf ที่อยู่ท้ายบรรทัด

macOS / Linux

cat log-20200108.log | grep "format has not" | grep -o "[^\]*.PDF"

Windows (PowerShell)

cat log-20200108.log `
| Select-String -Pattern "(format has not)*\\([a-z0-9-]+.pdf)$" `
| %{$_.Matches.Groups[2].Value}

ตัวอย่าง 4

ดึงชื่อไฟล์ pdf แล้วเก็บลงใน error.txt โดยใช้ output redirect (>)

macOS / Linux

cat log-20200108.log \
| grep "format has not" \
| grep -o "" > error.txt

Windows (PowerShell)

cat log-20200108.log `
| Select-String -Pattern "(format has not)*\\([a-z0-9-]+.pdf)$" `
| %{$_.Matches.Groups[2].Value} > error.txt

--

--

No responses yet