成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Linux上12個高效的文本過濾命令

系統 Linux 系統運維
在這篇文章中,我們將會看一些 Linux 中的過濾器命令行工具。過濾器是一個程序,它從標準輸入讀取數據,在數據上執行操作,然后把結果寫到標準輸出。

在這篇文章中,我們將會看一些 Linux 中的過濾器命令行工具。過濾器是一個程序,它從標準輸入讀取數據,在數據上執行操作,然后把結果寫到標準輸出。

因此,它可以用來以強大的方式處理信息,例如重新結構化輸出以生成有用的報告,修改文件里面的文本,以及其他很多系統管理任務。

下面是 Linux 上的一些有用的文件或者文本過濾器。

[[184244]]

1、 awk 命令

awk 是一個卓越的模式掃描和處理語言,它可被用于在 Linux 下構造有用的過濾器。你可以通過閱讀我們的 awk 系列 1 到 13 部分 來開始使用它。

另外,也可以通過閱讀 awk 的 man 手冊來獲取更多的信息和使用選項。

  1. $ man awk 

2、 sed 命令

sed 是一款過濾和轉換文本的強大的流編輯器。我們已經寫了兩篇關于 sed 的有用的文章,你可以通過這兒來了解:

  • 如何使用 GNU sed 命令在 Linux 下創建、編輯和處理文件
  • 日常 Linux 系統管理員任務使用的 15 個有用的 sed 命令小貼士和技巧

sed 的 man 手冊已經添加控制選項和說明:

  1. $ man sed 

3、 grep、 egrep、 fgrep、 rgrep 命令行

這些過濾器輸出匹配指定模式的行。它們從一個文件或者標準輸入讀取行,并且輸出所有匹配的行,默認輸出到標準輸出。

注意:主程序是 grep,這些變體與使用特定的選項的 grep 相同,如下所示(為了向后兼容性,它們依舊在使用):

 

  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: 

在 Linux 下的 grep、 egrep 和 fgrep 的差異?中,你可以了解更多。

4、 head 命令

head 用于顯示文件前面的部分,默認情況下它輸出前 10 行。你可以使用 -n 標志來指定顯示的行數:

  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 

學習如何 使用帶有 tail 和 cat 命令的 head 命令,以便在 Linux 下更有效的使用。

5、 tail 命令

tail 輸出一個文件的后面的部分(默認 10 行)。使用 -n 選項來指定顯示的行數。

下面的命令將會輸出指定文件的最后 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 有一個特殊的選項 -f ,可以 實時查看一個文件的變化 (尤其是日志文件)。

下面的命令將會使你能夠監控指定文件的變化:

  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 的 man 手冊,獲取使用選項和說明的完整內容:

  1. $ man tail 

6、 sort 命令

sort 用于將文本文件或標準輸入的行進行排序。

下面是一個名為 domain.list 的文件的內容:

  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 

你可以像這樣運行一個簡單的 sort 命令 來排序文件內容:

  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 命令,請參閱以下一些關于 sort 命令的有用的文章。

如何基于修改日期和時間來查找和排序文件

7、 uniq 命令

uniq 命令用于報告或者忽略重復行,它從標準輸入過濾行,并且把結果寫到標準輸出。

在對一個輸入流運行 sort 之后,你可以使用 uniq 刪除重復行,如下例所示。

為了顯示行出現的數目,使用 -c 選項,要在對比時忽略大小寫的差異,使用 -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. tecmint@TecMint ~ $ sort domains.list | uniq -c  
  10. 2 linuxsay.com 
  11. 2 news.tecmint.com 
  12. 2 tecmint.com 
  13. 1 windowsmint.com  

通過閱讀 uniq 的 man 手冊來獲取進一步的使用信息和選項:

  1. $ man uniq 

8、 fmt 命令行

fmt 是一款簡單的優化的文本格式化器,它重新格式化指定文件的段落,并且打印結果到標準輸出。

以下是從文件 domain-list.txt 提取的內容:

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

為了把上面的內容重新格式化成一個標準的清單,運行下面的命令,使用 -w 選項定義最大行寬度:

  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 命令轉換文本文件或者標準輸入之后打印出來。例如在 Debian 系統上,你可以像下面這樣顯示所有的安裝包:

  1. $ dpkg -l 

為了將要打印的列表在頁面和列中組織好,使用以下命令。

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

其中,使用的標志如下:

  • --column 定義在輸出中創建的列數。
  • -l 指定頁面的長度(默認是 66 行)。

10、 tr 命令行

這個命令從標準輸入轉換或者刪除字符,然后輸出結果到標準輸出。

使用 tr 的語法如下:

  1. $ tr options set1 set2 

看一下下面的例子,在第一個命令,set1( [:upper:] ) 代表指定輸入字符的大小寫(都是大寫字符)。 set2([:lower:]) 代表期望結果字符的大小寫。第二個例子意思相似,轉義字符 \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 命令是一個有用的文件過濾器,最初為查看證書而建。它一頁頁顯示文件內容,用戶可以通過按回車來顯示更多的信息。

你可以像這樣使用它來顯示大文件:

  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 命令相反的一個命令,但是它提供了額外的特性,而且對于大文件,它會更快些。

按照 more 命令相同的方式使用它:

  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 

學習為什么 Linux 下進行有效的文件瀏覽, ‘less’ 比 ‘more’ 命令更快。

基本上就這些了,如果你還知道其他本文沒有提供的 Linux 下有用的文本過濾命令行工具,可以在下面的評論部分通知我們。

作者簡介:Aaron Kili 是一名 Linux 和 F.O.S.S 愛好者、一名未來的 Linux 系統管理員、web 開發者,并且目前是一名 TecMint 上的內容創造者,他喜歡計算機相關的工作,并且堅信知識的分享。

責任編輯:未麗燕 來源: Linux.cn
相關推薦

2017-01-13 08:30:02

Linux過濾文本實用命令

2016-12-07 18:22:23

shelllinuxgrep

2020-04-25 19:00:15

Linux終端命令

2018-01-15 13:20:26

Linux命令IP

2015-07-21 16:06:49

Linux進程管理命令

2013-11-13 13:55:16

Linux命令grep

2020-04-14 15:10:38

Linux終端命令

2022-11-06 17:48:39

Linux系統命令

2011-02-16 09:26:49

2019-11-25 15:46:11

LinuxVim命令

2025-01-15 17:00:00

開發Linux命令

2019-06-25 08:42:13

Linux命令指令

2024-06-24 13:35:48

2019-10-15 14:14:26

Linuxshell運維

2014-08-07 10:15:27

linux

2019-12-24 07:55:20

Linuxtop命令

2010-08-25 10:50:14

Linux命令

2022-05-25 16:38:42

sudoLinuxroot 賬戶

2020-07-13 11:20:23

Python魔法命令代碼

2023-12-16 22:21:42

Linux網絡命令
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 在线播放中文字幕 | 成人二区 | 日韩成人在线播放 | 亚洲一区二区三区免费观看 | 毛片一区二区 | 欧美日韩免费一区二区三区 | 国产精品久久免费观看 | 久久福利电影 | 久久久久久免费观看 | 国产精品久久国产精品 | 成人在线精品 | 在线视频 亚洲 | 亚洲视频欧美视频 | 91极品视频 | 国产成人精品a视频一区www | 久久久久国产精品 | 亚洲一区 中文字幕 | 久久美女网 | 精品久久一区二区 | 伊人影院在线观看 | 成人久久久 | 成人国产在线视频 | 日韩网站在线 | 精品久久久久久亚洲精品 | 美女一区二区在线观看 | 羞羞的视频免费观看 | 麻豆久久久久久久 | 国产乱码精品一区二区三区五月婷 | 国产欧美在线 | 欧美一区二区免费视频 | 91se在线| 久久久99国产精品免费 | 亚洲国产精品久久久久久 | 午夜精品一区二区三区免费视频 | 亚洲欧美精品在线 | 亚洲精品美女视频 | 欧美精品一区二区三区在线 | 欧美一区二区三区在线观看视频 | 国产91在线 | 亚洲 | 成人激情免费视频 | 天天干国产|