12 คำสั่งที่มีประโยชน์สำหรับการกรองข้อความเพื่อการทำงานของไฟล์อย่างมีประสิทธิภาพใน Linux
ในบทความนี้ เราจะตรวจสอบเครื่องมือบรรทัดคำสั่งจำนวนหนึ่งที่ทำหน้าที่เป็นตัวกรองใน Linux ตัวกรอง คือโปรแกรมที่อ่านอินพุตมาตรฐาน ดำเนินการกับตัวกรองนั้น และเขียนผลลัพธ์ไปยังเอาต์พุตมาตรฐาน
ด้วยเหตุนี้ จึงสามารถใช้เพื่อประมวลผลข้อมูลด้วยวิธีที่มีประสิทธิภาพ เช่น การปรับโครงสร้างเอาต์พุตเพื่อสร้างรายงานที่เป็นประโยชน์ การแก้ไขข้อความในไฟล์ และงานการดูแลระบบอื่นๆ อีกมากมาย
ด้วยเหตุนี้ ด้านล่างนี้คือไฟล์หรือตัวกรองข้อความที่มีประโยชน์บางส่วนใน Linux
1. คำสั่ง Awk
Awk เป็นภาษาสแกนและประมวลผลรูปแบบที่โดดเด่น สามารถใช้เพื่อสร้างตัวกรองที่มีประโยชน์ใน Linux คุณสามารถเริ่มใช้งานได้โดยอ่านซีรี่ส์ Awk ตอนที่ 1 ถึงตอนที่ 13 ของเรา
นอกจากนี้ โปรดอ่านหน้าคู่มือ awk เพื่อดูข้อมูลเพิ่มเติมและตัวเลือกการใช้งาน:
man awk
2. คำสั่งเซด
sed เป็นเครื่องมือแก้ไขสตรีมที่ทรงพลังสำหรับการกรองและแปลงข้อความ เราได้เขียนบทความที่เป็นประโยชน์สองบทความเกี่ยวกับ sed แล้ว ซึ่งคุณสามารถอ่านได้ที่นี่:
- วิธีใช้คำสั่ง GNU 'sed' เพื่อสร้าง แก้ไข และจัดการไฟล์ใน Linux
- 15 เคล็ดลับและเทคนิคคำสั่ง 'sed' ที่เป็นประโยชน์สำหรับงานการดูแลระบบ Linux รายวัน
หน้า sed man ได้เพิ่มตัวเลือกการควบคุมและคำแนะนำ:
man sed
3. คำสั่ง Grep, Egrep, Fgrep, Rgrep
ตัวกรองเหล่านี้บรรทัดเอาต์พุตที่ตรงกับรูปแบบที่กำหนด พวกเขาอ่านบรรทัดจากไฟล์หรืออินพุตมาตรฐาน และพิมพ์บรรทัดที่ตรงกันทั้งหมดเป็นเอาต์พุตมาตรฐานตามค่าเริ่มต้น
หมายเหตุ: โปรแกรมหลักคือ grep รูปแบบต่างๆ จะเหมือนกับการใช้ตัวเลือก grep เฉพาะด้านล่าง (และยังคงใช้สำหรับความเข้ากันได้แบบย้อนหลัง):
egrep = grep -E
fgrep = grep -F
rgrep = grep -r
ด้านล่างนี้คือคำสั่ง grep พื้นฐานบางส่วน:
tecmint@TecMint ~ $ grep "aaronkilik" /etc/passwd
aaronkilik:x:1001:1001::/home/aaronkilik:
tecmint@TecMint ~ $ cat /etc/passwd | grep "aronkilik"
aaronkilik:x:1001:1001::/home/aaronkilik:
คุณสามารถอ่านเพิ่มเติมเกี่ยวกับความแตกต่างระหว่าง Grep, Egrep และ Fgrep ใน Linux ได้อย่างไร
4.หัวหน้าสั่งการ
head ใช้เพื่อแสดงส่วนแรกของไฟล์ โดยจะแสดงบรรทัด 10 บรรทัดแรกตามค่าเริ่มต้น คุณสามารถใช้แฟล็ก -n
num เพื่อระบุจำนวนบรรทัดที่จะแสดง:
tecmint@TecMint ~ $ head /var/log/auth.log
Jan 2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session closed for user root
Jan 2 10:51:34 TecMint sudo: tecmint : TTY=unknown ; PWD=/home/tecmint ; USER=root ; COMMAND=/usr/lib/linuxmint/mintUpdate/checkAPT.py
Jan 2 10:51:34 TecMint sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Jan 2 10:51:39 TecMint sudo: pam_unix(sudo:session): session closed for user root
Jan 2 10:55:01 TecMint CRON[4099]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 2 10:55:01 TecMint CRON[4099]: pam_unix(cron:session): session closed for user root
Jan 2 11:05:01 TecMint CRON[4138]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 2 11:05:01 TecMint CRON[4138]: pam_unix(cron:session): session closed for user root
Jan 2 11:09:01 TecMint CRON[4146]: pam_unix(cron:session): session opened for user root by (uid=0)
tecmint@TecMint ~ $ head -n 5 /var/log/auth.log
Jan 2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session closed for user root
Jan 2 10:51:34 TecMint sudo: tecmint : TTY=unknown ; PWD=/home/tecmint ; USER=root ; COMMAND=/usr/lib/linuxmint/mintUpdate/checkAPT.py
Jan 2 10:51:34 TecMint sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Jan 2 10:51:39 TecMint sudo: pam_unix(sudo:session): session closed for user root
เรียนรู้วิธีใช้คำสั่ง head พร้อมคำสั่ง tail และ cat เพื่อการใช้งานอย่างมีประสิทธิภาพใน Linux
5. คำสั่งหาง
tail ส่งออกส่วนสุดท้าย (10 บรรทัดโดยค่าเริ่มต้น) ของไฟล์ ใช้สวิตช์ num -n
เพื่อระบุจำนวนบรรทัดที่จะแสดง
คำสั่งด้านล่างจะแสดงบรรทัดสุดท้ายของไฟล์ที่ระบุ 5:
tecmint@TecMint ~ $ tail -n 5 /var/log/auth.log
Jan 6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22.
Jan 6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22.
Jan 6 13:01:27 TecMint sshd[1269]: Received SIGHUP; restarting.
Jan 6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22.
Jan 6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22.
นอกจากนี้ tail ยังมีตัวเลือกพิเศษ -f
สำหรับการดูการเปลี่ยนแปลงในไฟล์แบบเรียลไทม์ (โดยเฉพาะไฟล์บันทึก)
คำสั่งต่อไปนี้จะช่วยให้คุณสามารถติดตามการเปลี่ยนแปลงในไฟล์ที่ระบุ:
tecmint@TecMint ~ $ tail -f /var/log/auth.log
Jan 6 12:58:01 TecMint sshd[1269]: Server listening on :: port 22.
Jan 6 12:58:11 TecMint sshd[1269]: Received SIGHUP; restarting.
Jan 6 12:58:12 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22.
Jan 6 12:58:12 TecMint sshd[1269]: Server listening on :: port 22.
Jan 6 13:01:27 TecMint sshd[1269]: Received SIGHUP; restarting.
Jan 6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22.
Jan 6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22.
Jan 6 13:01:27 TecMint sshd[1269]: Received SIGHUP; restarting.
Jan 6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22.
Jan 6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22.
อ่านหน้าคู่มือ ส่วนท้าย เพื่อดูรายการตัวเลือกและคำแนะนำการใช้งานทั้งหมด:
man tail
6. คำสั่งเรียงลำดับ
sort ใช้เพื่อเรียงลำดับบรรทัดของไฟล์ข้อความหรือจากอินพุตมาตรฐาน
ด้านล่างนี้คือเนื้อหาของไฟล์ชื่อ domains.list:
tecmint@TecMint ~ $ cat domains.list
linux-console.net
linux-console.net
news.linux-console.net
news.linux-console.net
linuxsay.com
linuxsay.com
windowsmint.com
windowsmint.com
คุณสามารถเรียกใช้คำสั่ง sort แบบง่ายๆ เพื่อจัดเรียงเนื้อหาไฟล์ดังนี้:
tecmint@TecMint ~ $ sort domains.list
linuxsay.com
linuxsay.com
news.linux-console.net
news.linux-console.net
linux-console.net
linux-console.net
windowsmint.com
windowsmint.com
คุณสามารถใช้คำสั่ง sort ได้หลายวิธี อ่านบทความที่มีประโยชน์เกี่ยวกับคำสั่ง sort ดังนี้:
- 14 ตัวอย่างที่เป็นประโยชน์ของคำสั่ง 'sort' ของ Linux - ตอนที่ 1
- 7 ตัวอย่างคำสั่ง 'เรียงลำดับ' Linux ที่น่าสนใจ - ตอนที่ 2
- วิธีค้นหาและจัดเรียงไฟล์ตามวันที่และเวลาที่แก้ไข
- วิธีจัดเรียงเอาต์พุตของคำสั่ง 'ls' ตามวันที่และเวลาที่แก้ไขล่าสุด
7. คำสั่งยูนิค
คำสั่ง uniq ใช้เพื่อรายงานหรือละเว้นบรรทัดที่ซ้ำ โดยจะกรองบรรทัดจากอินพุตมาตรฐาน และเขียนผลลัพธ์ไปยังเอาต์พุตมาตรฐาน
หลังจากรัน sort บนอินพุตสตรีม คุณสามารถลบบรรทัดที่ซ้ำกันด้วย uniq ดังตัวอย่างด้านล่าง
หากต้องการระบุจำนวนครั้งของบรรทัด ให้ใช้ตัวเลือก -c
และละเว้นความแตกต่างในกรณีขณะเปรียบเทียบโดยรวมตัวเลือก -i
:
tecmint@TecMint ~ $ cat domains.list
linux-console.net
linux-console.net
news.linux-console.net
news.linux-console.net
linuxsay.com
linuxsay.com
windowsmint.com
tecmint@TecMint ~ $ sort domains.list | uniq -c
2 linuxsay.com
2 news.linux-console.net
2 linux-console.net
1 windowsmint.com
อ่านหน้าคู่มือ uniq เพื่อดูข้อมูลการใช้งานและการตั้งค่าสถานะเพิ่มเติม:
man uniq
8. คำสั่ง fmt
fmt ตัวจัดรูปแบบข้อความที่เรียบง่ายที่สุด จะจัดรูปแบบย่อหน้าในไฟล์ที่ระบุและพิมพ์ผลลัพธ์เป็นเอาต์พุตมาตรฐาน
ต่อไปนี้เป็นเนื้อหาที่แยกมาจากไฟล์ domain-list.txt:
1.linux-console.net 2.news.linux-console.net 3.linuxsay.com 4.windowsmint.com
หากต้องการฟอร์แมตเนื้อหาด้านบนเป็นรายการมาตรฐาน ให้รันคำสั่งต่อไปนี้โดยใช้สวิตช์ -w
เพื่อกำหนดความกว้างของบรรทัดสูงสุด:
tecmint@TecMint ~ $ cat domain-list.txt
1.linux-console.net 2.news.linux-console.net 3.linuxsay.com 4.windowsmint.com
tecmint@TecMint ~ $ fmt -w 1 domain-list.txt
1.linux-console.net
2.news.linux-console.net
3.linuxsay.com
4.windowsmint.com
9. คำสั่งประชาสัมพันธ์
คำสั่ง pr แปลงไฟล์ข้อความหรืออินพุตมาตรฐานสำหรับการพิมพ์ ตัวอย่างเช่น ในระบบ Debian คุณสามารถแสดงรายการแพ็คเกจที่ติดตั้งทั้งหมดได้ดังนี้:
dpkg -l
หากต้องการจัดระเบียบรายการในหน้าและคอลัมน์ที่พร้อมสำหรับการพิมพ์ ให้ใช้คำสั่งต่อไปนี้
tecmint@TecMint ~ $ dpkg -l | pr --columns 3 -l 20
2017-01-06 13:19 Page 1
Desired=Unknown/Install ii adduser ii apg
| Status=Not/Inst/Conf- ii adwaita-icon-theme ii app-install-data
|/ Err?=(none)/Reinst-r ii adwaita-icon-theme- ii apparmor
||/ Name ii alsa-base ii apt
+++-=================== ii alsa-utils ii apt-clone
ii accountsservice ii anacron ii apt-transport-https
ii acl ii apache2 ii apt-utils
ii acpi-support ii apache2-bin ii apt-xapian-index
ii acpid ii apache2-data ii aptdaemon
ii add-apt-key ii apache2-utils ii aptdaemon-data
2017-01-06 13:19 Page 2
ii aptitude ii avahi-daemon ii bind9-host
ii aptitude-common ii avahi-utils ii binfmt-support
ii apturl ii aview ii binutils
ii apturl-common ii banshee ii bison
ii archdetect-deb ii baobab ii blt
ii aspell ii base-files ii blueberry
ii aspell-en ii base-passwd ii bluetooth
ii at-spi2-core ii bash ii bluez
ii attr ii bash-completion ii bluez-cups
ii avahi-autoipd ii bc ii bluez-obexd
.....
ธงที่ใช้ในที่นี้คือ:
--column
กำหนดจำนวนคอลัมน์ที่สร้างขึ้นในผลลัพธ์-l
ระบุความยาวของหน้า (ค่าเริ่มต้นคือ 66 บรรทัด)
10. คำสั่ง tr
เครื่องมือนี้แปลหรือลบอักขระจากอินพุตมาตรฐานและเขียนผลลัพธ์ไปยังเอาต์พุตมาตรฐาน
ไวยากรณ์สำหรับการใช้ tr เป็นดังนี้:
tr options set1 set2
ดูตัวอย่างด้านล่าง ในคำสั่งแรก set1( [:upper:] )
แทนตัวพิมพ์ของอักขระอินพุต (ตัวพิมพ์ใหญ่ทั้งหมด)
จากนั้น set2([:lower:])
แสดงถึงกรณีที่อักขระผลลัพธ์จะเป็น มันก็เหมือนกันในตัวอย่างที่สอง และ Escape Sequence \n
หมายถึงการพิมพ์เอาต์พุตในบรรทัดใหม่:
tecmint@TecMint ~ $ echo "WWW.TECMINT.COM" | tr [:upper:] [:lower:]
linux-console.net
tecmint@TecMint ~ $ echo "news.linux-console.net" | tr [:lower:] [:upper:]
NEWS.TECMINT.COM
11. คำสั่งเพิ่มเติม
คำสั่ง เพิ่มเติม เป็นตัวกรองการอ่านไฟล์ที่มีประโยชน์ ซึ่งสร้างขึ้นเพื่อการดูใบรับรองโดยทั่วไป โดยจะแสดงเนื้อหาไฟล์ในรูปแบบหน้าเว็บ ซึ่งผู้ใช้สามารถกด [Enter] เพื่อดูข้อมูลเพิ่มเติม
คุณสามารถใช้มันเพื่อดูไฟล์ขนาดใหญ่ได้เช่น:
tecmint@TecMint ~ $ dmesg | more
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Initializing cgroup subsys cpuacct
[ 0.000000] Linux version 4.4.0-21-generic (buildd@lgw01-21) (gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2) ) #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 (Ubuntu 4.4.0-21.37-generic
4.4.6)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.4.0-21-generic root=UUID=bb29dda3-bdaa-4b39-86cf-4a6dc9634a1b ro quiet splash vt.handoff=7
[ 0.000000] KERNEL supported cpus:
[ 0.000000] Intel GenuineIntel
[ 0.000000] AMD AuthenticAMD
[ 0.000000] Centaur CentaurHauls
[ 0.000000] x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x02: 'SSE registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x04: 'AVX registers'
[ 0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format.
[ 0.000000] x86/fpu: Using 'eager' FPU context switches.
[ 0.000000] e820: BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d3ff] usable
[ 0.000000] BIOS-e820: [mem 0x000000000009d400-0x000000000009ffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000a56affff] usable
[ 0.000000] BIOS-e820: [mem 0x00000000a56b0000-0x00000000a5eaffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000a5eb0000-0x00000000aaabefff] usable
--More--
12. คำสั่งน้อยลง
less ตรงกันข้ามกับคำสั่ง more ด้านบน แต่มีคุณสมบัติพิเศษและเร็วกว่าเล็กน้อยหากใช้ไฟล์ขนาดใหญ่
ใช้ในลักษณะเดียวกับเพิ่มเติม:
tecmint@TecMint ~ $ dmesg | less
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Initializing cgroup subsys cpuacct
[ 0.000000] Linux version 4.4.0-21-generic (buildd@lgw01-21) (gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2) ) #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 (Ubuntu 4.4.0-21.37-generic
4.4.6)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.4.0-21-generic root=UUID=bb29dda3-bdaa-4b39-86cf-4a6dc9634a1b ro quiet splash vt.handoff=7
[ 0.000000] KERNEL supported cpus:
[ 0.000000] Intel GenuineIntel
[ 0.000000] AMD AuthenticAMD
[ 0.000000] Centaur CentaurHauls
[ 0.000000] x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x02: 'SSE registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x04: 'AVX registers'
[ 0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format.
[ 0.000000] x86/fpu: Using 'eager' FPU context switches.
[ 0.000000] e820: BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d3ff] usable
[ 0.000000] BIOS-e820: [mem 0x000000000009d400-0x000000000009ffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000a56affff] usable
[ 0.000000] BIOS-e820: [mem 0x00000000a56b0000-0x00000000a5eaffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000a5eb0000-0x00000000aaabefff] usable
:
เรียนรู้ว่าเหตุใดคำสั่ง 'less' จึงเร็วกว่าคำสั่ง 'more' เพื่อการนำทางไฟล์อย่างมีประสิทธิภาพใน Linux
เพียงเท่านี้ โปรดแจ้งให้เราทราบถึงเครื่องมือบรรทัดคำสั่งที่มีประโยชน์ที่ไม่ได้กล่าวถึงในที่นี้ ซึ่งทำหน้าที่เป็นตัวกรองข้อความใน Linux ผ่านทางส่วนความคิดเห็นด้านล่าง