Linux常用文本處理命令

文本查看

cat

cat 是一個文本文件(查看)和(連接)工具,通常與more搭配使用,與more不同的是cat可以合併文件。查看一個文件的內容,用cat比較簡單,就是cat後面直接接文件名。

語法

cat [OPTION]... [FILE]...

選項

  • -A, --show-all 等價於 -vET
  • -b, --number-nonblank 對非空行輸出行編號
  • -e 等價於 -vE
  • -E, --show-ends 在每行結束出顯示$
  • -n, --number 對輸出的所有行編號
  • -s, --squeeze-blank 不輸出多行空行
  • -t 等價於 -vT
  • -T, --show-tabs 將TAB顯示爲 ^I
  • -u (ignored)
  • -v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

例子

  1. 使用cat查看/etc/fstab,並顯示非空行的行號

    [root@localhost ~]# cat -b /etc/fstab   
    
         1  #
         2  # /etc/fstab
         3  # Created by anaconda on Wed Jan 15 12:31:47 2020
         4  #
         5  # Accessible filesystems, by reference, are maintained under '/dev/disk'
         6  # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
         7  #
         8  UUID=edcd7459-580f-4622-b6cc-7e8c0893d59f /                       ext4    defaults        1 1
         9  UUID=113bc62d-f6b2-4671-a2cb-e04c4ed04cf6 /boot                   ext4    defaults        1 2
        10  UUID=69b69599-6670-409d-8761-991d44ec3d40 swap                    swap    defaults        0 0
        11  /dev/cdrom /media/cd    iso9660 auto,ro         0 0
    
  2. 用cat在/tmp目錄創建test文件,以hhh作爲結束符

    [root@localhost tmp]# cat>test<<hhh
    > 123
    > test
    > hh
    > hhh
    [root@localhost tmp]# cat test 
    123
    test
    hh
    
  3. 用cat向test追加內容,以gg作爲結束符

    [root@localhost tmp]# cat>>test<<gg
    > kkkk
    > gg
    [root@localhost tmp]# cat test 
    123
    test
    hh
    kkkk
    
  4. 使用cat連接多個文件

    [root@localhost tmp]# cat>tes1<<jkl
    > 123
    > jkl
    [root@localhost tmp]# cat>test2<<jkl 
    > 456
    > jkl
    [root@localhost tmp]# cat tes1 test2 > test3
    [root@localhost tmp]# cat test3
    123
    456
    

more

more 是我們最常用的工具之一,最常用的就是顯示輸出的內容,然後根據窗口的大小進行分頁顯示,然後還能提示文件的百分比;

語法

more [options] file [...]

選項

  • -number 定義屏幕大小,爲number行;
  • -d 提示Press space to continue, ‘q’ to quit.(按空格鍵繼續,按q鍵退出),當非法的按鍵被按下時,不會響鈴;
  • -l 忽略Ctrl+l (換頁)字符;
  • -f 計算行數時,以實際上的行數,而非自動換行過後的行數(有些單行字數太長的會被擴展爲兩行或兩行以上)
  • -p 通過清除窗口而不是滾屏來對文件進行換頁。和-c參數有點相似;
  • -c 從頂部清屏然後顯示;
  • -s 把連續的多個空行顯示爲一行;
  • -u 把文件內容中的下劃線去掉
  • +/pattern 從pattern 前兩行開始顯示;
  • +number 從第number行開始顯示;

常用動作指令

我們查看一個內容較大的文件時,要用到more的動作指令,比如ctrl+f(或空格鍵) 是向下顯示一屏,ctrl+b是返回上一屏; Enter鍵可以向下滾動顯示n行,默認爲1行;

下面爲幾個常用的動作指令;

  • Enter 向下n行,需要定義,默認爲1行;
  • Ctrl+f 向下滾動一屏;
  • 空格鍵 向下滾動一屏;
  • Ctrl+b 返回上一屏;
  • = 輸出當前行的行號;
  • :f 輸出文件名和當前行的行號;
  • v 調用vi編輯器;
  • ! 命令 調用Shell,並執行命令;
  • q 退出more當我們查看某一文件時,想調用vi來編輯它,不要忘記了v動作指令,這是比較方便的;

例子

使用more顯示/etc/passwd文件,屏幕大小爲1,從第2行開始,顯示提示

[root@localhost tmp]# more -1 +2 -d /etc/passwd  
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
--More--(9%)[Press space to continue, 'q' to quit.]

less

less 工具也是對文件或其它輸出進行分頁顯示的工具,應該說是linux正統查看文件內容的工具,功能極其強大;您是初學者,我建議您用less。由於less的內容太多,我們把最常用的介紹一下;

語法格式

less [options] file
常用選項

  • -c 從頂部(從上到下)刷新屏幕,並顯示文件內容。而不是通過底部滾動完成刷新;
  • -f 強制打開文件,二進制文件顯示時,不提示警告;-i 搜索時忽略大小寫;除非搜索串中包含大寫字母;
  • -I 搜索時忽略大小寫,除非搜索串中包含小寫字母;
  • -m 顯示讀取文件的百分比;
  • -M 顯法讀取文件的百分比、行號及總行數;
  • -N 在每行前輸出行號;
  • -p pattern 搜索pattern;比如在/etc/profile搜索單詞MAIL,就用 less -p MAIL /etc/profile
  • -s 把連續多個空白行作爲一個空白行顯示;
  • -Q 在終端下不響鈴;

常用的動作命令

進入less後,我們得學幾個動作,這樣更方便 我們查閱文件內容;最應該記住的命令就是q,這個能讓less終止查看文件退出;

  • Enter 向下移動一行;
  • y 向上移動一行;
  • 空格鍵 向下滾動一屏;
  • b 向上滾動一屏;
  • d 向下滾動半屏;
  • h less的幫助;
  • u 向上洋動半屏;
  • w 可以指定顯示哪行開始顯示,是從指定數字的下一行顯示;比如指定的是6,那就從第7行顯示;
  • g 跳到第一行;
  • G 跳到最後一行;
  • p n% 跳到n%,比如 10%,也就是說比整個文件內容的10%處開始顯示;
  • /pattern 搜索pattern ,比如 /MAIL表示在文件中搜索MAIL單詞;
  • v 調用vi編輯器;
  • q 退出less
  • !command 調用SHELL,可以運行命令;比如!ls 顯示當前列當前目錄下的所有文件;

就less的動作來說,內容太多了,用的時候查一查man less是最好的。

head

head 將每個文件的前10行打印到標準輸出。如果有多個文件,則在每個文件前面加上給出文件名的頭。如果沒有文件,或者文件爲-,則讀取標準輸入。

語法

head [OPTION]... [FILE]...

選項

  • -c, --bytes=[-]K 打印每個文件的前K個字節;以“-”開頭,打印每個文件的最後K個字節以外的所有字節
  • -n, --lines=[-]K 打印前K行而不是前10行;以“-”打頭,列出除最後K行以外的所有內容
  • -q, --quiet, --silent 從不打印文件名
  • -v, --verbose 總是打印文件名

例子

比如我們顯示/etc/profile的前10行內容,應該是:
[root@localhost ~]# head -n 10 /etc/profile

tail

tail將每個文件的最後10行打印到標準輸出。如果有多個文件,則在每個文件之前加上給出該文件的頭

的名字。如果沒有文件,或者文件爲-,則讀取標準輸入。

語法

tail [OPTION]... [FILE]...

選項

  • -c, --bytes=K 輸出最後K個字節;或者使用-c +K以每個文件的第K位開始輸出字節
  • -f, --follow[={name|descriptor}] 該參數用於監視File文件增長;缺少選項參數意味着“描述符”
  • -F 使用–follow=name,重新打開一個還沒有打開的文件,在N(默認5)次迭代後更改大小,以查看是否已取消鏈接或重命名(這是通常的情況旋轉日誌文件);對於inotify,這個選項很少有用
  • -n, --lines=K 輸出最後的K行,而不是最後的10行;或者使用-n +K從第K行開始輸出
  • –pid=PID 與-f合用,表示在進程ID,PID死掉之後結束
  • -s, --sleep-interval=N 與-f合用,表示在每次反覆的間隔休眠S秒
  • -v, --verbose 總是打印文件名

例子

顯示文件 syslog 的後十行內容並在文件內容增加後,且自動顯示新增的文件內容。

tail -f /var/log/syslog

vi/vim/nano

使用vi/vim/nano這些文本編輯器也能實現文本查看功能

文本搜索

find

在目錄層次結構中搜索文件

語法

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

-H、-L和-P:用於決定find是如何對待字符鏈接文件。默認find採取-P選項,不追蹤字符鏈接文件。

-D debugoptions:這個是find的調試模式,當我們執行find後的命令輸出,與我們所期望的不同時,使用該選項。

-Olevel:啓用查詢優化(query optimization)。

上述三種選項,新手都可以忽略,保持其默認即可。簡化後的結果爲。

find [path...] [expression]

path:表示find查找文件的搜索目錄。find只會在給出的目錄下查找。可以有多個。

expression:表達式

常用expression

  • -name “PATTERN” 根據文件名來查找文件,pattern支持globbing字符。

    [root@localhost ~]# find /etc -name *tab
    /etc/statetab
    /etc/fstab
    /etc/selinux/targeted/active/modules/100/updfstab
    /etc/inittab
    /etc/rwtab
    /etc/crontab
    /etc/anacrontab
    /etc/mtab
    /etc/crypttab
    [root@localhost ~]# 
    
  • -iname “PATTERN”:類似-name,區別在於該選項是忽略字母大小寫。

  • -size SIZE 根據文件的大小查找

    #查找/etc下大於1M的文件
    [root@localhost ~]# find /etc -size +1M
    /etc/brltty/zh-tw.ctb
    /etc/selinux/targeted/active/policy.linked
    /etc/selinux/targeted/active/policy.kern
    /etc/selinux/targeted/contexts/files/file_contexts.bin
    /etc/selinux/targeted/policy/policy.31
    /etc/udev/hwdb.bin
    [root@localhost ~]# 
    
  • -regex PATTERN 正則表達式匹配文件

    [root@localhost ~]# find /etc -regex ".*tab" 
    /etc/statetab
    /etc/fstab
    /etc/selinux/targeted/active/modules/100/updfstab
    /etc/inittab
    /etc/rwtab
    /etc/crontab
    /etc/anacrontab
    /etc/mtab
    /etc/crypttab
    [root@localhost ~]# 
    
  • -print0 文件名輸出到一行中

  • -exec 對找到的文件調用其他命令處理,它的終止是以“;”爲結束標誌的,所以這句命令後面的分號是不可缺少的,考慮到各個系統中分號會有不同的意義,所以前面加反斜槓。

    #對搜索到的文件進行ls -l
    [root@localhost ~]# find /etc -name "*tab" -exec ls -l {} \;
    -rw-r--r--. 1 root root 212 Aug  4  2017 /etc/statetab
    -rw-r--r--. 1 root root 543 Feb 13 16:20 /etc/fstab
    total 20
    -rw-------. 1 root root  2142 Feb  6 17:41 cil
    -rw-------. 1 root root 10360 Feb  6 17:41 hll
    -rw-------. 1 root root     2 Feb  6 17:41 lang_ext
    -rw-r--r--. 1 root root 511 Aug  4  2017 /etc/inittab
    -rw-r--r--. 1 root root 966 Aug  4  2017 /etc/rwtab
    -rw-r--r--. 1 root root 451 Jun 10  2014 /etc/crontab
    -rw-------. 1 root root 541 Aug  3  2017 /etc/anacrontab
    lrwxrwxrwx. 1 root root 17 Jan 15 12:31 /etc/mtab -> /proc/self/mounts
    -rw-------. 1 root root 0 Jan 15 12:31 /etc/crypttab
    
  • -type 根據文件的類型查找

    [root@localhost ~]# find /etc/sysconfig/ -type l     
    /etc/sysconfig/selinux
    /etc/sysconfig/network-scripts/ifdown
    /etc/sysconfig/network-scripts/ifdown-isdn
    /etc/sysconfig/network-scripts/ifup-isdn
    /etc/sysconfig/network-scripts/ifup
    /etc/sysconfig/grub
    
  • -user NAME:根據文件的所有者查找,可以是username,也可以是UID。

    [root@localhost ~]# find /home -user alice
    /home/alice
    /home/alice/.mozilla
    /home/alice/.mozilla/plugins
    /home/alice/.mozilla/extensions
    /home/alice/.bashrc
    /home/alice/.bash_logout
    /home/alice/.bash_profile
    [root@localhost ~]# 
    
  • -group NAME:根據文件的所有組查找,可以是groupname,也可以是GID。

    [root@localhost ~]# find /home -group bobby
    /home/bobby
    /home/bobby/.mozilla
    /home/bobby/.mozilla/plugins
    /home/bobby/.mozilla/extensions
    /home/bobby/.bashrc
    /home/bobby/.bash_logout
    /home/bobby/.bash_profile
    [root@localhost ~]# 
    

locate

locate 讓使用者可以很快速的搜尋檔案系統內是否有指定的檔案。其方法是先建立一個包括系統內所有檔案名稱及路徑的數據庫,之後當尋找時就只需查詢這個數據庫,而不必實際深入檔案系統之中了。在一般的 distribution 之中,數據庫的建立都被放在 crontab 中自動執行。

語法

locate [OPTION]... PATTERN...

常用選項

  • -A, --all 只打印匹配所有模式的條目,而不是隻需要其中一個模式匹配。
  • -b, --basename 只匹配指定模式的基名稱。
  • -c, --count 不要在標準輸出中寫入文件名,只寫入匹配項的數量。
  • -d, --database DBPATH 指定特定數據庫,缺省時使用/var/lib/mlocate/mlocate.db文件,在特定需求下可以通過-d執行搜索對象數據庫。
  • -e, --existing 顯示當前存在的文件或者目錄(在沒有使用updatedb進行信息同步更新時,可能會搜索出已經刪除的文件或者臨時文件)
  • -i, --ignore-case 不區分大小寫
  • -l, --limit, -n LIMIT 僅輸出幾行的意思,例如輸出5行則是-l 5;
  • -S, --statistics 將每個讀數據庫的統計信息寫入標準輸出,而不是搜索文件併成功退出。
  • -q, --quiet 不要寫關於讀取和處理數據庫時遇到的錯誤的消息
  • -r, --regexp REGEXP 根據基本的正則表達式尋找
  • –regex 根據擴展的正則表達式尋找

例子:

  1. 查找匹配net的文件,顯示4行

    [root@localhost ~]# locate -l 4 net
    /boot/grub2/i386-pc/net.mod
    /data/centos7/Packages/dracut-network-033-502.el7.x86_64.rpm
    /data/centos7/Packages/glib-networking-2.50.0-1.el7.x86_64.rpm
    /data/centos7/Packages/libnetfilter_conntrack-1.0.6-1.el7_3.x86_64.rpm
    
  2. 將每個讀數據庫的統計信息寫入標準輸出

    [root@localhost ~]# locate -S
    Database /var/lib/mlocate/mlocate.db:
            10,388 directories
            141,958 files
            6,898,873 bytes in file names
            3,172,981 bytes used to store database
    
  3. 剛創建的文件在更新數據庫之前查找,locate會提示找不到,這時可以使用updatedb更新數據庫

    [root@localhost ~]# touch swm
    [root@localhost ~]# locate swm
    /usr/share/doc/xorg-x11-proto-devel-2018.4/COPYING-windowswmproto
    [root@localhost ~]# updatedb
    [root@localhost ~]# locate swm
    /root/swm
    /usr/share/doc/xorg-x11-proto-devel-2018.4/COPYING-windowswmproto
    
    

文本處理

grep

grep的作用是按行查找字符,輸出包含字符的行。

語法

grep [options] 'PATTERN' FILE

常用選項

  • –color=auto 高亮顯示匹配到內容
  • -E, --extended-regexp 支持擴展正則表達式
  • -P, --perl-regexp 支持PERL正則表達式
  • -f FILE, --file=FILE 指定規則文件,其內容含有一個或多個規則樣式,讓grep查找符合規則條件的文件內容 (-f is specified by POSIX.)。
  • -n, --line-number 顯示行號
  • -i, --ignore-case 忽略大小寫
  • -c, --count #計算符合樣式的列數。
  • -o, --only-matching 只輸出匹配到內容
  • -v, --invert-match 反向匹配,顯示不包含匹配文本的所有行。
  • -A NUM, --after-context=NUM after,匹配到的行的後NUM行
  • -B NUM, --before-context=NUM before,匹配到的行的前NUM行
  • -C NUM, -NUM, --context=NUM both,匹配到的行的前後NUM行

例子

  1. 查找指定進程(第3條結果是grep進程本身,並非真正要找的進程。)

    [root@localhost ~]# ps -ef | grep "sshd"
    root       1075      1  0 10:49 ?        00:00:00 /usr/sbin/sshd -D
    root       1510   1075  0 10:51 ?        00:00:00 sshd: root@pts/0
    root       4885   1522  0 16:46 pts/0    00:00:00 grep --color=auto sshd
    
  2. 從文件中讀取關鍵詞進行搜索 且顯示行號

    [root@localhost tmp]# cat test1
    da.cnblogs.com
    ubuntu
    ubuntu linux
    redhat
    Redhat
    linuxmint
    [root@localhost tmp]# cat test2
    linux
    Redhat
    [root@localhost tmp]# cat test1 | grep -nf test2
    3:ubuntu linux
    5:Redhat
    6:linuxmint
    [root@localhost tmp]# 
    
  3. 從多個文件中查找關鍵詞

    [root@localhost tmp]# grep "linux" test1 test2
    test1:ubuntu linux
    test1:linuxmint
    test2:linux
    [root@localhost tmp]# 
    

cut

從文件的每一行截取一部分內容

語法

cut OPTION... [FILE]...

選項

  • -b, --bytes=LIST 以字節爲單位進行分割。
  • -c, --characters=LIST 以字符爲單位進行分割
  • -d 指定字段的分隔符,默認的字段分隔符爲“TAB”;
  • -f 與-d一起使用,指定顯示哪個區域。
  • -n 與“-b”選項連用,不分割多字節字符;
  • –complement 補足被選擇的字節、字符或字段;
  • –out-delimiter=<字段分隔符> 使用字符串作爲輸出分隔符
  • –help 顯示指令的幫助信息;
  • –version 顯示指令的版本信息。

例子

  1. 使用 : 作爲分隔符對/etc/passwd進行切分,並輸出字段1

    [root@localhost tmp]# cut -d: -f1 /etc/passwd
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    operator
    games
    ftp
    nobody
    systemd-network
    dbus
    polkitd
    postfix
    sshd
    ...
    
  2. 在1的基礎上再輸出字段2,並以%作爲輸出分隔符

    cut -d: -f1,2 --output-delimiter='%' /etc/passwd
    root%x
    bin%x
    daemon%x
    adm%x
    lp%x
    sync%x
    shutdown%x
    halt%x
    mail%x
    operator%x
    games%x
    ftp%x
    nobody%x
    systemd-network%x
    dbus%x
    polkitd%x
    postfix%x
    sshd%x
    

sort

將文本文件內容加以排序,sort可針對文本文件的內容,以行爲單位來排序。

語法

sort [OPTION]... [FILE]...

sort [OPTION]... --files0-from=F

–files0-from=F
從文件F中以null結尾的名稱指定的文件中讀取輸入;如果F是-則從標準輸入中讀取名稱

常用選項

-n, --numeric-sort 以整數類型比較字段

-r, --reverse 倒置排序的順序爲 由大至小(descending),而非默認的由小至大(ascending)

-t, --field-separator=SEP 使用單個字符SEP作爲字段分割字符,取代默認的空白字符。

-k, --key=KEYDEF 通過一個鍵排序;KEYDEF給出位置和類型

-u, --unique 只有唯一的記錄,丟棄所有具有相同鍵值的記錄,只留其中的第一條。

例子

以:爲分隔符,通過第3個字段以整數類型進行排序

[root@localhost tmp]# sort -t: -k3 -n /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
...

uniq

用於報告或忽略文件中的重複行,一般與sort命令結合使用。

語法

uniq [OPTION]... [INPUT [OUTPUT]]

選項

  • -c, --count 在每行前加上表示相應行目出現次數的前綴編號
  • -d, --repeated 只輸出重複的行
  • -D, --all-repeated[=METHOD] 顯示所有重複的行
    METHOD{none(default),prepend,separate}
  • -f, --skip-fields=N 比較時跳過前N 列
  • -i, --ignore-case 在比較的時候不區分大小寫
  • -s, --skip-chars=N 比較時跳過前N 個字符
  • -u, --unique 只顯示唯一的行
  • -z, --zero-terminated 使用’\0’作爲行結束符,而不是新換行
  • -w, --check-chars=N 對每行第N 個字符以後的內容不作對照
  • –help 顯示此幫助信息並退出
  • –version 顯示版本信息並退出

例子

統計/etc/passwd各種shell的數量

[root@localhost tmp]# cut -d: -f7 /etc/passwd | sort | uniq -c  
      6 /bin/bash
      1 /bin/sync
      1 /sbin/halt
     44 /sbin/nologin
      1 /sbin/shutdown

wc

word counting,單詞,字符數,字節數,行數統計

語法

wc [OPTION]... [FILE]...
wc [OPTION]... --files0-from=F

選項

  • -c, --bytes 統計字節數
  • -m, --chars 統計字符數
  • -l, --lines 統計行數
  • –files0-from=F 從文件F中以null結尾的名稱指定的文件中讀取輸入;如果F是-則從標準輸入中讀取名稱
  • -L, --max-line-length 打印最長行的長度
  • -w, --words 打印單詞數,一個單詞被定義爲由空白、跳格或換行字符分隔的字符串
  • –help 顯示此幫助信息並退出
  • –version 顯示版本信息並退出

例子

統計進程數

[root@localhost tmp]# ps aux | wc -l
102

tee

tee命令讀取標準輸入,把這些內容同時輸出到標準輸出和(多個)文件中,tee命令可以重定向標準輸出到多個文件。要注意的是:在使用管道線時,前一個命令的標準錯誤輸出不會被tee讀取。

語法

tee [OPTION]... [FILE]...

選項

  • -a, --append 輸出到標準輸出的同時,追加到文件file中。如果文件不存在,則創建;如果已經存在,就在末尾追加內容,而不是覆蓋。
  • -i, --ignore-interrupts 忽略中斷信號。
  • –help 顯示此幫助信息並退出
  • –version 顯示版本信息並退

例子

tee與重定向的差別

[root@localhost tmp]# seq 5 > 1.txt 
[root@localhost tmp]# cat 1.txt 
1
2
3
4
5
[root@localhost tmp]# cat 1.txt > 2.txt
[root@localhost tmp]# cat 1.txt | tee 3.txt 
1
2
3
4
5
[root@localhost tmp]# cat 2.txt 
1
2
3
4
5
[root@localhost tmp]# cat 3.txt  
1
2
3
4
5

xargs

將上一個命令的輸出每一行作爲參數傳遞給xargs後面的命令,xargs後面的命令默認是echo。

語法

somecommand |xargs -item command

選項

  • -d 更改分隔符,默認情況下,xargs將換行符和空格作爲分隔符,把標準輸入分解成一個個命令行參數。

    [root@localhost tmp]# echo -n "one%two%three" | xargs -d% mkdir
    [root@localhost tmp]# ls
    one  three  two
    [root@localhost tmp]# 
    
  • -p 打印出要執行的命令,詢問用戶是否要執行。

    [root@localhost tmp]# echo -n "one two three" | xargs -p touch
    touch one two three ?...yes
    [root@localhost tmp]# ls
    one  three  two
    [root@localhost tmp]# 
    
  • -t 則是打印出最終要執行的命令,然後直接執行,不需要用戶確認

    [root@localhost tmp]# echo -n "one two three" | xargs -t touch
    touch one two three 
    [root@localhost tmp]# ls
    one  three  two
    [root@localhost tmp]# 
    
  • -0 由於xargs默認將空格作爲分隔符,所以不太適合處理文件名,因爲文件名可能包含空格。

    find命令有一個特別的參數-print0,指定輸出的文件列表以null分隔。然後,xargs命令的-0參數表示用null當作分隔符。

    [root@localhost tmp]# find . -print0  | xargs -0  ls -l
    -rw-r--r--. 1 root root    0 Feb 23 21:15 ./one
    -rw-r--r--. 1 root root    0 Feb 23 21:15 ./three
    -rw-r--r--. 1 root root    0 Feb 23 21:15 ./two
    
    .:
    total 0
    -rw-r--r--. 1 root root 0 Feb 23 21:15 one
    -rw-r--r--. 1 root root 0 Feb 23 21:15 three
    -rw-r--r--. 1 root root 0 Feb 23 21:15 two
    
    ./.font-unix:
    total 0
    
    ./.ICE-unix:
    total 0
    
    ./.Test-unix:
    total 0
    
    ./.X11-unix:
    total 0
    
    ./.XIM-unix:
    total 0
    [root@localhost tmp]# 
    
  • -L 如果標準輸入包含多行,-L參數指定多少行作爲一個命令行參數。

  • -n 指定每次將多少項,作爲命令行參數。

  • -I 如果xargs要將命令行參數傳給多個命令,可以使用-I參數。

    [root@localhost tmp]#  echo -n '/etc/fstab:/etc/inittab'  | xargs -t -d: -n1 -I{}  cp -a {}  /tmp
    cp -a /etc/fstab /tmp 
    cp -a /etc/inittab /tmp 
    [root@localhost tmp]# 
    
  • –max-procs xargs默認只用一個進程執行命令。如果命令要執行多次,必須等上一次執行完,才能執行下一次。–max-procs參數指定同時用多少個進程並行執行命令。

join

合併文件內容

語法

join [OPTION]... FILE1 FILE2

選項

  • -a FILENUM:除了顯示匹配好的行另外將指定序號(1或2)文件裏部匹配的行顯示出來
  • -e EMPTY:將須要顯示可是文件裏不存在的域用此選項指定的字符取代
  • -i :忽略大寫和小寫
  • -j FIELD :等同於 -1 FIELD -2 FIELD,-j指定一個域作爲匹配字段
  • -o FORMAT:以指定格式輸出
  • -t CHAR :以指定字符作爲輸入輸出的分隔符。join 默認以空白字符做分隔符(空格和\t),能夠使用 join -t $’\t’來指定使用tab做分隔符
  • -v FILENUM:與-a相似 但值顯示文件裏沒匹配上的行
  • -1 FIELD:以file1中FIELD字段進行匹配
  • -2 FIELD:以file2中FIELD字段進行匹配
  • –help :打印命令幫助文件

例子

[root@localhost tmp]# cat > test1 <<HH
> aa 12345
> bb 23456
> cc 58795
> dd 12457
> HH
[root@localhost tmp]# cat > test2 <<HH
> aa qwert
> bb uiopy
> cc hutfj
> ee hufvb
> HH
[root@localhost tmp]# join test1 test2
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
[root@localhost tmp]# join -a1 test1 test2
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
dd 12457
[root@localhost tmp]# join -a2 test1 test2  
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
ee hufvb
[root@localhost tmp]# join -o 1.2,2.2 test1 test2   
12345 qwert
23456 uiopy
58795 hutfj
[root@localhost tmp]# 

tr

可以用來刪除一段文本信息中的某些文字,或者將其進行轉換。

語法

tr [OPTION]... SET1 [SET2]

選項

  • -c, -C, --complement \取代所有不屬於第一字符集的字符
  • -d, --delete 刪除所有屬於第一字符集的字符
  • -s, --squeeze-repeats 把連續重複的字符以單獨一個字符表示
  • -t, --truncate-set1 先刪除第一字符集較第二字符集多出的字符

例子

[root@localhost tmp]# echo "abc1235a12bc" | tr 'abc' '<>?'     
<>?1235<12>?
[root@localhost tmp]# echo "abc1235a12bc" | tr -c 'abc' '<>?'  
abc????a??bc?[root@localhost tmp]# echo "abc1235a12bc" | tr -d 'abc'
123512

st tmp]# join test1 test2
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
[root@localhost tmp]# join -a1 test1 test2
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
dd 12457
[root@localhost tmp]# join -a2 test1 test2
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
ee hufvb
[root@localhost tmp]# join -o 1.2,2.2 test1 test2
12345 qwert
23456 uiopy
58795 hutfj
[root@localhost tmp]#




#### tr

可以用來刪除一段文本信息中的某些文字,或者將其進行轉換。

**語法**

`tr [OPTION]... SET1 [SET2]`

**選項**

- -c, -C, --complement	\取代所有不屬於第一字符集的字符
- -d, --delete             刪除所有屬於第一字符集的字符
- -s, --squeeze-repeats   把連續重複的字符以單獨一個字符表示
- -t, --truncate-set1    先刪除第一字符集較第二字符集多出的字符

**例子**

[root@localhost tmp]# echo “abc1235a12bc” | tr ‘abc’ ‘<>?’
<>?1235<12>?
[root@localhost tmp]# echo “abc1235a12bc” | tr -c ‘abc’ ‘<>?’
abc???a??bc?[root@localhost tmp]# echo “abc1235a12bc” | tr -d ‘abc’
123512




文本處理命令還有功能強大的sed和awk,由於功能強大且複雜,過段時間會另開一篇專門介紹
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章