linux文本處理命令、三劍客之sed使用詳解


一、文件處理命令

1、sort命令

  • sort 命令對 File 參數指定的文件中的行排序,並將結果寫到標准輸出。如果 File 參數指定多個文件,那么 sort 命令將這些文件連接起來,並當作一個文件進行排序。

sort語法

[root@www ~]# sort [-fbMnrtuk] [file or stdin]
選項與參數:
-f  :忽略大小寫的差異,例如 A 與 a 視為編碼相同;
-b  :忽略最前面的空格符部分;
-M  :以月份的名字來排序,例如 JAN, DEC 等等的排序方法;
-n  :使用『純數字』進行排序(默認是以文字型態來排序的);
-r  :反向排序;
-u  :就是 uniq ,相同的數據中,僅出現一行代表;
-t  :分隔符,默認是用 [tab] 鍵來分隔;
-k  :以那個區間 (field) 來進行排序的意思

案例 1:

對/etc/passwd 的賬號進行排序
[root@www ~]# cat /etc/passwd | sort
adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
  • sort 是默認以第一個數據來排序,而且默認是以字符串形式來排序,所以由字母 a 開始升序排序。

/etc/passwd 內容是以 : 來分隔的,我想以第三欄來排序,該如何
[root@www ~]# cat /etc/passwd | sort -t ':' -k 3
root:x:0:0:root:/root:/bin/bash
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

①默認是以字符串來排序的,如果想要使用數字排序:

cat /etc/passwd | sort -t ':' -k 3n
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh

②默認是升序排序,如果要倒序排序,如下

cat /etc/passwd | sort -t ':' -k 3nr
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
ntp:x:106:113::/home/ntp:/bin/false
messagebus:x:105:109::/var/run/dbus:/bin/false
sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin

③如果要對/etc/passwd,先以第六個域的第2個字符到第4個字符進行正向排序,再基於第一個域進行反向排序。

cat /etc/passwd |  sort -t':' -k 6.2,6.4 -k 1r      
sync:x:4:65534:sync:/bin:/bin/sync
proxy:x:13:13:proxy:/bin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh

④查看/etc/passwd有多少個shell:對/etc/passwd的第七個域進行排序,然后去重:

cat /etc/passwd |  sort -t':' -k 7 -u
root:x:0:0:root:/root:/bin/bash
syslog:x:101:102::/home/syslog:/bin/false
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin

2、uniq命令

  • uniq命令可以去除排序過的文件中的重復行,因此uniq經常和sort合用。也就是說,為了使uniq起作用,所有的重復行必須是相鄰的。

uniq語法

[root@www ~]# uniq [-icu]
選項與參數:
-i   :忽略大小寫字符的不同;
-c  :進行計數
-u  :只顯示唯一的行

testfile的內容如下

cat testfile
hello
world
friend
hello
world
hello

①直接刪除未經排序的文件,將會發現沒有任何行被刪除

#uniq testfile  
hello
world
friend
hello
world
hello

②排序文件,默認是去重

#cat words | sort |uniq
friend
hello
world

③排序之后刪除了重復行,同時在行首位置輸出該行重復的次數

#sort testfile | uniq -c
1 friend
3 hello
2 world

④僅顯示存在重復的行,並在行首顯示該行重復的次數

#sort testfile | uniq -dc
3 hello
2 world

⑤僅顯示不重復的行

sort testfile | uniq -u
friend  

3、cut命令

  • cut命令可以從一個文本文件或者文本流中提取文本列。

cut語法

[root@www ~]# cut -d'分隔字符' -f fields <==用於有特定分隔字符
[root@www ~]# cut -c 字符區間            <==用於排列整齊的信息
選項與參數:
-d  :后面接分隔字符。與 -f 一起使用;
-f  :依據 -d 的分隔字符將一段信息分割成為數段,用 -f 取出第幾段的意思;
-c  :以字符 (characters) 的單位取出固定字符區間;

PATH 變量如下

[root@www ~]# echo $PATH
/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games
# 1 | 2       | 3   | 4       | 5            | 6            | 7

①將 PATH 變量取出,我要找出第五個路徑

#echo $PATH | cut -d ':' -f 5
/usr/local/bin

②將 PATH 變量取出,我要找出第三和第五個路徑。

#echo $PATH | cut -d ':' -f 3,5
/sbin:/usr/local/bin

③將 PATH 變量取出,我要找出第三到最后一個路徑。

echo $PATH | cut -d ':' -f 3-
/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games

image

4、tr命令

  • 什么是tr命令?tr,translate的簡寫

  • 在這里用到的意思是轉化,轉變,轉換

  • 通過使用 tr,您可以非常容易地實現 sed 的許多最基本功能。您可以將 tr 看作為 sed 的(極其)簡化的變體:它可以用一個字符來替換另一個字符,或者可以完全除去一些字符。您也可以用它來除去重復字符。這就是所有 tr 所能夠做的。

rt語法

  tr命令格式:tr [ -d ] [ -c ] [ -s ] [ 字符串1 ] [ 字符串2 ] 文件名。其中字符串1用於查詢,字符串2用於處理各種轉換。
  選項與參數:
 	-c 用字符串1中字符集的補集替換此字符集,要求字符集為ASCII。
	-d 刪除字符串1中所有輸入字符。
	-s 刪除所有重復出現字符序列,只保留第一個;即將重復出現字符串壓縮為一個字		 符串。			

案例:

①刪除文件中的空行

$ echo -e "1\n\n2\n\n\n3" | tr -s '\n' #輸出1\n2\n3

 # 如果參數-s替換為-d,就是刪除所有換行符,輸出結果為123。

②刪除重復字符

$ echo "Hellooo    Javaaa" | tr -s "[ ao]" #輸出Hello Java

#  說明:-s是刪除所有重復出現字符序列,只保留第一個。再用下面一個例子加深一下這句話的理解。

$ echo "Heoolloooo oo Pythonnnnn" | tr -s 'on' #刪除字符on,輸出Heollo o Python

③刪除空格

$ echo "   Hello World  " | tr -d '[ \t]' #刪除空格,包括tab鍵。輸出HelloWorld

#  說明:這里tr命令會刪除包括中間的空格,如果只需要刪除行首或者尾部的空格,可以使用sed命令

④大小寫替換,shell編程中可用於忽略大小寫的字符串判斷場景。

$ echo "Hello World" | tr '[a-z]' '[A-Z]' #小寫轉大寫,輸出HELLO WORLD
$ echo "Hello World" | tr '[A-Z]' '[a-z]' #大寫轉小寫,輸出hello world
$ echo "Hello World" | tr '[A-Za-z]' '[a-zA-Z]' #大小寫互換,輸出hELLO wORLD

⑤刪除數字或字母,在shell編程中可用於判斷輸入是否為純數字或字母。

$ echo "hello 123World456" | tr -d '[0-9]' #刪除數字,輸出hello World
$ echo "hello123World456" | tr -d '[a-zA-Z]' #刪除字母,輸出123456

⑥多行內容合並為一行。

$ echo -e "1\n2\n3\n4" | tr -d '\n' #輸出1234

⑦刪除非數字字符,主要用於了解下-c參數的作用。

$ echo "2018abcdefdf06zzz01" |tr -d -c '[0-9]' #輸出20180601

5、wc命令

  • 統計文件里面有多少單詞,多少行,多少字符

wc語法

[root@www ~]# wc [-lwm]
選項與參數:
-c # : 統計文件的Bytes數
-l # : 僅列出行;
-w # : 僅列出多少字(英文單字);
-m # : 多少字符;

注:在Linux系統中,一段連續的數字或字母組合為一個詞

默認使用wc統計/etc/passwd

#wc /etc/passwd
40   45 1719 /etc/passwd

40是行數,45是單詞數,1719是字節數

wc的命令比較簡單使用,每個參數使用如下:

#wc -l /etc/passwd   #統計行數,在對記錄數時,很常用
23 /etc/passwd       #表示系統有40個賬戶

#wc -w /etc/passwd  #統計單詞出現次數
41 /etc/passwd

#wc -m /etc/passwd  #統計文件的字節數
1051 /etc/passwd

#wc -c /etc/passwd  #統計文件的Bytes數
1051 /etc/passwd

 # -m -c 理解為一樣就好

image

二、linux三劍客之sed

  • sed 是一種新型的,非交互式的編輯器。它能執行與編輯器 vi 和 ex 相同的編輯任務。sed 編輯器沒有提供交互式使用方式,使用者只能在命令行輸入編輯命令、指定文件名,然后在屏幕上查看輸出。 sed 編輯器沒有破壞性,它不會修改文件,除非使用 shell 重定向來保存輸出結果。默認情況下,所有的輸出行都被打印到屏幕上。

sed是linux中,流媒體編輯器。

grep : 過濾文本
sed  :  修改文本
awk  :  處理文本

sed語法格式

sed [參數] '處理規則' [操作對象]

sed的參數

選項 說明
-n 使用安靜模式,在一般情況所有的 STDIN 都會輸出到屏幕上,加入-n 后只打印 被 sed 特殊處理的行
-e 多重編輯,且命令順序會影響結果
-f 指定一個 sed 腳本文件到命令行執行,
-r Sed 使用擴展正則
-i 直接修改文檔讀取的內容,不在屏幕上輸出

sed的編輯模式

命 令 說 明
a\ 在當前行后添加一行或多行
c\ 用新文本修改(替換)當前行中的文本
d 刪除行
i\ 在當前行之前插入文本
h 把模式空間里的內容復制到暫存緩存區
H 把模式空間里的內容追加到暫存緩存區
g 取出暫存緩沖區里的內容,將其復制到模式空間,覆蓋該處原有內容
G 取出暫存緩沖區里的內容,將其復制到模式空間,追加在原有內容后面
l 列出非打印字符
p 打印行
n 讀入下一輸入行,並從下一條命令而不是第一條命令開始處理
q 結束或退出 sed
r 從文件中讀取輸入行
對所選行意外的所有行應用命令
s 用一個字符串替換另一個
i 忽略大小寫(跟 s 模式一起使用時)
& 代表前面匹配到的內容

sed替換標志

g 在行內進行全局替換 (全部執行)
p 打印行
w 將行寫入文件
x 交換暫存緩沖區與模式空間的內容
y 將字符轉換為另一字符(不能對正則表達式使用 y 命令)

打印: p 命令

  • 命令 p 是打印命令,用於顯示模式緩存區的內容。默認情況下, sed 把輸入行打印在屏幕上,選項-n 用於取消默認打印操縱。當選項-n 和命令 p 同時出現時, sed 可打印選定的內容

案例1

[root@Gin scripts]# sed '/north/p' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4     
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13

# 說明:默認情況下, sed 把所有輸入行都打印在標准輸出上。如果在某一行匹配到 north, sed就把該行另外打印一遍。 

案例2

[root@Gin scripts]# sed -n '/north/p' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9

# 說明:默認情況下, sed 打印當前緩存區中的輸入行。命令 p 指示 sed 將再次打印該行。選項-n 取消 sed 取消默認打印操作。選線-n 和命令配合使用,模式緩沖區內的輸入行,只被打印一次。如果不指定-n 選項, sed 就會像上例中那樣,打印出重復的行。如果指定了-n,則sed 只打印包含模式 north 的行。

刪除: d 命令

  • 命令 d 用於刪除輸入行。sed 先將輸入行從文件復制到模式緩存區,然后對該行執行 sed命令,最后將模式緩存區的內容顯示在屏幕上。如果發出的是命令 d,當前模式緩存區的輸入行會被刪除,不被顯示

案例1

[root@Gin scripts]# sed '3d' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4     
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13


# 說明:刪除第 3 行。默認情況下,其余的行都被打印到屏幕上。

案例2

[root@Gin scripts]# sed '3,$d' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23

# 說明:刪除從第三行到最后一行內容,剩余各行被打印。地址范圍是開始第 3 行,結束最后一行。

替換: s 命令

  • 命令 s 是替換命令。替換和取代文件中的文本可以通過 sed 中的 s 來實現, s 后包含在斜杠中的文本是正則表達式,后面跟着的是需要替換的文本。可以通過 g 標志對行進行全局替換

案例1

[root@Gin scripts]# sed 's/west/north/g' ceshi.txt
northnorth      NW      Charles Main    3.0     .98     3       34
northern                WE      Sharon Gray     5.3     .97     5     
southnorth      SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4     
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13

# 說明:s 命令用於替換。命令末端的 g 表示在行內全局替換;也就是說如果每一行里出現多個west,所有的 west 都會被替換為 north。如果沒有 g 命令,則只將每一行的第一 west 替換為 north。

案例2

[root@Gin scripts]# sed -n 's/^west/north/p' ceshi.txt
northern                WE      Sharon Gray     5.3     .97     5      
# 說明:s 命令用於替換。選線-n 與命令行末尾的標志 p 結合,告訴 sed 只打印發生替換的那些行;也就是說,如果只有在行首找到 west 並替換成 north 時才會打印此行。

案例3

[root@Gin scripts]# sed -n 's/Hemenway/Jones/gp' ceshi.txt
southeast       SE      Patricia Jones  4.0     .7      4       17

# 說明:文件中出現的所有的 Hemenway 都被替換為 Jones,只有發生變化的行才會打印出來。選項-n 與命令 p 的組合取消了默認的輸出。標志 g 的含義是表示在行內全局替換。

指定行的范圍:逗號

  • 行的范圍從文件中的一個地址開始,在另一個地址結束。地址范圍可以是行號(例如5,10),正則表達式(例如/Dick/和/Joe/),或者兩者的結合(例如/north/,$)范圍是閉合的——包含開始條件的行,結束條件的行,以及兩者之間的行。如果結束條件無法滿足,就會一直操作到文件結尾。如果結束條件滿足,則繼續查找滿足開始條件的位置,范圍重新開始。

案例1

[root@Gin scripts]# sed -n '/west/,/east/p' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4     

#說明:打印模式 west 和 east 之間所有的行。如果 west 出現在 east 之后的某一行,則打印的范圍從 west 所在行開始,到下一個出現 east 的行或文件的末尾(如果前者未出現)。圖中用箭頭表示出了該范圍。

img

案例2

[root@Gin scripts]# sed -n '5,/northeast/p' ceshi.txt
southeast       SE      Patricia Hemenway       4.0     .7      4     
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13

# 說明:打印從第 5 行開始第一個以 northeast 開頭的行之間的所有行。

多重編輯: e 命令

  • -e 命令是編輯命令,用於 sed 執行多個編輯任務的情況下。在下一行開始編輯前,所有的編輯動作將應用到模式緩存區的行上。

案例1

[root@Gin scripts]# sed -e '1,3d' -e 's/Hemenway/Jones/' ceshi.txt
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Jones  4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13

# 說明:選項-e 用於進行多重編輯。第一重編輯編輯刪除第 1~3 行。第二重編輯將Hemenway 替換為 Jones。因為是逐行進行這兩行編輯(即這兩個命令都在模式空間的當前行上執行),所以編輯命令的順序會影響結果。例如,如果兩條命令都執行的是替換,前一次替換會影響后一次替換。

追加: a 命令

  • a 命令是追加命令,追加將新文本到文件中當前行(即讀入模式的緩存區行)的后面。不管是在命令行中,還是在 sed 腳本中, a 命令總是在反斜杠的后面。

案例1

[root@Gin scripts]# sed '/^north/a Hello world!' ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
Hello world!
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4     
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
Hello world!
north           NO      Margot Weber    4.5     .89     5       9
Hello world!
central         CT      Ann Stephens    5.7     .94     5       13

# 說明:命令 a 用於追加。字符串 Hello, World!被加在以 north 開頭的各行之后。如果要追加的內容超過一行,則除最后一行外,其他各行都必須以反斜杠結尾。

插入: i 命令

  • i 命令是插入命令,類似於 a 命令,但不是在當前行后增加文本,而是在當前行前面插入新的文本,即剛讀入緩存區模式的行。

案例1

[root@Gin scripts]# sed '/eastern/i Hello,world!\
> -----------------------' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4     
Hello,world!
-----------------------
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13

# 說明:命令 i 是插入命令。如果在某一行匹配到模式 eastern,i 命令就在該行的上方插入命令中插入反斜杠后面后的文本。除了最后一行,

修改: c 命令

  • c 命令是修改命令。 sed 使用該命令將已有的文本修改成新的文本。舊文本被覆蓋。

案例1

[root@Gin scripts]# sed '/eastern/c Hello,world! \
> ------------------' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4     
Hello,world!
------------------
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13

# 說明:c 命令是修改命令。該命令將完整地修改在模式緩沖區行的當前行。如果模式 eastern被匹配, c 命令將其后的文本替換包含 eastern 的行。

image

獲取下一行: n 命令

  • n 命令表示下一條命令。 sed 使用該命令獲取輸入文件的下一行,並將其讀入到模式緩沖區中,任何 sed 命令都將應用到匹配行,緊接着的下一行上。

案例1

[root@Gin scripts]# sed '/eastern/{n;s/AM/Archie/;}' ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      Archie Main Jr. 5.1     .94     3       13   ## 此行就是被替換的行   
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13

 # 說明:如果在某一行匹配到模式 eastern, n 命令就指示 sed 用下一個輸入行(即包含 AM MainJr 的那行)替換模式空間中的當前行,並用 Archie 替換 AM,然后打印該行,再繼續往下處理

轉換: y,命令

  • y 命令表示轉換。該命令與 tr 命令相似,字符按照一對一的方式從左到右進行轉換。例如 y/abc/ABC/,會把小寫字母轉換成大寫字母, a-->A,b-->B,c-->C。

案例1

[root@Gin scripts]# sed '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' ceshi.txt
NORTHWEST       NW      CHARLES MAIN    3.0     .98     3       34
WESTERN         WE      SHARON GRAY     5.3     .97     5       23
SOUTHWEST       SW      LEWIS DALSASS   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13

# 說明:y 命令把 1~3 行中所有的小寫命令字母都轉換成了大寫。正則表達式元字符對 y 命令不起作用。與替分隔符一樣,斜杠可以被替換成其他字符。

退出: q 命令

  • 命令表示退出命令。該命令將導致 sed 程序退出,且不再進行其他的處理。

案例1

[root@Gin scripts]# sed '5q' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17

# 說明:打印完第 5 行之后, q 讓 sed 程序退出。

案例2

[root@Gin scripts]# sed '/Lewis/{ s/Lewis/Joseph/;q; }' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Joseph Dalsass  2.7     .8      2       18

# 說明:在某行匹配到模式 Lewis 時, s 表示先用 Joseph 替換 Lewis,然后 q 命令讓 sed 退出。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM