1. 程式人生 > 實用技巧 >文字檔案編輯命令

文字檔案編輯命令

文字檔案編輯命令

  • cat

    cat命令用於檢視純文字檔案(內容較少的),格式為:cat [選項] [檔案]

    [root@zhufanyu ~]# cat linux.txt 
    Hello world
    
    

    如果需要顯示文字中的行號

    [root@zhufanyu ~]# cat -n linux.txt 
         1	
         2	Hello world
         3	Hello world
         4	Hello world
    
  • more

    more命令用於檢視純文字檔案(內容比較多的), 格式為:more [選項] 檔案

    [root@zhufanyu ~]# more linux.txt 
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    --More--(61%)
    
  • head

    head命令用於檢視純文字文件的前N行, 格式為:head [選項] [檔案]

    [root@zhufanyu ~]# head -n 20 linux.txt 
    
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    
  • tail

    tail 命令用於檢視純文字文件的後N行或持續重新整理內容,格式為:tail [選項] [檔案]

    [root@zhufanyu ~]# tail -f /var/log/messages
    Jan  3 22:50:57 zhufanyu dnf[2524]: CentOS-8 - Extras                                47 kB/s | 1.5 kB     00:00
    Jan  3 22:50:57 zhufanyu dnf[2524]: Extra Packages for Enterprise Linux 8 - x86_64  341 kB/s | 4.7 kB     00:00
    Jan  3 22:50:57 zhufanyu dnf[2524]: Extra Packages for Enterprise Linux 8 - x86_64   37 MB/s | 8.7 MB     00:00
    Jan  3 22:51:00 zhufanyu dnf[2524]: Docker CE Stable - x86_64                       8.5 kB/s | 3.5 kB     00:00
    Jan  3 22:51:00 zhufanyu dnf[2524]: Metadata cache created.
    Jan  3 22:51:00 zhufanyu systemd[1]: Started dnf makecache.
    Jan  3 23:00:03 zhufanyu systemd[1]: Starting system activity accounting tool...
    Jan  3 23:00:03 zhufanyu systemd[1]: Started system activity accounting tool.
    Jan  3 23:10:03 zhufanyu systemd[1]: Starting system activity accounting tool...
    Jan  3 23:10:03 zhufanyu systemd[1]: Started system activity accounting tool.
    
    
  • tr

    tr 命令用於替換文字檔案中的字元, 格式為: tr [原始字元] [目標字元]

    [root@zhufanyu ~]# cat linux.txt | tr H h
    hello world
    hello world
    hello world
    hello world
    hello world
    
    [root@zhufanyu ~]# cat linux.txt | tr [a-z] [A-Z]
    HELLO WORLD
    HELLO WORLD
    HELLO WORLD
    
  • wc

    wc命令用於統計指定文字的行數、字數、位元組數,格式為: wc [引數] 文字

    引數 作用
    -l 只顯示行數
    -w 只顯示單詞數
    -c 只顯示位元組數
    [root@zhufanyu ~]# wc -l /etc/passwd
    31 /etc/passwd
    
    [root@zhufanyu ~]# wc -lwc linux.txt 
     45  88 529 linux.txt
    
  • stat

    stat 命令用於檢視檔案的具體儲存資訊和時間等資訊,格式為: stat 檔名稱

    [root@zhufanyu ~]# stat linux.txt 
      File: linux.txt
      Size: 529       	Blocks: 8          IO Block: 4096   regular file
    Device: fd01h/64769d	Inode: 17475419    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2021-01-03 23:16:55.555646971 +0800
    Modify: 2021-01-03 22:55:54.613694479 +0800
    Change: 2021-01-03 22:55:54.614694513 +0800
     Birth: -
    
    
  • cut

    cut 命令用於按“列”提取文字字元,格式為:cut [引數] 文字

    使用cut命令按列檢視資訊,-f 加引數顯示要檢視的列數, -d 用來設定間隔符號

    [root@zhufanyu ~]# cut -f1 -d: /etc/passwd
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    
    
  • diff

    diff 命令用於比較多個文字檔案的差異,格式為:diff [引數] 檔案

    在使用diff命令時,不僅可以使用--brief 引數來確認兩個檔案是否不同,還可以使用-c引數來詳細比較出多個檔案的差異之處

    [root@zhufanyu ~]# diff --brief linux.txt linux_1.txt 
    Files linux.txt and linux_1.txt differ
    
    

    使用-c 引數的diff命令來描述檔案內容具體的不同:

    [root@zhufanyu ~]# diff -c linux.txt linux_1.txt 
    *** linux.txt	2021-01-03 22:55:54.613694479 +0800
    --- linux_1.txt	2021-01-03 23:34:12.941983133 +0800
    ***************
    *** 11,17 ****
      Hello world
      Hello world
      Hello world
    ! Hello world
      Hello world
      Hello world
      Hello world
    --- 11,17 ----
      Hello world
      Hello world
      Hello world
    ! ABCDEFG
      Hello world
      Hello world
      Hello world