0805文本處理工具

文本處理工具

查看文本文件

    文件內容:less和cat
            文件截?。篽ead和tail
            按列抽?。篶ut
            按關鍵字抽?。篻rep

   

文件查看命令: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命令復制在其它文件中進行的改變(要謹慎使用?。?br />        適用-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.www58058.com/30049

(0)
我的滑板鞋我的滑板鞋
上一篇 2016-08-07
下一篇 2016-08-07

相關推薦

  • 馬哥linux第一周學習筆記

    計算機組成 Linux基本命令

    Linux干貨 2017-12-17
  • 第十周練習-腳本部分

    1、寫一個腳本 (1) 能接受四個參數:start, stop, restart, status start: 輸出“starting 腳本名 finished.” … (2) 其它任意參數,均報錯退出; #!/bin/bash # case $1 in start)     echo&…

    Linux干貨 2016-12-31
  • mount中-o的選項利用

        Mount下—-o 選項的各項用處  mount-o 選項 sync,async 同步模式,異步模式(defaults)         此選項的默認模式為異步模式。在同步模式下,內存的任何修改都會實時的同步到硬盤當中,這種模式的…

    2017-08-19
  • day6總結

    主要內容: useradd與usermod對比 groupadd與groupmod對比 passwd與chage對比 gpasswd與groupmems對比 切換用戶 chown與chgrp用法 一般權限與特殊權限 chmod用法   useradd創建用戶的初始信息存放在/etc/login.defs和/etc//default/useradd文…

    系統運維 2016-08-08
  • puppet學習筆記

      一、Puppet基礎原理: Puppet是一款使用GPLV2X協議授權的開源管理配置工具,用ruby語言開發,既可以通過客戶端—服務器的方式運行,也可以獨立運行。puppet可以為系統管理員提供方便,快捷的系統自動化管理。   二、puppet工作流程 1. 客戶端 puppet-client 向 puppet-maste…

    Linux干貨 2015-11-04
  • 馬哥教育N22期第八周作業

    1、請描述網橋、集線器、二層交換機、三層交換機、路由器的功能、使用場景與區別。 設備 功能 使用場景 區別 網橋 根據MAC地址來轉發幀,本地通信只限本網段內, 相當于中繼器,線路比較長保證信號不變形的傳輸 工作在物理層,相當于一個物理接口 集線器 接收到的數據包進行廣播轉發,所有端口都能收到此數據,通過MAC確認是不是發給自己的包進行回應 廣播形式轉發數據…

    Linux干貨 2016-11-14
欧美性久久久久