天天看點

Linux 2 unit6 文本處理工具

###unit6###

diff 指令

diff 指令用于比較兩個檔案的内容 , 以了解其差別。它還可用于建立更新檔檔案。更新檔檔案用于在企業環境的多台計算機之間對相似檔案進行更改

patch 指令

patch 采用更新檔檔案 patchfile ( 包含由 diff 生成的差異清單 ) 并将這些差異應用于生成更新檔版的一個或多個原始檔案。通常 , 更新檔版替換原始檔案 , 但當指定 -b 選項時 , 可以制作備份。用 .orig 檔案名字尾重命名原始檔案

grep 指令

grep 将顯示檔案中與模式比對的行。其也可以處理标準輸入将顯示檔案中與模式比對的行。其也可以處理标準輸入

Cut 指令

cut 用于 “ 剪切 ” 檔案中的文本字段或列并将其顯示到标準輸出

sort 指令

sort 用于排序文本資料。該資料可以位于檔案中或其他指令輸出中。 Sort 通常與管道一起使用

uniq 指令

uniq“ 删除 ” 檔案中重複的相鄰行。若要隻列印檔案中出現的唯一行(“ 删除 ” 所有重複行 ), 必須首先對 uniq 的輸入進行排序。由于可以為uniq 指定其決策所基于的字段或列 , 是以這些字段或列是對其輸入進行排序所必須的字段或列。如果未與選項一起使用 , uniq 會使用整個記錄作為決策鍵 , 删除其輸入中的重複行

tr 指令

tr 用于轉字元 : 即 , 如果給定了兩個字元範圍 , 則隻要發現某個字元位于第一個範圍中 , 就會将其轉換為第二個範圍中對等的字元。該指令通常在 shell 腳本中使用 , 以按預期情況轉換資料

sed 指令

sed 指令是流編輯器 , 用于對文本資料流執行編輯。假定要處理一個檔案名 , sed 将對檔案中的所有行執行搜尋和替換 , 以将修改後的資料發送到标準輸出 ; 即 , 其實際上并不修改現有檔案。與 grep 一樣 , sed 通常在管道中使用

##diff指令的使用##

[root@localhost ~]# cd /mnt

[root@localhost mnt]# ls

[root@localhost mnt]# vim file

[root@localhost mnt]# cat file

hello xiao

[root@localhost mnt]# vim file1

[root@localhost mnt]# cat file1

123

[root@localhost mnt]# diff file file1  ##檢視檔案的不同

1a2

> 123

[root@localhost mnt]# diff -c file file1   ##-c顯示上下周圍的行

*** file2017-04-29 21:08:17.822776336 -0400

--- file12017-04-29 21:08:32.733776336 -0400

***************

*** 1 ****

--- 1,2 ----

  hello xiao

+ 123

[root@localhost mnt]# diff -u file file1 ##-u使用統一格式,對生成更新檔檔案非常有效

--- file2017-04-29 21:08:17.822776336 -0400

+++ file12017-04-29 21:08:32.733776336 -0400

@@ -1 +1,2 @@

 hello xiao

[root@localhost mnt]# diff -u file file1 > file.path ##比較兩檔案的不同,輸出更新檔檔案為file.path

file  file1  file.path

[root@localhost mnt]# cat file.path

[root@localhost mnt]# yum install patch -y   ##打更新檔插件

Loaded plugins: langpacks

rhel_dvd                                                 | 4.1 kB     00:00     

Resolving Dependencies

--> Running transaction check

---> Package patch.x86_64 0:2.7.1-8.el7 will be installed

--> Finished Dependency Resolution

Dependencies Resolved

================================================================================

 Package        Arch            Version                 Repository         Size

Installing:

 patch          x86_64          2.7.1-8.el7             rhel_dvd          110 k

Transaction Summary

Install  1 Package

Total download size: 110 k

Installed size: 210 k

Downloading packages:

patch-2.7.1-8.el7.x86_64.rpm                               | 110 kB   00:00     

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

Warning: RPMDB altered outside of yum.

  Installing : patch-2.7.1-8.el7.x86_64                                     1/1

  Verifying  : patch-2.7.1-8.el7.x86_64                                     1/1

Installed:

  patch.x86_64 0:2.7.1-8.el7                                                    

Complete!

[root@localhost mnt]# patch file file.path  ##給file檔案打更新檔

patching file file

[root@localhost mnt]# patch -b file file.path  ##-b備份原檔案

patching file file

file  file1  file.orig  file.path

[root@localhost mnt]# cat file.orig

###grep指令(過濾)###

[root@localhost mnt]# cp /etc/passwd .   ##複制passwd到目前位置

file  file1  file.orig  file.path  passwd

[root@localhost mnt]# vim passwd

[root@localhost mnt]# cat 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

nobody:x:99:99:Nobody:/:/sbin/nologin

test:root:root

test:test:root

root:test:root

root:root:tast

TEST:root:ROOT

[root@localhost mnt]# grep test passwd   ##檢視含有test關鍵字的内容

[root@localhost mnt]# grep test passwd -i   ##模糊查找含有test的檔案(忽略大小寫)

[root@localhost mnt]# grep test passwd -i -v   ##-v反向過濾

[root@localhost mnt]# grep -i -E "test|root" passwd  ##-E過濾多個關鍵字

[root@localhost mnt]# grep -i -E "^test" passwd  ##以test關鍵字開頭的

[root@localhost mnt]# grep -i -E "root$" passwd  ##以root關鍵字結尾的

[root@localhost mnt]# grep "test" passwd | grep -E "^test|test$" -v ##反向查找含有test關鍵字且以其開頭和結尾的

[root@localhost mnt]# echo westos > file ##将westos輸出到file(westos覆寫file内容)

westos

[root@localhost mnt]# grep westos -r /mnt/    ##過濾/mnt下的westos

/mnt/file:westos

[root@localhost mnt]# grep westos -r /mnt/ -n   ##-r對檔案實行遞歸式搜尋,從命名目錄開始

/mnt/file:1:westos

###cut剪切###

passwd

[root@localhost mnt]# cut -d : -f 1 passwd  ##剪切第一列(-d用于指定提取字元段的分隔符,-f指定要從每行中提取的字段)

root

bin

daemon

adm

lp

sync

shutdown

halt

mail

operator

games

ftp

nobody

test

TEST

[root@localhost mnt]# cut -d : -f 1,3 passwd   ##剪切提取第一列和第三列

root:0

bin:1

daemon:2

adm:3

lp:4

sync:5

shutdown:6

halt:7

mail:8

operator:11

games:12

ftp:14

nobody:99

test:root

root:root

root:tast

TEST:ROOT

[root@localhost mnt]# cut -d : -f 1-3 passwd  ##剪切提取一到三列

root:x:0

bin:x:1

daemon:x:2

adm:x:3

lp:x:4

sync:x:5

shutdown:x:6

halt:x:7

mail:x:8

operator:x:11

games:x:12

ftp:x:14

nobody:x:99

[root@localhost mnt]# cut -c 1-3 passwd   ##剪切提取一到三字元(-c指定要從每行中提取的文本列)

roo

dae

lp:

syn

shu

hal

mai

ope

gam

nob

tes

TES

實驗:

[root@localhost mnt]# ifconfig eth0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 172.25.254.113  netmask 255.255.255.0  broadcast 172.25.254.255

        inet6 fe80::5054:ff:fe00:d0a  prefixlen 64  scopeid 0x20<link>

        ether 52:54:00:00:0d:0a  txqueuelen 1000  (Ethernet)

        RX packets 7323  bytes 844132 (824.3 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 2051  bytes 278634 (272.1 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

實驗要求:提取出其中的ip位址172.25.254.113

1)[root@localhost mnt]# ifconfig eth0 | grep inet

[root@localhost mnt]# ifconfig eth0 | grep inet | inet6 -v

bash: inet6: command not found...

[root@localhost mnt]# ifconfig eth0 | grep inet | grep inet6 -v

[root@localhost mnt]# ifconfig eth0 | grep inet | grep inet6 -v | cut -d " " -f 10

172.25.254.113

2)[root@localhost mnt]# ifconfig eth0 | grep inet | grep inet6 -v | awk -F " " '{print $2}'

###sort排序指令與uniq删除指令###

[root@localhost mnt]# sort file   ##直接排序(隻排了第一列數字)

1

11

13

2

22

4

45

5

6

7

76

8

9

[root@localhost mnt]# sort file -n  ##按數值大小排列(-n按數值而非字元排序)

[root@localhost mnt]# sort file -rn  ##-r倒序排列

[root@localhost mnt]# sort file -rnu  ##-u隻排列一次,不重複出現

[root@localhost mnt]# sort file -rn | uniq -c  ##-c每行顯示一次,删除重複行

      2 76

      2 45

      2 22

      2 13

      1 11

      2 9

      2 8

      2 7

      2 6

      2 5

      2 4

      4 2

      2 1

[root@localhost mnt]# sort file -rn | uniq -d  ##-d隻顯示重複行

[root@localhost mnt]# sort file -rn | uniq -u   ##-u僅顯示唯一行

file

[root@localhost mnt]# sort file

1:@:1

1:@:4

14:@:45

23:@:76

2:@:6

3:@:2

3:@:8

4:@:22

4:@:4

5:@:45

6:@:1

6:@:13

6:@:2

6:@:5

65:@:22

6:@:7

6:@:9

7:@:11

7:@:2

7:@:5

7:@:9

9:@:7

9:@:76

9:@:8

99:@:13

[root@localhost mnt]# sort file -n

[root@localhost mnt]# sort -t : -k 3 -n file   ##-t指定分隔符 -k3将第三列按數值排序

[root@localhost mnt]# sort -t : -k 3 -n file | uniq -c  ##每行顯示一次,删除重複行

      1 1:@:1

      1 6:@:1

      1 3:@:2

      1 6:@:2

      2 7:@:2

      1 1:@:4

      1 4:@:4

      1 6:@:5

      1 7:@:5

      2 2:@:6

      1 6:@:7

      1 9:@:7

      1 3:@:8

      1 9:@:8

      1 6:@:9

      1 7:@:9

      1 7:@:11

      1 6:@:13

      1 99:@:13

      1 4:@:22

      1 65:@:22

      1 14:@:45

      1 5:@:45

      1 23:@:76

      1 9:@:76

[root@localhost mnt]# ps aux   ##檢視各程序運作

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root         1  0.0  0.2  49944  3972 ?        Ss   20:53   0:01 /usr/lib/systemd/systemd --switched-r

root         2  0.0  0.0      0     0 ?        S    20:53   0:00 [kthreadd]

root         3  0.0  0.0      0     0 ?        S    20:53   0:00 [ksoftirqd/0]

root         5  0.0  0.0      0     0 ?        S<   20:53   0:00 [kworker/0:0H]

root      3232  0.0  0.0 123356  1324 pts/1    R+   22:15   0:00 ps aux

[root@localhost mnt]# ps ax -o pid --sort -%mem | grep PID -v | head -n 5   ##隻輸出pid排名前五個的程序pid

 1990

  716

 1926

 2047

  602

###sed替換指令###

[root@localhost mnt]# cp /etc/passwd .    ##複制檔案到目前位置

[root@localhost mnt]# sed 's/sbin/westos/g' passwd   ##将sbin用westos替換(一定用s開頭和g結尾)

bin:x:1:1:bin:/bin:/westos/nologin

daemon:x:2:2:daemon:/westos:/westos/nologin

adm:x:3:4:adm:/var/adm:/westos/nologin

lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

sync:x:5:0:sync:/westos:/bin/sync

shutdown:x:6:0:shutdown:/westos:/westos/shutdown

halt:x:7:0:halt:/westos:/westos/halt

mail:x:8:12:mail:/var/spool/mail:/westos/nologin

operator:x:11:0:operator:/root:/westos/nologin

[root@localhost mnt]# sed 's/sbin/westos/g' -i passwd   ##-i用來改變原檔案裡面的參數值

[root@localhost mnt]# sed 's/westos/sbin/g' -i passwd

[root@localhost mnt]# cat passwdroot:x:0:0:root:/root:/bin/bash

[root@localhost mnt]# sed -e 's/sbin/westos/g' -e 's/nologin/linux/g' -i passwd  ##更改兩個字元時記得用-e隔開

bin:x:1:1:bin:/bin:/westos/linux

daemon:x:2:2:daemon:/westos:/westos/linux

adm:x:3:4:adm:/var/adm:/westos/linux

lp:x:4:7:lp:/var/spool/lpd:/westos/linux

mail:x:8:12:mail:/var/spool/mail:/westos/linux

operator:x:11:0:operator:/root:/westos/linux

[root@localhost mnt]# vim rule

[root@localhost mnt]# sed -f rule passwd

operator:x:11:0:operator:/root:/sbin/nologin.

[root@localhost mnt]# cat passwd -b > westos  ##-b顯示行數

[root@localhost mnt]# cat westos

     1root:x:0:0:root:/root:/bin/bash

     2bin:x:1:1:bin:/bin:/westos/linux

     3daemon:x:2:2:daemon:/westos:/westos/linux

     4adm:x:3:4:adm:/var/adm:/westos/linux

     5lp:x:4:7:lp:/var/spool/lpd:/westos/linux

     6sync:x:5:0:sync:/westos:/bin/sync

     7shutdown:x:6:0:shutdown:/westos:/westos/shutdown

     8halt:x:7:0:halt:/westos:/westos/halt

     9mail:x:8:12:mail:/var/spool/mail:/westos/linux

    10operator:x:11:0:operator:/root:/westos/linux

[root@localhost mnt]# sed '3,5s/westos/sbin/g' passwd   ##更改三到五行

daemon:x:2:2:daemon:/sbin:/sbin/linux

adm:x:3:4:adm:/var/adm:/sbin/linux

lp:x:4:7:lp:/var/spool/lpd:/sbin/linux

[root@localhost mnt]# sed 5x westos   ##屏蔽第五行

[root@localhost mnt]# sed 5d westos   ##删除第五行

[root@localhost mnt]# sed 5p westos   ##複制第五行

[root@localhost mnt]# sed -n 5p westos   ##隻顯示第五行

###tr 指令####

tr 用于轉字元 : 即 , 如果給定了兩個字元範圍 , 則隻要發現

某個字元位于第一個範圍中 , 就會将其轉換為第二個範圍中

對等的字元。該指令通常在 shell 腳本中使用 , 以按預期

情況轉換資料

tr 'A-Z' 'a-z' <file

本文轉自 AELY木 51CTO部落格,原文連結:http://blog.51cto.com/12768057/1921089,如需轉載請自行聯系原作者

繼續閱讀