0805文本处理工具

文本处理工具

查看文本文件

    文件内容:less和cat
            文件截取:head和tail
            按列抽取:cut
            按关键字抽取:grep

   

文件查看命令:cat, tac,rev
                cat [OPTION]… [FILE]…
                    -E: 显示行结束符$
                    -n: 对显示出的每一行进行编号
                    -A:显示所有控制符
                    -b:非空行编号
                    -s:压缩连续的空行成一行

[root@localhost ~]# cat issue.out 
I am xiaoshui 


How are you        

haha
how old are you

[root@localhost ~]# cat -E issue.out 
I am xiaoshui $
$
$
How are you        $
$
haha$
how old are you$
$
[root@localhost ~]# cat -A issue.out 
I am xiaoshui $
$
$
How are you^I^I$
$
haha$
how old are you$
$
[root@localhost ~]# cat -b issue.out 
     1    I am xiaoshui 


     2    How are you        

     3    haha
     4    how old are you
[root@localhost ~]# cat -sb issue.out 
     1    I am xiaoshui 

     2    How are you        

     3    haha
     4    how old are you
[root@localhost ~]# tac /etc/passwd //tac将cat查看的结果反过来
mage:x:4343:4346::/home/mage:/bin/bash
wang:x:4342:4345::/home/wang:/bin/bash
ash:x:4341:4343::/home/ash:/bin/bash
nologin:x:4340:4342::/home/nologin:/etc/nologin
basher:x:4339:4341::/home/basher:/bin/bash
testbash:x:4338:4340::/home/testbash:/bin/bash
bash:x:4337:4339::/home/bash:/bin/bash
xiaoxiao:x:4336:4338::/home/xiaoxiao:/bin/bash
xiaoming:x:4335:4337::/home/xiaoming:/bin/bash
xiaosming:x:4334:4336::/home/xiaosming:/bin/bash
jixingshui:x:4333:4335::/home/jixingshui:/bin/bash
tomch:x:4331:4334::/home/tom:/bin/bash

   

head [OPTION]… [FILE]…
            -c #: 指定获取前#字节
            -n #: 指定获取前#行
            -#:指定行数

     

[root@localhost ~]# head /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
[root@localhost ~]# head -c 1 /etc/passwd
r[root@localhost ~]# head -n2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost ~]# head -n2 /et

  tail
            tail [OPTION]… [FILE]…
            -c #: 指定获取后#字节
            -n #: 指定获取后#行
            -#:
            -f: 跟踪显示文件新追加的内容,常用日志监控

[root@localhost ~]# tail /etc/passwd
xiaosming:x:4334:4336::/home/xiaosming:/bin/bash
xiaoming:x:4335:4337::/home/xiaoming:/bin/bash
xiaoxiao:x:4336:4338::/home/xiaoxiao:/bin/bash
bash:x:4337:4339::/home/bash:/bin/bash
testbash:x:4338:4340::/home/testbash:/bin/bash
basher:x:4339:4341::/home/basher:/bin/bash
nologin:x:4340:4342::/home/nologin:/etc/nologin
ash:x:4341:4343::/home/ash:/bin/bash
wang:x:4342:4345::/home/wang:/bin/bash
mage:x:4343:4346::/home/mage:/bin/bash
[root@localhost ~]# tail -n 5 /etc/passwd
basher:x:4339:4341::/home/basher:/bin/bash
nologin:x:4340:4342::/home/nologin:/etc/nologin
ash:x:4341:4343::/home/ash:/bin/bash
wang:x:4342:4345::/home/wang:/bin/bash
mage:x:4343:4346::/home/mage:/bin/bash
[root@localhost ~]#


QQ截图20160806145558.jpg

QQ截图20160806145721.jpg

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

[root@localhost ~]# cut -d: -f1  /etc/passwd  
root
bin
daemon
adm
lp
[root@localhost ~]# cut -d: -f1,3,5  /etc/passwd
root:0:root
bin:1:bin
daemon:2:daemon
adm:3:adm
[root@localhost ~]# cut -d: -f1-5  /etc/passwd
root:x:0:0:root
bin:x:1:1:bin
daemon:x:2:2:daemon
adm:x:3:4:adm
lp:x:4:7:lp
[root@localhost ~]# cut -d: -f1-5  --output-delimiter='=' /etc/passwd
root=x=0=0=root
bin=x=1=1=bin
daemon=x=2=2=daemon
adm=x=3=4=adm

        paste 合并两个文件同行号的列到一行
            paste [OPTION]… [FILE]…
                -d 分隔符:指定分隔符,默认用TAB
                -s : 所有行合成一行显示
                paste f1 f2
                paste -s f1 f2

[root@localhost ~]# cp /etc/issue issue.out 
cp: overwrite ‘issue.out’? y
[root@localhost ~]# paste /etc/issue issue.out 
\S    \S
Kernel \r on an \m    Kernel \r on an \m
    
Mage Education Learning Services    Mage Education Learning Services
http://www.magedu.com    http://www.magedu.com
tty is \l    tty is \l
hostname is \n    hostname is \n
current time is \t    current time is \t
[root@localhost ~]# paste -s /etc/issue 
\S    Kernel \r on an \m        Mage Education Learning Services    http://www.magedu.com    tty is \l    hostname is \n    current time is \t

分析文本文件:

    文本数据统计:wc
    整理文本:sort

    比较文件:diff和patch


    WC: 计数单词总数、行总数、字节总数和字符总数 

        -l来只计数行数
        -w来只计数单词总数
        -c来只计数字节总数
        -m来只计数字符总数

  
[root@localhost ~]# wc issue.out  //不加参数默认依次显示行,单词,字符数
  9  21 123 issue.out
[root@localhost ~]# wc -l issue.out 
9 issue.out
[root@localhost ~]# wc -c issue.out 
123 issue.out
[root@localhost ~]# wc -w issue.out 
21 issue.out
[root@localhost ~]# wc -m issue.out 
123 issue.out

    sort:文本排序

    $sort[options]file(s)

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

[root@localhost ~]# sort -t: -k3 -n /etc/passwd
hu
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
[root@localhost ~]# sort -t: -k3 -n -r /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
mage:x:4343:4346::/home/mage:/bin/bash
wang:x:4342:4345::/home/wang:/bin/bash
ash:x:4341:4343::/home/ash:/bin/bash
nologin:x:4340:4342::/home/nologin:/etc/nologin
basher:x:4339:4341::/home/basher:/bin/bash
testbash:x:4338:4340::/home/testbash:/bin/bash
[root@localhost ~]# sort -u out 
hahahahahah
ixingshui
j
jixingshui
nihaoa
[root@localhost ~]# cat out 
hahahahahah
jixingshui
jixingshui
j
ixingshui
nihaoa
jixingshui

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

        uniq[OPTION]… [FILE]…
        -c: 显示每行重复出现的次数
        -d: 仅显示重复过的行
        -u: 仅显示不曾重复的行
        连续且完全相同方为重复

[root@localhost ~]# cat  out 
xiaoshui
nihaoa
xiaoshui
xiaoshui
xiaoshui
jane
jane
hahah
hahah

[root@localhost ~]# uniq -c out 
      1 xiaoshui
      1 nihaoa
      3 xiaoshui
      2 jane
      2 hahah
      1 
[root@localhost ~]# sort -r out | uniq -c  //常和sort一同使用
      4 xiaoshui
      1 nihaoa
      2 jane
      2 hahah
      1

    diff命令的输出被保存在一种叫做“补丁”的文件中
        使用-u选项来输出“统一的(unified)”diff格式文件,最适用于补丁文件。
    patch命令复制在其它文件中进行的改变(要谨慎使用!)
        适用-b选项来自动备份改变了的文件
        $diff-ufoo.conf-brokenfoo.conf-works>foo.patch
        $patch-bfoo.conf-brokenfoo.patch


Linux上文本处理三剑客

grep:文本过滤(模式:pattern)工具;

sed:stream editor,文本编辑工具;

awk:Linux上的实现gawk,文本报告生成器;


    grep: Global search REgularexpression and Print out the line

        作用:文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查;打印匹配到的行;
        模式:由正则表达式字符及文本字符所编写的过滤条件

        grep [OPTIONS] PATTERN [FILE…]


        grep命令选项

            –color=auto: 对匹配到的文本着色显示;
            -v: 显示不能够被pattern匹配到的行;
            -i: 忽略字符大小写
            -n:显示匹配的行号
            -c: 统计匹配的行数
            -o: 仅显示匹配到的字符串;
            -q: 静默模式,不输出任何信息
            -A #: after, 后#行
            -B #: before, 前#行
            -C #:context, 前后各#行
            -e:实现多个选项间的逻辑or关系
                grep –e ‘cat ’ -e ‘dog’ file
            -w:整行匹配整个单词
            -E:使用ERE

         基本正则表达式元字符:
            字符匹配:
                .:匹配任意的单个字符;
                []:匹配指定范围内的任意单个字符;
                [^]:匹配指定范围外的任意单个字符;
                     [:digit:],[:lower:],[:upper:],[:alpha:],[:alnum:],[:punct:],[:space:]
            匹配次数:用在要指定其出现的次数的字符的后面,用于限制其前面字符出现的次数;默认工作与贪婪模式;
                *:匹配其前面的字符任意次;0,1,多次;

                .*:匹配任意长度的任意字符;
                \?:匹配其前面的字符的0次或1次;即其前面的字符是可有可无的;
                \+:匹配其前面的字符1次或多次;即其前面的字符要出出现至少1次;
                \{m\}:匹配其前面的字符m次;
                \{m,n\}:匹配其前面的字符至少m次,至多n次;
                    \{0,n\}:至多n次;
                    \{m,\}:至少m次;

            位置锚定:
                ^:行首锚定;用于模式的最左侧;
                $: 行尾锚定;用于模式的最右侧;
                ^PATTERN$ 用于PATTERN来匹配整行;
                    ^$:空白行;
                    ^[[:space:]]*$:空行或包含空白字符的行;
                单词:非特殊字符组成的连续字符(字符串)都称为单词;
                \<或\b:词首锚定,用于但此模式的左侧;
                \>或\b:词尾锚定,用于单词模式的右侧;
                \<PATTERN\>:匹配完整单词

            分组及引用
                \(\):将一个或多个字符捆绑在一起,当作一个整体进行处理;
                    \(xy\)*ab
                Note:分组括号中的模式匹配到的内容会被正则表达式引擎自动记录于内部的变量中;这些变量为:
                 \1:模式从左侧其,第一个左括号以及与之匹配的右括号之间的模式所匹配到的字符;
                 \2:模式从左侧其,第二个左括号以及与之匹配的右括号之间的模式所匹配到的字符;
                 \3:…

                (a+(b-c)*d) 括号可嵌套不可交叉

[root@localhost ~]# grep "xiaoshui" out
xiaoshui
xiaoshui
xiaoshui
xiaoshui
[root@localhost ~]# grep -v "xiaoshui" out
nihaoa
jane
jane
hahah
hahah
[root@localhost ~]# cat  out 
xiaoshui
nihaoa
xiaoshui
xiaoshui
xiaoshui
jane
jane
[root@localhost ~]# grep -q "xiaoshui" out
[root@localhost ~]# 
[root@localhost ~]# grep -o "root" /etc/passwd
root
root
root
root
root
root
root
root
[root@localhost ~]# grep  "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
chroot:x:1003:1003::/home/chroot:/bin/bash
rooter:x:3320:4327::/home/rooter:/bin/bash

练习题:

1、找出ifconfig命令结果中本机的所有IPv4地址

QQ截图20160806160030.jpg

2、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多个空白字符结尾的行

QQ截图20160806160234.jpg

egrep:  扩展正则表达式

     字符匹配:
        .:任意单个字符
        []:指定范围内的任意的单个字符
        [^]:指定范围外的任意单个字符

      次数匹配:
        *:任意次,0,1,或多次;
        ?:0次或1次,其前面的字符是可有可无的
        +:其前面的字符至少一次;
        {m}:其前面的额字符m次;
        {m,n}:至少m次,至多n次;
             {0,m}
             {m,}
      位置锚定:
         ^:行首锚定;
         $: 行尾锚定;
         \<,\b:词首锚定;
         \>,\b: 词尾锚定;

      分组及应用:
          ():分组;括号内的模式匹配到的字符会被记录与正则表达式引擎的内部变量中;
          后向引用:\1,\2…

      或:
         a|b:a或者b;
            C|cat: C或者cat
            (c|C)at:cat或Cat

练习题:

1、添加用户bash、testbash、basher以及nologin(其shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行

QQ截图20160806160817.jpg

2、使用egrep取出/etc/rc.d/init.d/functions中其基名

QQ截图20160806161255.jpg

   grep与egrep功能十分强大,需要多思考多练习,慢慢地熟练掌握它,才能在以后的工作中得心应手的使用它。

原创文章,作者:我的滑板鞋,如若转载,请注明出处:http://www.178linux.com/30049

(0)
上一篇 2016-08-07 22:06
下一篇 2016-08-07 22:07

相关推荐