10-Linux常用文本查看及处理工具
10-Linux常用文本查看及处理工具

10-Linux常用文本查看及处理工具

常见处理工具:wccutsortuniqdiffpatch

wc

word count单词统计

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

-c, --bytes 显示字节数
      print the byte counts

-m, --chars
      print the character counts

-l, --lines 显示行数
      print the newline counts

--files0-from=F
      read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input

-L, --max-line-length
      print the length of the longest line

-w, --words 显示单词数
      print the word counts

--help display this help and exit

--version
      output version information and exit

WC [OPTION]...[FILE]..

  • -l:lines
  • -w:words
  • -c:bytes

cut

cut OPTION...[FILE]...
OPTION:

  • -d CHAR:以指定的字符为分隔符:
    -f FIELDS:挑选出的字段;
    • #:指定的单个字段:
    • #-#:连续的多个字段;
    • #,#:离散的多个字段:
[root@VM-4-10-centos ~]# wc lovers.txt
 4 16 82 lovers.txt
# 显示文件行数
[root@VM-4-10-centos ~]# wc -l lovers.txt | cut -d ' ' -f1
4

sort

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

  • -n:基于数值大小而非字符进行排序:

  • -t CHAR:指定分隔符:

  • -k#:用于排序比较的字段;

    # 按第三个数值排序
    [root@VM-4-10-centos ~]# 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
  • -r:逆序排序

  • -f:忽略字符大小写

  • -u:重复的行(连续且相同)只保留一份

    [root@VM-4-10-centos ~]# cut -d: -f7 /etc/passwd | sort -u
    /bin/bash
    /bin/false 
    /bin/sync
    /sbin/halt
    /sbin/nologin
    /sbin/shutdown
    [root@VM-4-10-centos ~]# cut -d: -f7 /etc/passwd | sort -u | wc -l
    6
[root@VM-4-10-centos ~]# cut -d: -f3 /etc/passwd | sort
0
1
1000
...
997
998
999
[root@VM-4-10-centos ~]# cut -d: -f3 /etc/passwd | sort -n
0
1
2
3
4
5
6
...

1006
1007
1008
1009
# 按第三个数值排序
[root@VM-4-10-centos ~]# 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
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
redis:x:995:990:Redis Database Server:/var/lib/redis:/sbin/nologin
syslog:x:996:994::/home/syslog:/bin/false
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
www:x:1000:1000::/home/www:/sbin/nologin
mysql:x:1001:1001::/home/mysql:/sbin/nologin
lighthouse:x:1002:1002::/home/lighthouse:/bin/bash
lemon:x:1003:1003::/home/lemon:/bin/bash
x:x:1004:1004::/home/x:/bin/bash
user3:x:1005:1005::/home/user3:/bin/bash
bash:x:1006:1006::/home/bash:/bin/bash
basher:x:1007:1007::/home/basher:/bin/bash
nologin:x:1008:1008::/home/nologin:/sbin/nologin
testbash:x:1009:1009::/home/testbash:/bin/bash

uniq

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

  • -c:显示每行的重复次数
  • -u:仅显示未曾重复过的行
  • -d:仅显示重复过的行
# 重复行出现的次数
[root@VM-4-10-centos ~]# cut -d: -f7 /etc/passwd | sort | uniq -c
      8 /bin/bash
      1 /bin/false
      1 /bin/sync
      1 /sbin/halt
     25 /sbin/nologin
      1 /sbin/shutdown
# 存在重复行的行(至少两次)
[root@VM-4-10-centos ~]# cut -d: -f7 /etc/passwd | sort | uniq -d
/bin/bash
/sbin/nologin

diff、patch

diff -u: 使用unfled机制,即显示要修改的行的上下文,默认为3行:

patch /PATH/TO/OLDFILE </PATH/TO/PATCH_FILE

[root@VM-4-10-centos ~]# cp /etc/fstab ./fstab
[root@VM-4-10-centos ~]# cp fstab fstab.new
# 修改文件,第一行改成is new
[root@VM-4-10-centos ~]# vim fstab.new
# diff比较
[root@VM-4-10-centos ~]# diff fstab fstab.new
2c2
< #
---
> # is new
# 生成补丁文件
[root@VM-4-10-centos ~]# diff fstab fstab.new  > fstab.patch
# 使用patch打补丁
[root@VM-4-10-centos ~]# patch -i fstab.patch fstab
patching file fstab
# 成了
[root@VM-4-10-centos ~]# cat fstab

# is new
# /etc/fstab
# Created by anaconda on Thu Mar  7 06:38:37 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=4b499d76-769a-40a0-93dc-4a31a59add28 /                       ext4    defaults        1 1
# 逆向补丁——还原
[root@VM-4-10-centos ~]# patch -R -i fstab.patch fstab
patching file fstab
[root@VM-4-10-centos ~]# cat fstab

#
# /etc/fstab
# Created by anaconda on Thu Mar  7 06:38:37 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=4b499d76-769a-40a0-93dc-4a31a59add28 /                       ext4    defaults        1 1

发表回复

您的电子邮箱地址不会被公开。