文本文件编辑命令

文本文件编辑命令

  • 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
    
    
原文地址:https://www.cnblogs.com/zhufanyu/p/14227523.html