zoukankan      html  css  js  c++  java
  • 文本查看及处理工具简单命令

    一、文件查看命令:

    cat,tac,rev

    cat [OPTION]... [FILE]...(查看文件内容)

    -E:显示行结束符$
    -n:对显示出的每一行进行编号
    -A:显示所有控制符
    -b:非空行编号
    -s:压缩连续的空行成一行
    

    示例一:

    [root@centos6data]#cat -n f1
         1	1
         2	2
         3	3
         4	14
         5	
         6	adw
         7	we
         8	ads
    

    示例二:

    [root@centos6data]#cat -A f1
    1$
    2$
    3$
    14$
    $
    adw$
    we$
    ads$
    

    示例三:

    [root@centos6data]#cat <f1 > f2  将f1的内容新建f2并存入内容
    [root@centos6data]#cat f2
    1
    2
    3
    14
    
    adw
    we
    ads
    

    tac  纵向将反向显示文本内容:

    [root@centos6data]#seq 1 10 >f1
    [root@centos6data]#tac f1
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    

    rev 横向将文本反向显示:

    [root@centos6data]#cat /etc/fstab >f1
    [root@centos6data]#rev f1
    
    #
    batsf/cte/ #
    9102 61:60:51 22 guA uhT no adnocana yb detaerC #
    #
    'ksid/ved/' rednu deniatniam era ,ecnerefer yb ,smetsyselif elbisseccA #
    ofni erom rof )8(diklb ro/dna )8(tnuom ,)8(sfdnif ,)5(batsf segap nam eeS #
    #
    1 1        stluafed    4txe                       / a625053c83bb-2fdb-f174-f26c-07ba9a62=DIUU
    2 1        stluafed    4txe                   toob/ 404f39fbf14b-cc08-97e4-083d-3edd6f1e=DIUU
    2 1        stluafed    4txe                   atad/ ac9d2493fc8c-5cb9-1a64-432b-9df075f7=DIUU
    0 0        stluafed    paws                    paws cafaae2f597c-f3ca-5984-7881-c9d6f5eb=DIUU
    0 0        stluafed   sfpmt                mhs/ved/                   sfpmt
    0 0  026=edom,5=dig  stpved                stp/ved/                  stpved
    0 0        stluafed   sfsys                    sys/                   sfsys
    0 0        stluafed    corp                   corp/                    corp
    

    二、分页查看文件内容

    more: 分页查看文件
    more [OPTIONS...] FILE...
    -d: 显示翻页及退出提示

    [root@centos6data]#cat /etc/passwd | more
    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
    uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
    rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
    rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin
    avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
    vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
    abrt:x:173:173::/etc/abrt:/sbin/nologin
    rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
    ntp:x:38:38::/etc/ntp:/sbin/nologin
    saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
    --More--

    less:一页一页地查看文件或STDIN输出查看时有用的命令包括:
    /文本 搜索 文本
    n/N 跳到下一个 或 上一个匹配
    less 命令是man命令使用的分页器

    [root@centos6data]#ls /etc/ | less
    abrt
    acpi
    adjtime
    akonadi
    aliases
    aliases.db
    alsa
    alternatives
    anacrontab
    anthy-conf
    asound.conf
    at.deny
    audisp
    audit
    autofs.conf
    autofs_ldap_auth.conf
    auto.master
    auto.misc
    auto.net
    auto.smb
    avahi
    bash_completion.d
    bashrc
    blkid
    bluetooth
    bonobo-activation
    cas.conf
    

    三、显示文本前或后行内

    head [OPTION]... [FILE]...(取文件的前几行)
    -c #: 指定获取前#字节
    -n #: 指定获取前#行
    -#: 指定行数

    示例:取当前文本的前两行内容

    [root@centos6data]#df | head -n2
    Filesystem     1K-blocks    Used Available Use% Mounted on
    /dev/sda2      100660656 8786328  86754328  10% 

    tail [OPTION]... [FILE]...(取文件的后几行)

    -c #: 指定获取后#字节
    -n #: 指定获取后#行
    -#:同上
    -f: 跟踪显示文件fd新追加的内容,常用日志监控
    相当于 --follow=descriptor
    -F: 跟踪文件名,相当于--follow=name --retry
    
    tailf 类似tail –f,当文件不增长时并不访问文件

    示例:查看日志文件的后5行

    [root@centos6data]#tail /var/log/messages  -n 5
    Nov  8 14:21:26 centos6 pulseaudio[2688]: alsa-sink.c: We were woken up with POLLOUT set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail.
    Nov  8 14:21:26 centos6 rtkit-daemon[2690]: Sucessfully made thread 2695 of process 2688 (/usr/bin/pulseaudio) owned by '42' RT at priority 5.
    Nov  8 14:21:27 centos6 pulseaudio[2688]: alsa-source.c: ALSA woke us up to read new data from the device, but there was actually nothing to read!
    Nov  8 14:21:27 centos6 pulseaudio[2688]: alsa-source.c: Most likely this is a bug in the ALSA driver 'snd_ens1371'. Please report this issue to the ALSA developers.
    Nov  8 14:21:27 centos6 pulseaudio[2688]: alsa-source.c: We were woken up with POLLIN set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail.

    -f和-F之间的区别:

    总结:-f跟踪的是文件描述符,而-F跟踪的是文件名。

    先验证-f,删除了f1就不会在跟踪内容,就算创建了f1,也不会再跟踪。

    [root@ansibledata]#tail -f -n 2 f1  跟踪此时的文件,在此文件中追加内容,就会显示跟踪效果
    5
    6
    a
    
    [root@ansibledata]#echo a >> f1  追加一个文件
    我们将f1删除重新复制一个文件再起名为f1,此时不再跟踪文件信息
    [root@ansibledata]#rm -f f1
    [root@ansibledata]#cat /etc/fstab >  f1
    [root@ansibledata]#tail -f -n 2 f1
    5
    6
    a

    验证-F:当tail 加-F 参数时,跟踪的文件如果被删除就会显示此文件不可访问,如果再创建f1,-F就会继续跟踪,说明-F是跟踪文件:

    [root@ansibledata]#rm -rf f1
    [root@ansibledata]#tail -F -n 2 f1
    UUID=eb4bf5e6-2645-4b1c-bda8-12c5831b81c2 /data                   xfs     defaults        0 0
    UUID=b8c37e0b-3628-40b6-ac44-c36ca09b448f swap                    swap    defaults        0 0
    tail: ‘f1’ has become inaccessible: No such file or directory
    [root@ansibledata]#cp /etc/inittab f1   当我们再创建f1。
    [root@ansibledata]#tail -F -n 2 f1  此时-F会继续跟踪文件
    UUID=eb4bf5e6-2645-4b1c-bda8-12c5831b81c2 /data xfs defaults 0 0
    UUID=b8c37e0b-3628-40b6-ac44-c36ca09b448f swap swap defaults 0 0
    tail: ‘f1’ has become inaccessible: No such file or directory
    tail: ‘f1’ has appeared; following end of new file
    # inittab is no longer used when using systemd.
    #
    # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
    #
    # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
    #
    # systemd uses 'targets' instead of runlevels. By default, there are two main targets:
    #
    # multi-user.target: analogous to runlevel 3
    # graphical.target: analogous to runlevel 5
    #
    # To view current default target, run:
    # systemctl get-default
    #
    # To set a default target, run:
    # systemctl set-default TARGET.target
    

    四、文本截取工具

    cut [OPTION]... [FILE]...(按列抽取文本)

    -d DELIMITER: 指明分隔符,默认tab
    -f FILEDS:
    #: 第#个字段
    #,#[,#]:离散的多个字段,例如1,3,6
    #-#:连续的多个字段, 例如1-6
    混合使用:1-3,7
    -c 按字符切割
    --output-delimiter=STRING指定输出分隔符
    

    示例一:

    取出文本的第一列到第三列:

    [root@ansibledata]#getent passwd | cut -d: -f1-3
    root:x:0
    bin:x:1
    daemon:x:2
    adm:x:3
    lp:x:4
    sync:x:5
    shutdown:x:6
    halt:x:7
    mail:x:8
    operator:x:11
    games:x:12
    ftp:x:14
    

    示例二:

    将df显示的文本按空格压缩,用cut以%作为分隔符,取第5列,将磁盘利用率取出来:

    [root@ansibledata]#df | tr -s " " % |cut -d% -f5
    Use
    5
    0
    0
    2
    0
    1
    17
    1
    

    示例三:

    将网卡的IP地址取出来。

    [root@ansibledata]#ifconfig ens33 | tr -s " " | head -n2 |tail -n1|cut -d" " -f3
    192.168.34.101
    

    paste 合并两个文件同行号的列到一行

    paste [OPTION]... [FILE]...
    -d 分隔符:指定分隔符,默认用TAB
    -s : 所有行合成一行显示
    

    示例一:

    默认将f1和f2合并为两列

    [root@ansibledata]#seq 1 10 > f1
    [root@ansibledata]#echo {a..j} | tr " " "
    " > f2
    [root@ansibledata]#paste f1 f2
    1	a
    2	b
    3	c
    4	d
    5	e
    6	f
    7	g
    8	h
    9	i
    10	j

     示例二:

     -s 用法:横向合并

    [root@ansibledata]#paste -s f1 f2
    1	2	3	4	5	6	7	8	9	10
    1	2	3	4	5	6	7	8	9	10
    

     -d用法:可以以冒号作为分隔符

    [root@ansibledata]#paste -d: f1 f2
    1:a
    2:b
    3:c
    4:d
    5:e
    6:f
    7:g
    8:h
    9:i
    10:j
    

    五、收集文本统计数据wc

    wc:计数单词总数、行总数、字节总数和字符总数
    可以对文件或STDIN中的数据运行行数 字数 字节数
    常用选项

    -l 只计数行数
    -w 只计数单词总数
    -c 只计数字节总数
    -m 只计数字符总数
    -L 显示文件中最长行的长度
    

    示例一:

    统计当前文本的行数

    [root@ansibledata]#cat f2 | wc -l
    12
    [root@ansibledata]#cat /var/log/messages | wc -l
    44786
    

    示例二:

    统计当前文本文件最长行的长度

    [root@ansibledata]#cat /var/log/messages | wc -L
    1420
    

    示例三:

    除了字母其他的字母都转换为回车换行:

    [root@ansibledata]#cat /etc/profile | tr -sc "a-zA-Z" "
    " |wc -l
    253

    六、文本排序sort

    把整理过的文本显示在STDOUT,不改变原始文件
    sort [options] file(s)
    常用选项

    -r 执行反方向(由上至下)整理
    -R 随机排序
    -n 执行按数字大小整理
    -f 选项忽略(fold)字符串中的字符大小写
    -u 选项(独特,unique)删除输出中的重复行
    -t c 选项使用c做为字段界定符
    -k X 选项按照使用c字符分隔的X列来整理能够使用多次
    

    示例:

    对文本以冒号形式对第三列进行逆序排序

    [root@ansibledata]#cat /etc/passwd | sort -t: -k3 -nr
    nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    wang:x:1001:1001::/home/wang:/bin/bash
    liu:x:1000:1000:liu:/home/liu:/bin/bash
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
    colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
    gluster:x:996:993:GlusterFS daemons:/run/gluster:/sbin/nologin
    saslauth:x:995:76:Saslauthd user:/run/saslauthd:/sbin/nologin
    unbound:x:994:989:Unbound DNS resolver:/etc/unbound:/sbin/nologin
    

    示例:

    sort -u 有去重复的字符串功能,不加-u会默认将重复的排序到一起。

    [root@ansibledata]#cat f1
    aa
    bb
    cc
    dd
    [root@ansibledata]#cat f2
    aaa
    bb
    cc
    ddd
    [root@ansibledata]#cat f1 f2 | sort -u
    aa
    aaa
    bb
    cc
    dd
    ddd
    

    示例:

    取出分区利用率的最大值:

    [root@ansibledata]#df | tr -s " " :|cut -d: -f5|sort -nr |head -1
    17%

    七、uniq命令:从输入中删除前后相接的重复的行

    uniq [OPTION]... [FILE]...

    -c: 显示每行重复出现的次数
    -d: 仅显示重复过的行
    -u: 仅显示不曾重复的行

    注:连续且完全相同方为重复,才会进行统计
    常和sort 命令一起配合使用:
    sort userlist.txt | uniq -c

    示例:

    取出日志文件的前十个文本内容:

    [root@ansibledata]#cut -d " " -f1 /var/log/httpd/access_log|sort |uniq -c | sort -nr |head -10
    

    示例:

    取出文件中的交集,也就是两个文件中重复的部分:

    [root@ansibledata]#cat f1 
    aa
    bb
    cc
    ss
    ddd
    [root@ansibledata]#cat f2
    aa
    cc
    sss
    dddd
    [root@ansibledata]#cat f1 f2 |sort | uniq -d
    aa
    cc
    

    示例:

    显示两个文本中不同的内容,也就是都没有的内容:

    [root@ansibledata]#cat f1 f2 | sort |uniq -u
    bb
    ddd
    dddd
    ss
    sss
    

    示例:

    显示文件的并集,就是将两个文件的内容都合在一起:

    [root@ansibledata]#cat f1 f2 | sort -u
    aa
    bb
    cc
    ddd
    dddd
    ss
    sss
    

    八、比较文件diff

    diff:逐行比较两个文件之间的区别

    • diff 命令的输出被保存在一种叫做“补丁”的文件中
    • 使用 -u 选项来输出“统一的(unified)”diff格式文件,最适用于补丁文件

    示例:

    [root@ansibledata]#diff -u f1 f2
    --- f1	2019-11-08 17:05:58.956592311 +0800
    +++ f2	2019-11-08 17:06:09.556591856 +0800
    @@ -1,4 +1,4 @@
     aa
    -bb
    +bbb
     ccc
    -eee
    +ee
    [root@ansibledata]#cat f1
    aa
    bb
    ccc
    eee
    [root@ansibledata]#cat f2
    aa
    bbb
    ccc
    ee

    patch 复制在其它文件中进行的改变(要谨慎使用)
    适用 -b 选项来自动备份改变了的文件

    示例:

    [root@ansibledata]#cat f1
    aa
    bb
    ccc
    eee
    [root@ansibledata]#cat f2
    aa
    bbb
    ccc
    ee
    [root@ansibledata]#diff -u f1 f2 > diff.log 将文件备份
    [root@ansibledata]#rm -f f2  删除f2
    [root@ansibledata]#patch -b f1 diff.log 复制f1
    patching file f1
    [root@ansibledata]#cat f1  此时的f1是之前的f2内容
    aa
    bbb
    ccc
    ee
    [root@ansibledata]#cat f1.orig  f2.orig是之前的f1内容
    aa
    bb
    ccc
    eee
    

      

     

     

     

  • 相关阅读:
    python基础(str,list,tuple)
    MySQL数据类型概念
    Ubuntu安装sublime
    Ubuntu安装pycharm
    ubuntu安装mysql
    微信小程序注册开发流程
    新开篇
    被玩坏了的题——马的遍历
    一道数学恶心题——小凯的疑惑
    搜索基础题:八皇后
  • 原文地址:https://www.cnblogs.com/struggle-1216/p/11731860.html
Copyright © 2011-2022 走看看