centos

sys.3.4 文本查看及处理工具

 

wc , cut , sort , uniq , diff , patch ;

1. wc命令:统计文件的行数、单词数、字节数;
wc – print newline, word, and byte counts for each file

wc [OPTION]… [FILE]…
wc [OPTION]… –files0-from=F

[root@localhost ~]# wc /etc/fstab
11 54 465 /etc/fstab ## 数字从左至右:行数 单词数 字节数
[root@localhost ~]#

OPTIONS:
-l, –lines : 显示文件行数;
-w, –words : 显示文件内容的单词数;
-c, –bytes : 显示文件内容的字节数;

[root@localhost ~]# 
[root@localhost ~]# wc -l /etc/fstab 
11 /etc/fstab
[root@localhost ~]# 
[root@localhost ~]# wc -w /etc/fstab
54 /etc/fstab
[root@localhost ~]# 
[root@localhost ~]# wc -c /etc/fstab
465 /etc/fstab
[root@localhost ~]#

2. cut命令
cut – remove sections from each line of files

cut OPTION… [FILE]…

OPTIONS:
-d, –delimiter=DELIM : 后面接指定的字符,以此为分隔符;默认以空白字符为分隔符;
-f, –fields=LIST : 挑选出指定的字段;
## -fNUM : 指定某个字段;
## -fNUM1-NUM2 : 指定连续的字段;
## -fNUM1,NUM2 : 指定离散的字段;

[root@localhost ~]# 
[root@localhost ~]# head -5 /etc/passwd | cut -d: -f1
root
bin
daemon
adm
lp
[root@localhost ~]# 
[root@localhost ~]# head -5 /etc/passwd | cut -d : -f1
root
bin
daemon
adm
lp
[root@localhost ~]# 
[root@localhost ~]# head -5 /etc/passwd | cut -d : -f1-7
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
[root@localhost ~]# 
[root@localhost ~]# head -5 /etc/passwd | cut -d : -f1,3,4,5,7
root:0:0:root:/bin/bash
bin:1:1:bin:/sbin/nologin
daemon:2:2:daemon:/sbin/nologin
adm:3:4:adm:/sbin/nologin
lp:4:7:lp:/sbin/nologin
[root@localhost ~]# 

[root@localhost ~]# 
[root@localhost ~]# wc  /etc/fstab 
 11  54 465 /etc/fstab
[root@localhost ~]# wc  /etc/fstab | cut -d' ' -f 1

[root@localhost ~]# wc  /etc/fstab | cut -d' ' -f 2
11
[root@localhost ~]# wc  /etc/fstab | cut -d' ' -f 4
54
[root@localhost ~]#

3. sort命令
sort – sort lines of text files

sort [OPTION]… [FILE]…
sort [OPTION]… –files0-from=F

OPTIONS:
-t, –field-separator=SEP : 指定字段的分隔符;
-k, –key=KEYDEF : 用于指定排序比较字段;
-n, –numeric-sort : 按数字大小升序排列;
-r, –reverse : 逆序排列;
-f, –ignore-case : 忽略字母大小写;
-u, –unique : 重复的行只保留一份;

4. uniq命令:报告或移除重复的行;
uniq – report or omit repeated lines

uniq [OPTION]… [INPUT [OUTPUT]]

OPTIONS:
-c, –count : 统计每行重复出现的次数;
-u, –unique : 仅显示没有重复过的行;
-d, –repeated : 仅显示重复过的行;

cut , sort , uniq 综合练习:
理解:~]# cut   -d:   -f7    /etc/passwd | sort | uniq -c | sort    -n    -r

[root@localhost ~]# cut -d: -f7 /etc/passwd  ## 以冒号为分隔符,截取第7字段;默认以源文件的行顺序显示;
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/sync
/sbin/shutdown
/sbin/halt
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/bash
/bin/bash
/sbin/nologin
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/sbin/nologin
/bin/bash
[root@localhost ~]# cut -d: -f7 /etc/passwd | sort  ## sort不带任何选项,默认以字母或数字的升序排列;
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/sync
/sbin/halt
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/shutdown

[root@localhost ~]# cut -d: -f7 /etc/passwd | sort | uniq           ## uniq 不带任何选项,默认不显示重复的行,即重复的行只显示一行;
/bin/bash
/bin/sync
/sbin/halt
/sbin/nologin
/sbin/shutdown
[root@localhost ~]# cut -d: -f7 /etc/passwd | sort | uniq -c         ##  对重复行进行统计,但不排序;
     10 /bin/bash
      1 /bin/sync
      1 /sbin/halt
     18 /sbin/nologin
      1 /sbin/shutdown
[root@localhost ~]# cut -d: -f7 /etc/passwd | sort | uniq -c | sort -n       ##  升序排列
      1 /bin/sync
      1 /sbin/halt
      1 /sbin/shutdown
     10 /bin/bash
     18 /sbin/nologin
[root@localhost ~]# cut -d: -f7 /etc/passwd | sort | uniq -c | sort -n -r     ##  降序排列
     18 /sbin/nologin
     10 /bin/bash
      1 /sbin/shutdown
      1 /sbin/halt
      1 /bin/sync
[root@localhost ~]# cut -d: -f7 /etc/passwd | sort | uniq -d               ##  只显示重复行的一行;
/bin/bash
/sbin/nologin
[root@localhost ~]# cut -d: -f7 /etc/passwd | sort | uniq -u               ##  只显示非重复行;
/bin/sync
/sbin/halt
/sbin/shutdown
[root@localhost ~]#

5. diff 命令:比较2个文件的不同之处;
diff – compare files line by line

diff   [OPTION] …   FILES

diff    /PATH/TO/OLDFILE    /PATH/TO/NEWFILE   >  /PATH/TO/PATCH_FILE
diff    -u    /PATH/TO/OLDFILE    /PATH/TO/NEWFILE
-u 选项: 使用unified机制,显示要修改的行的上下文,默认显示前后3行;

6. patch 命令: 打补丁
patch – apply changes to files

打补丁操作:
patch   [OPTIONS]   -i   /PATH/TO/PATCH_FILE    /PATH/TO/OLDFILE
或者
patch    /PATH/TO/OLDFILE    <    /PATH/TO/PATCH_FILE

还原操作:
patch    -R    -i    /PATH/TO/PATCH_FILE     /PATH/TO/OLDFILE

diff 与 patch 组合举例:

[root@localhost tmp]# 
[root@localhost tmp]# ls -l | grep "fstab*"
-rw-r--r--  1 root root 465 Mar 24 01:10 fstab
-rw-r--r--  1 root root 480 Mar 24 01:06 fstab.new
[root@localhost tmp]# 
[root@localhost tmp]# diff fstab fstab.new    ## 预先在文件 fstab.new 中添加了内容;用diff命令进行比较;
5c5
< #
---
> # hello lucifer.
[root@localhost tmp]# 
[root@localhost tmp]# diff fstab fstab.new  > fstab.patch   ## 把比较得出的补丁保存在另外一个文件中;
[root@localhost tmp]# 
[root@localhost tmp]# more fstab.patch 
5c5
< #
---
> # hello lucifer.
[root@localhost tmp]# 
[root@localhost tmp]# patch -i fstab.patch fstab    ##  对源文件 fstab 进行打补丁操作,让源文件内容跟 fstab.new 文件内容一致; 
patching file fstab
[root@localhost tmp]# 
[root@localhost tmp]# diff fstab fstab.new          ##  上面打补丁操作完成后,再次进行比较;文件内容相同,系统不会有任何信息提示;
[root@localhost tmp]# 
[root@localhost tmp]# patch -R -i fstab.patch fstab  ##  用补丁文件还原 源文件 fstab;
patching file fstab
[root@localhost tmp]# 
[root@localhost tmp]# diff fstab fstab.new           ## 源文件 fstab还原后,再次进行比较;
5c5
< #
---
> # hello lucifer.
[root@localhost tmp]# 
[root@localhost tmp]# diff -u fstab fstab.new       ## diff 带选项 -u , 默认把变化内容所在行的前后3行内容全部显示出来,更加明确更改内容所处的位置;
--- fstab	2018-03-24 01:15:33.454443897 +0800
+++ fstab.new	2018-03-24 01:06:44.200402610 +0800
@@ -2,7 +2,7 @@
 #
 # /etc/fstab
 # Created by anaconda on Wed Dec 27 05:42:43 2017
-#
+# hello lucifer.
 # 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
 #
[root@localhost tmp]#

提取 ifconfig ens33 信息显示中的IP地址:
ifconfig    ens33  |  grep    inet  |  grep    -v    inet6  |  awk ‘{print $2}’

[root@localhost tmp]# 
[root@localhost tmp]# ifconfig ens33 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.129  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::7e22:852b:5f82:f5d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ce:f2:dd  txqueuelen 1000  (Ethernet)
        RX packets 414873  bytes 453773719 (432.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 116160  bytes 11453853 (10.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost tmp]# ifconfig ens33 | grep inet|grep -v inet6|awk '{print $2}'
192.168.206.129
[root@localhost tmp]#

 

Leave a Reply

Your email address will not be published. Required fields are marked *