文本处理工具

2018-06-05  本文已影响0人  毛利卷卷发

文本查看

cat

查看文件的内容,常用选项:

rev

将一行内容倒过来

[root@centos6 ~]# echo "1234567890" |rev
0987654321

more

分页查看文件,不支持man的快捷键

less

一页一页的查看文件,使用的时man的分页器,支持man的快捷键

head

从头部开始查看,默认显示前10行,常用选项:

[root@centos6 ~]# head -n 3 /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
[root@centos6 ~]# head -3 /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

tail

从尾部开始查看,默认显示后10行,常用选项:

tailf

相当于tail -f -10,当文件不增长时不在访问文件,可以减少磁盘的IO

cut

显示文件或stdin数据的指定列,常用选项:

[root@centos6 ~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@centos6 ~]# head -1 /etc/passwd |cut -d: -f1,3-6 --output-delimiter=/
root/0/0/root//root

[root@centos6 ~]# head -1 /etc/passwd |cut -c1-5
root:

paste

合并两个文件同行号的列到一行,常用选项:

[root@centos6 app]# cat a
a
b
c
[root@centos6 app]# cat b
1
2
3
[root@centos6 app]# paste a b
a   1
b   2
c   3
[root@centos6 app]# paste -d : a b
a:1
b:2
c:3
[root@centos6 app]# paste -s a b
a   b   c
1   2   3
[root@centos6 app]# paste -s a 
a   b   c

文本分析

wc

默认显示行数,字数(不包含符号),字节数和文件名,常用选项:

[root@centos6 app]# cat a
nihao
helloword
[root@centos6 app]# cat a|wc
      2       2      16
[root@centos6 app]# cat a
nihao
helloword
[root@centos6 app]# wc a
 2  2 16 a
[root@centos6 app]# wc -L a
9 a
[root@centos6 app]# wc -L /etc/passwd
79 /etc/passwd

sort

把整理过的文本显示在stdout,不改变原始文件,默认排序方式受LC_COLLATE影响,按照UTF-8编码排序,可以使用export LC_COLLATE=C改为按照ASCII编码来排序,常用选项:

[root@centos6 app]# tail /etc/passwd|sort -t: -k3 -n
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

uniq

从输入中删除前后相接的重复的行,一般配合sort先排序一下,常用选项:

diff和patch

  1. diff file.old file.new:比较两个文件不同之处
  2. diff -u file.old dile.new > file.patch:导出补丁文件
  3. patch -b file.old file.patch:将补丁打入旧文件
[root@centos6 app]# cat a.old
nihao
helloword
[root@centos6 app]# cat a.new
nihao
5
2
[root@centos6 app]# diff a.old a.new
2c2,3
< helloword
---
> 5
> 2
[root@centos6 app]# diff -u a.old a.new > file.patch
[root@centos6 app]# ls
a.new  a.old  file.patch
[root@centos6 app]# patch -b a.old file.patch 
patching file a.old
[root@centos6 app]# cat a.old
nihao
5
2

练习

  1. 查出用户UID最大值的用户名、UID及shell类型

    [root@centos6 app]# cat /etc/passwd|sort -t: -k3 -n |tail -1 |cut -d: -f1,3,7
    nfsnobody:65534:/sbin/nologin
    
  2. 查出/tmp的权限,以数字方式显示

    [root@centos6 app]# stat /tmp/
      File: `/tmp/'
      Size: 4096        Blocks: 8          IO Block: 4096   directory
    Device: 802h/2050d  Inode: 262145      Links: 21
    Access: (1777/drwxrwxrwt)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2018-05-25 14:24:58.846543054 +0800
    Modify: 2018-05-25 19:28:20.935536819 +0800
    Change: 2018-05-25 19:28:20.935536819 +0800
    [root@centos6 app]# stat /tmp/|head -4|tail -1|tr "(" /|cut -d/ -f2
    1777
    
  3. 统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

    [root@centos6 app]# cat /var/log/httpd/access_log |cut -d- -f1|sort |uniq -c |sort -t' ' -k1 -nr
      14966 172.18.118.213 
       2246 172.18.118.190 
          8 172.18.118.140 
          2 172.18.118.147
    
上一篇 下一篇

猜你喜欢

热点阅读