自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux下過(guò)濾文本、實(shí)現(xiàn)高效文件操作的12個(gè)實(shí)用命令

譯文
系統(tǒng) Linux
我們?cè)诒疚闹薪榻B了多款在Linux下充當(dāng)過(guò)濾器的命令行工具。過(guò)濾器是這樣一種程序:讀取標(biāo)準(zhǔn)輸入后,對(duì)它執(zhí)行操作,然后將結(jié)果寫入到標(biāo)準(zhǔn)輸出。過(guò)濾器工具以有效的方式處理信息,比如重構(gòu)輸出以生成實(shí)用報(bào)告,修改文件中的文本,以及處理其他許多系統(tǒng)管理任務(wù)。

【51CTO.com快譯】我們?cè)诒疚闹薪榻B了多款在Linux下充當(dāng)過(guò)濾器的命令行工具。過(guò)濾器是這樣一種程序:讀取標(biāo)準(zhǔn)輸入后,對(duì)它執(zhí)行操作,然后將結(jié)果寫入到標(biāo)準(zhǔn)輸出。

過(guò)濾器工具以有效的方式處理信息,比如重構(gòu)輸出以生成實(shí)用報(bào)告,修改文件中的文本,以及處理其他許多系統(tǒng)管理任務(wù)。

Linux

言歸正傳,下面介紹幾款Linux環(huán)境下實(shí)用的文件或文本過(guò)濾器。

1.Awk命令

Awk是一種出色的模式掃描和處理語(yǔ)言,它可以用來(lái)在Linux下構(gòu)建實(shí)用過(guò)濾器。如果你從頭至尾看過(guò)我們編寫的Awk系列文章:第1部分至第13部分(http://www.tecmint.com/category/awk-command/),就可以開(kāi)始使用它。

另外,還可以參閱awk的參考手冊(cè)頁(yè),了解更多信息和用法選項(xiàng):

  1. $ man awk 

2.Sed命令

sed是一種強(qiáng)大的流編輯器,可用于過(guò)濾和轉(zhuǎn)換文本。我們已經(jīng)編寫過(guò)介紹sed的兩篇實(shí)用文章,你可以在此閱讀:

《如何使用GNU “sed”命令在Linux下創(chuàng)建、編輯和處理文件》(http://www.tecmint.com/sed-command-to-create-edit-and-manipulate-files-in-linux/)

《處理日常Linux系統(tǒng)管理任務(wù)的15個(gè)實(shí)用的“sed”命令技巧和方法》(http://www.tecmint.com/linux-sed-command-tips-tricks/)

sed的參考手冊(cè)頁(yè)添加了控制選項(xiàng)和操作說(shuō)明:

  1. $ man sed 

3.Grep、Egrep、Fgrep和Rgrep命令

這些過(guò)濾器輸出與特定模式匹配的行。它們從文件或標(biāo)準(zhǔn)輸入讀取行,默認(rèn)情況下將所有匹配的行打印輸出到標(biāo)準(zhǔn)輸出。

注意:主程序是grep,幾個(gè)變種與使用特定的grep選項(xiàng)完全一樣(它們?nèi)钥捎糜谙蚝蠹嫒?: 

  1. $ egrep = grep -E 
  2. $ fgrep = grep -F 
  3. $ rgrep = grep -r  

下面是一些基本的grep命令:

  1. tecmint@TecMint ~ $ grep "aaronkilik" /etc/passwd 
  2. aaronkilik:x:1001:1001::/home/aaronkilik: 
  3. tecmint@TecMint ~ $ cat /etc/passwd | grep "aronkilik" 
  4. aaronkilik:x:1001:1001::/home/aaronkilik:  

詳細(xì)內(nèi)容請(qǐng)參閱《Linux下Grep、Egrep和Fgrep之間有何區(qū)別?》(http://www.tecmint.com/difference-between-grep-egrep-and-fgrep-in-linux/)。

4.head命令

head用于顯示文件的最初部分,默認(rèn)情況下輸出頭10行。你可以使用-n num標(biāo)志,指定顯示的行數(shù):

  1. tecmint@TecMint ~ $ head /var/log/auth.log   
  2. Jan  2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session opened for user root by (uid=0) 
  3. Jan  2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session closed for user root 
  4. Jan  2 10:51:34 TecMint sudo:  tecmint : TTY=unknown ; PWD=/home/tecmint ; USER=root ; COMMAND=/usr/lib/linuxmint/mintUpdate/checkAPT.py 
  5. Jan  2 10:51:34 TecMint sudo: pam_unix(sudo:session): session opened for user root by (uid=0) 
  6. Jan  2 10:51:39 TecMint sudo: pam_unix(sudo:session): session closed for user root 
  7. Jan  2 10:55:01 TecMint CRON[4099]: pam_unix(cron:session): session opened for user root by (uid=0) 
  8. Jan  2 10:55:01 TecMint CRON[4099]: pam_unix(cron:session): session closed for user root 
  9. Jan  2 11:05:01 TecMint CRON[4138]: pam_unix(cron:session): session opened for user root by (uid=0) 
  10. Jan  2 11:05:01 TecMint CRON[4138]: pam_unix(cron:session): session closed for user root 
  11. Jan  2 11:09:01 TecMint CRON[4146]: pam_unix(cron:session): session opened for user root by (uid=0) 
  12. tecmint@TecMint ~ $ head  -n 5 /var/log/auth.log   
  13. Jan  2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session opened for user root by (uid=0) 
  14. Jan  2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session closed for user root 
  15. Jan  2 10:51:34 TecMint sudo:  tecmint : TTY=unknown ; PWD=/home/tecmint ; USER=root ; COMMAND=/usr/lib/linuxmint/mintUpdate/checkAPT.py 
  16. Jan  2 10:51:34 TecMint sudo: pam_unix(sudo:session): session opened for user root by (uid=0) 
  17. Jan  2 10:51:39 TecMint sudo: pam_unix(sudo:session): session closed for user root  

5.tail命令

tail輸出文件的末尾部分(默認(rèn)情況下是末尾10行)。使用-n num參數(shù)選項(xiàng)符,即可指定顯示的行數(shù)。

下面這個(gè)命令會(huì)輸出指定文件的末尾5行:

  1. tecmint@TecMint ~ $ tail -n 5 /var/log/auth.log 
  2. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22. 
  3. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22. 
  4. Jan  6 13:01:27 TecMint sshd[1269]: Received SIGHUP; restarting. 
  5. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22. 
  6. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22.  

此外,tail有一個(gè)特殊的選項(xiàng)-f,可用于實(shí)時(shí)查看文件(尤其是日志文件)的變化。

下面這個(gè)命令讓你能夠密切關(guān)注指定文件的變化:

  1. tecmint@TecMint ~ $ tail -f /var/log/auth.log 
  2. Jan  6 12:58:01 TecMint sshd[1269]: Server listening on :: port 22. 
  3. Jan  6 12:58:11 TecMint sshd[1269]: Received SIGHUP; restarting. 
  4. Jan  6 12:58:12 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22. 
  5. Jan  6 12:58:12 TecMint sshd[1269]: Server listening on :: port 22. 
  6. Jan  6 13:01:27 TecMint sshd[1269]: Received SIGHUP; restarting. 
  7. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22. 
  8. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22. 
  9. Jan  6 13:01:27 TecMint sshd[1269]: Received SIGHUP; restarting. 
  10. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22. 
  11. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22.  

參閱tail的參考手冊(cè)頁(yè),即可了解完整的用法選項(xiàng)和操作說(shuō)明:

  1. $ man tail 

6.sort命令

sort用于排序文本文件的行或來(lái)自標(biāo)準(zhǔn)輸入的行。

下面是一個(gè)名為domains.list的文件的內(nèi)容:

  1. tecmint@TecMint ~ $ cat domains.list 
  2. tecmint.com 
  3. tecmint.com 
  4. news.tecmint.com 
  5. news.tecmint.com 
  6. linuxsay.com 
  7. linuxsay.com 
  8. windowsmint.com 
  9. windowsmint.com  

你可以運(yùn)行簡(jiǎn)單的sort命令,排序文件內(nèi)容,就像這樣:

  1. tecmint@TecMint ~ $ sort domains.list 
  2. linuxsay.com 
  3. linuxsay.com 
  4. news.tecmint.com 
  5. news.tecmint.com 
  6. tecmint.com 
  7. tecmint.com 
  8. windowsmint.com 
  9. windowsmint.com  

使用sort命令有好多方式,我們編寫了幾篇實(shí)用文章來(lái)介紹sort命令,如下所示:

《Linux “sort”命令的14個(gè)實(shí)用例子-第1部分》(http://www.tecmint.com/sort-command-linux/)

《7個(gè)有趣的Linux “sort”命令例子-第2部分》(http://www.tecmint.com/linux-sort-command-examples/)

《如何基于修改日期和時(shí)間來(lái)查找和排序文件》(http://www.tecmint.com/find-and-sort-files-modification-date-and-time-in-linux/)

http://www.tecmint.com/sort-ls-output-by-last-modified-date-and-time/

7.uniq命令

uniq命令用于報(bào)告或忽略重復(fù)的行,它可以過(guò)濾來(lái)自標(biāo)準(zhǔn)輸入的行,并將結(jié)果寫入到標(biāo)準(zhǔn)輸出。

對(duì)輸入流運(yùn)行sort后,可以用uniq來(lái)消除重復(fù)的行,如下面這個(gè)例子所示。

為了表明某行出現(xiàn)的次數(shù),可使用-c選項(xiàng),忽視大小寫區(qū)別,同時(shí)通過(guò)加入-i選項(xiàng)來(lái)比較:

  1. tecmint@TecMint ~ $ cat domains.list 
  2. tecmint.com 
  3. tecmint.com 
  4. news.tecmint.com 
  5. news.tecmint.com 
  6. linuxsay.com 
  7. linuxsay.com 
  8. windowsmint.com 
  9. sort domains.list | uniq -c  
  10. 2 linuxsay.com 
  11. 2 news.tecmint.com 
  12. 2 tecmint.com 
  13. 1 windowsmint.com   

閱讀uniq的參考手冊(cè)頁(yè),可進(jìn)一步了解用法信息和標(biāo)志:

  1. $ man uniq 

8.fmt命令

fmt是簡(jiǎn)單的最佳文本格式器,它可以重新格式化指定文件中的段落,并將結(jié)果打印輸出到標(biāo)準(zhǔn)輸出。

下面是從文件domain-list.txt提取的內(nèi)容:

1.tecmint.com 2.news.tecmint.com 3.linuxsay.com 4.windowsmint.com

要將上述內(nèi)容重新格式化成標(biāo)準(zhǔn)列表,運(yùn)行下面這個(gè)命令,-w參數(shù)選項(xiàng)符用來(lái)定義最大行寬:

  1. tecmint@TecMint ~ $ cat domain-list.txt  
  2. 1.tecmint.com 2.news.tecmint.com 3.linuxsay.com 4.windowsmint.com 
  3. tecmint@TecMint ~ $ fmt -w 1 domain-list.txt 
  4. 1.tecmint.com  
  5. 2.news.tecmint.com  
  6. 3.linuxsay.com  
  7. 4.windowsmint.com  

9.pr命令

pr命令可轉(zhuǎn)換文本文件或標(biāo)準(zhǔn)輸入,以便打印輸出。比如在Debian系統(tǒng)上,你可以列出所有已安裝的程序包,如下所示:

  1. $ dpkg -l 

想組織整理分成頁(yè)和列的列表、準(zhǔn)備打印輸出,運(yùn)行下面這個(gè)命令。

  1. tecmint@TecMint ~ $ dpkg -l | pr --columns 3 -l 20   
  2. 2017-01-06 13:19                                
  3.           Page 1 
  4. Desired=Unknown/Install  ii  adduser            ii  apg 
  5. | Status=Not/Inst/Conf-   ii  adwaita-icon-theme    ii  app-install-data 
  6. |/ Err?=(none)/Reinst-r    ii  adwaita-icon-theme-  ii  apparmor 
  7. ||/ Name              ii  alsa-base         ii  apt 
  8. +++-=============== ii  alsa-utils              ii  apt-clone 
  9. ii  accountsservice       ii  anacron               ii  apt-transport-https 
  10. ii  acl               ii  apache2               ii  apt-utils 
  11. ii  acpi-support        ii  apache2-bin         ii  apt-xapian-index 
  12. ii  acpid                 ii  apache2-data          ii  aptdaemon 
  13. ii  add-apt-key       ii  apache2-utils         ii  aptdaemon-data 
  14. 2017-01-06 13:19                                   
  15.            Page 2 
  16. ii  aptitude              ii  avahi-daemon      ii  bind9-host 
  17. ii  aptitude-common   ii  avahi-utils           ii  binfmt-support 
  18. ii  apturl                ii  aview             ii  binutils 
  19. ii  apturl-common         ii  banshee               ii  bison 
  20. ii  archdetect-deb       ii  baobab             ii  blt 
  21. ii  aspell                ii  base-files            ii  blueberry 
  22. ii  aspell-en             ii  base-passwd           ii  bluetooth 
  23. ii  at-spi2-core          ii  bash              ii  bluez 
  24. ii  attr                  ii  bash-completion       ii  bluez-cups 
  25. ii  avahi-autoipd         ii  bc                    ii  bluez-obexd 
  26. .....  

這里使用的標(biāo)志如下:

--column定義輸出中創(chuàng)建的列數(shù)。

-l 指定頁(yè)長(zhǎng)(默認(rèn)頁(yè)長(zhǎng)是66行)。

10.tr命令

這個(gè)工具可轉(zhuǎn)換或刪除來(lái)自標(biāo)準(zhǔn)輸入的字符,并將結(jié)果寫入到標(biāo)準(zhǔn)輸出。

使用tr的語(yǔ)法如下:

  1. $ tr options set1 set2 

不妨看一看下面的例子,在第一個(gè)命令中,set1([:upper:])表示輸入字符的大小寫(全是大寫)。

然后,set2([:lower:])表示隨后得到的字符會(huì)是小寫。第二個(gè)例子中一樣,換碼順序\n意味著打印輸出到新行上:

  1. tecmint@TecMint ~ $ echo "WWW.TECMINT.COM" | tr [:upper:] [:lower:] 
  2. www.tecmint.com 
  3. tecmint@TecMint ~ $ echo "news.tecmint.com" | tr [:lower:] [:upper:] 
  4. NEWS.TECMINT.COM  

11.more命令

more命令是一個(gè)實(shí)用的文件閱讀過(guò)濾器,基本上是用于查看證書(shū)而創(chuàng)建的。它顯示了頁(yè)面格式的文件內(nèi)容,用戶可以按回車鍵來(lái)查看更多信息。

你可以用它查看更廣龐大的文件,就像這樣:

  1. tecmint@TecMint ~ $ dmesg | more 
  2. [    0.000000] Initializing cgroup subsys cpuset 
  3. [    0.000000] Initializing cgroup subsys cpu 
  4. [    0.000000] Initializing cgroup subsys cpuacct 
  5. [    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 
  6. 4.4.6) 
  7. [    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 
  8. [    0.000000] KERNEL supported cpus: 
  9. [    0.000000]   Intel GenuineIntel 
  10. [    0.000000]   AMD AuthenticAMD 
  11. [    0.000000]   Centaur CentaurHauls 
  12. [    0.000000] x86/fpu: xstate_offset[2]:  576, xstate_sizes[2]:  256 
  13. [    0.000000] x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers' 
  14. [    0.000000] x86/fpu: Supporting XSAVE feature 0x02: 'SSE registers' 
  15. [    0.000000] x86/fpu: Supporting XSAVE feature 0x04: 'AVX registers' 
  16. [    0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format. 
  17. [    0.000000] x86/fpu: Using 'eager' FPU context switches. 
  18. [    0.000000] e820: BIOS-provided physical RAM map: 
  19. [    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d3ff] usable 
  20. [    0.000000] BIOS-e820: [mem 0x000000000009d400-0x000000000009ffff] reserved 
  21. [    0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved 
  22. [    0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000a56affff] usable 
  23. [    0.000000] BIOS-e820: [mem 0x00000000a56b0000-0x00000000a5eaffff] reserved 
  24. [    0.000000] BIOS-e820: [mem 0x00000000a5eb0000-0x00000000aaabefff] usable 
  25. --More-- 

 12.less命令

less的用途與上面的more命令恰好相反,不過(guò)它提供了額外的功能,處理大文件時(shí)要快一點(diǎn)。

可以與more同樣的方式來(lái)使用它:

  1. tecmint@TecMint ~ $ dmesg | less 
  2. [    0.000000] Initializing cgroup subsys cpuset 
  3. [    0.000000] Initializing cgroup subsys cpu 
  4. [    0.000000] Initializing cgroup subsys cpuacct 
  5. [    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 
  6. 4.4.6) 
  7. [    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 
  8. [    0.000000] KERNEL supported cpus: 
  9. [    0.000000]   Intel GenuineIntel 
  10. [    0.000000]   AMD AuthenticAMD 
  11. [    0.000000]   Centaur CentaurHauls 
  12. [    0.000000] x86/fpu: xstate_offset[2]:  576, xstate_sizes[2]:  256 
  13. [    0.000000] x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers' 
  14. [    0.000000] x86/fpu: Supporting XSAVE feature 0x02: 'SSE registers' 
  15. [    0.000000] x86/fpu: Supporting XSAVE feature 0x04: 'AVX registers' 
  16. [    0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format. 
  17. [    0.000000] x86/fpu: Using 'eager' FPU context switches. 
  18. [    0.000000] e820: BIOS-provided physical RAM map: 
  19. [    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d3ff] usable 
  20. [    0.000000] BIOS-e820: [mem 0x000000000009d400-0x000000000009ffff] reserved 
  21. [    0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved 
  22. [    0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000a56affff] usable 
  23. [    0.000000] BIOS-e820: [mem 0x00000000a56b0000-0x00000000a5eaffff] reserved 
  24. [    0.000000] BIOS-e820: [mem 0x00000000a5eb0000-0x00000000aaabefff] usable 

 不妨參閱《為何“less”比“more”更快速?》(http://www.tecmint.com/linux-more-command-and-less-command-examples/)一文,即可了解在Linux下如何實(shí)現(xiàn)高效的文件導(dǎo)航。

要是還有哪些在Linux下可充當(dāng)文本過(guò)濾器的實(shí)用命令行工具是本文沒(méi)有提及的,歡迎留言補(bǔ)充。

原文標(biāo)題:12 Useful Commands For Filtering Text for Effective File Operations in Linux,作者:Aaron Kili

【51CTO譯稿,合作站點(diǎn)轉(zhuǎn)載請(qǐng)注明原文譯者和出處為51CTO.com】

責(zé)任編輯:龐桂玉 來(lái)源: 51CTO.com
相關(guān)推薦

2017-02-27 14:50:36

Linux命令數(shù)據(jù)

2023-11-06 18:02:28

Linux實(shí)用命令

2020-09-28 15:14:31

Linux常用命令實(shí)用命令

2024-05-28 08:00:00

Python操作系統(tǒng)命令

2015-10-29 13:10:08

passwd命令Linux

2015-07-27 09:22:53

Unix文件系統(tǒng)命令

2024-11-19 18:32:12

Python路徑操作

2020-10-29 18:42:26

Linux命令操作系統(tǒng)

2015-09-23 09:22:01

系統(tǒng)硬件命令

2015-10-22 17:20:46

命令工具Linux

2009-12-17 10:07:40

linuxpv管道查看器

2014-08-07 10:15:27

linux

2016-12-07 18:22:23

shelllinuxgrep

2024-06-24 13:35:48

2020-04-25 19:00:15

Linux終端命令

2017-12-27 10:20:01

Linux ls命令實(shí)用范例

2017-12-27 09:40:32

Linuxfind命令

2010-02-24 14:50:33

Ubuntu vim

2018-12-13 15:00:51

zypper命令Linux

2023-10-30 10:47:45

Linux內(nèi)核模塊
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)