Linux命令行與Shell腳本編程
第十八章 文本處理與編輯器基礎
文本處理與編輯器基礎
- 文本處理
- 學習sed編輯器
- sed編輯器基礎命令
- gawk編輯器入門
- sed編輯器基礎
shell腳本可以將文本文件中各種數(shù)據的日常處理任務自動化Linux中的sed和gawk兩款工具能夠極大地簡化數(shù)據處理任務。
8.1.文本處理
想要即時處理文本文件中的文本,有一個可以自動格式化、插入、修改或刪除文本元素的簡單的命令行編輯器。
有兩款常見工具sed和gawk。
8.1.1.sed編輯器
sed編輯器(stream editor)流編輯器.
在交互式文本編輯器(比如Vim)中,可以用鍵盤命令交互式地插入、刪除或替換文本數(shù)據。
流編輯器則是根據事先設計好的一組規(guī)則編輯數(shù)據流。
sed編輯器根據命令來處理數(shù)據流中的數(shù)據,命令可以從命令行中輸入,可以保存在命令文本文件中.
sed編輯器可以執(zhí)行下列操作:
- 從輸入中讀取一行數(shù)據。
- 根據所提供的編輯器命令匹配數(shù)據。
- 按照命令修改數(shù)據流中的數(shù)據。
- 將新的數(shù)據輸出到STDOUT。
在流編輯器匹配并對一行數(shù)據執(zhí)行所有命令后,會讀取下一行數(shù)據并重復執(zhí)行。在流編輯器處理完數(shù)據流中的所有行后,就結束運行。
sed命令格式:
sed options script file
options參數(shù)允許修改sed命令的行為:
選項 | 描述 |
---|---|
-e commands | 處理輸入時,加入額外的sed命令 |
-f file | 處理輸入時,將file中指定的命令添加到已有命令中 |
-n | 不產生輸出,使用p(print)命令完成輸出 |
script參數(shù)指定了應用于流數(shù)據中的單個命令。
如果需要多個命令,則使用-e選項在命令行中指定,或使用-f選項在單獨的文件中指定。
8.1.1.1.在命令行中定義編輯器命令
默認情況下,sed編輯器會將指定的命令應用于STDIN輸入流中。因此,可以直接將數(shù)據通過管道傳入sed編輯器進行處理.
s 替換命令:替換命令會用斜線間指定的第二個字符串替換第一個字符串。
$ echo "This is a test" | sed 's/test/big test/'
This is a big test
sed編輯器的強大之處是可以同時對數(shù)據做出多處修改.
$ cat data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
$ sed 's/dog/cat/' data1.txt
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
sed命令幾乎瞬間就執(zhí)行完畢并返回數(shù)據。在處理每行數(shù)據的同時,結果也隨之顯現(xiàn)。在sed編輯器處理完整個文件之前就能看到結果。
重要的是sed編輯器并不會修改文本文件的數(shù)據。它只是將修改后的數(shù)據發(fā)送到STDOUT !!!
8.1.1.2.在命令行中使用多個編輯器命令
使用-e選項在sed命令行中執(zhí)行多個命令.
$ sed -e 's/brown/red/; s/dog/cat/' data1.txt
The quick red fox jumps over the lazy cat.
The quick red fox jumps over the lazy cat.
The quick red fox jumps over the lazy cat.
The quick red fox jumps over the lazy cat.
命令之間必須以分號 ; 分隔,命令末尾和分號之間不能出現(xiàn)空格。
也可以用bash shell中的次提示符來分隔命令。輸入第一個單引號標示出sed程序腳本的起始.
要在閉合單引號所在行結束命令。bash shell一旦發(fā)現(xiàn)了閉合單引號,就會執(zhí)行命令.
$ sed -e '
> s/brown/green/
> s/fox/toad/
> s/dog/cat/' data1.txt
The quick green toad jumps over the lazy cat.
The quick green toad jumps over the lazy cat.
The quick green toad jumps over the lazy cat.
The quick green toad jumps over the lazy cat.
8.1.1.3.從文件中讀取編輯器命令
將量要執(zhí)行的sed命令放進單獨的文件通常會更方便.
在sed命令中用-f選項來指定文件:
$ cat script1.sed
s/brown/green/
s/fox/toad/
s/dog/cat/
$ sed -f script1.sed data1.txt
The quick green toad jumps over the lazy cat.
The quick green toad jumps over the lazy cat.
The quick green toad jumps over the lazy cat.
The quick green toad jumps over the lazy cat.
不用在每條命令后面加分號。sed編輯器將每一行作為一條單獨的命令。
sed編輯器腳本文件容易與bash shell腳本文件混淆??梢允褂?sed作為sed腳本文件的擴展名。
更多sed命令 sed編輯器基礎命令
8.1.2.gawk編輯器
雖然sed編輯器非常方便,可以即時修改文本文件,但其自身也存在一些局限。
往往還需要一款更高級的文本文件處理工具能夠提供一個更貼近編程的環(huán)境,修改和重新組織文件中的數(shù)據。
gawk是Unix中最初的awk的GNU版本。gawk比sed的流編輯提升了一個“段位”,提供了一種編程語言而不僅僅是編輯器命令。
在gawk編程語言中,可以實現(xiàn):
- 定義變量來保存數(shù)據。
- 使用算術和字符串運算符來處理數(shù)據。
- 使用結構化編程概念(比如if-then語句和循環(huán))為數(shù)據處理添加處理邏輯。
- 提取文件中的數(shù)據將其重新排列組合,最后生成格式化報告。
gawk的報告生成能力多用于從大文本文件中提取數(shù)據并將其格式化成可讀性報告。
最完美的應用案例是格式化日志文件。gawk能夠從日志文件中過濾出所需的數(shù)據,將其格式化,以便讓重要的數(shù)據更易于閱讀。
8.1.2.1.gawk命令格式
gawk options program file
選項 | 描述 |
---|---|
-F fs | 指定行中劃分數(shù)據字段的字段分隔符 |
-f file | 從指定文件中讀物gawk腳本代碼 |
-v var=value | 定義gawk腳本中的變量與默認值 |
-L [keyword] | 指定gawk的兼容模式或警告級別 |
gawk可以編寫腳本來讀取文本行中的數(shù)據,然后對其進行處理并顯示,形成各種輸出報告。
8.1.2.2.從命令行讀取gawk腳本
gawk腳本用一對花括號來定義。必須將腳本命令放到一對單引號與花括號 ‘{}’ 之間。
gawk命令行假定腳本是單個文本字符串,因此必須將腳本放到單引號中。
沒有在命令行中指定文件名時,gawk程序會從STDIN接收數(shù)據。在腳本運行時,會一直等待來自STDIN的文本。
$ gawk '{print "Hello World!"}'
This is a test
Hello World!
hello
Hello World!
Goodbye
Hello World!
This is another test
Hello World!
腳本定義了一個命令:print:將文本打印到STDOUT。
在腳本運行時,會一直等待來自STDIN的文本。如果輸入一行文本并按下Enter鍵,則gawk會對這行文本執(zhí)行一遍腳本。
和sed編輯器一樣,gawk會對數(shù)據流中的每一行文本都執(zhí)行腳本。
bash shell提供了Ctrl+D組合鍵來生成EOF(end-of-file)字符終止gawk程序,表明數(shù)據流已經結束。
使用該組合鍵可以終止gawk程序并返回到命令行界面。
8.1.2.3.使用數(shù)據字段變量
gawk處理文本文件中的數(shù)據時會自動為每一行的各個數(shù)據元素分配一個變量。
在默認情況下,gawk會將下列變量分配給文本行中的數(shù)據字段。
- $0代表整個文本行。
- $n(1~n)代表文本行中的第n個數(shù)據字段。
文本行中的數(shù)據字段通過字段分隔符來劃分。
讀取一行文本時,gawk會用預先定義好的字段分隔符劃分出各個數(shù)據字段。默認是空白字符(比如空格或制表符)
使用$1字段變量來顯示每行文本的第一個數(shù)據字段:
$ cat data2.txt
One line of test text.
Two lines of test text.
Three lines of test text.
$ gawk '{print $1}' data2.txt
One
Two
Three
要讀取的文件采用了其他的字段分隔符,可以通過-F選項指定.
將冒號指定為字段分隔符(-F:)來顯示了系統(tǒng)中密碼文件的第一個數(shù)據字段.
$ gawk -F: '{print $1}' /etc/passwd
root
daemon
bin
...
christine
sshd
8.1.2.4.在腳本中使用多條命令
將多條命令組合成一個常規(guī)的腳本,在命令之間加入分號.
$ echo "My name is Rich" | gawk '{$4="Christine"; print $0}'
My name is Christine
或使用次提示符
$ gawk '{
> $4=" Christine "
> print $0 }'
My name is Rich
My name is Christine
與sed類似,未指定文件時,會從STDIN讀取輸入,使用Ctrl+D生成EOF來結束腳本.
8.1.2.5.從文件中讀取腳本
跟sed編輯器一樣,gawk允許將腳本保存在文件中,然后在命令行中引用腳本.
$ cat script2.gawk
{ print $1 "'s home directory is " $6 }
$ gawk -F: -f script2.gawk /etc/passwd
root's home directory is /root
daemon's home directory is /usr/sbin
bin's home directory is /bin
...
sshd's home directory is /run/sshd
可以在腳本文件中指定多條命令。
$ cat script3.gawk
{
text = "'s home directory is "
print $1 text $6
}
$ gawk -F: -f script3.gawk /etc/passwd
在gawk腳本中,引用變量值時無須使用 $ 符號。
8.1.2.6.在處理數(shù)據前運行腳本
gawk允許指定腳本何時運行。
默認情況gawk會從輸入中讀取一行文本,然后對數(shù)據執(zhí)行腳本。有時候需要在處理數(shù)據前先運行腳本.
BEGIN關鍵字會強制gawk在讀取數(shù)據前執(zhí)行BEGIN關鍵字之后指定的腳本.
$ gawk 'BEGIN {print "Hello World!"}'
Hello World!
BEGIN關鍵字在處理任何數(shù)據之前僅應用指定的腳本,在顯示過文本后,腳本直接結束,不等待任何數(shù)據。
想使用正常的腳本來處理數(shù)據,則必須用另一個區(qū)域來定義腳本.
在gawk執(zhí)行了BEGIN腳本后,會用第二段腳本來處理文件數(shù)據。
兩段腳本仍會被視為gawk命令行中的一個文本字符串,所以需要加在一個單引號內.
$gawk 'BEGIN {print "請每次輸入一個單詞!"}
> {print $1}'
請每次輸入一個單詞!
apple red
apple
old new
old
hello world
hello
8.1.2.7.在處理數(shù)據后運行腳本
和BEGIN關鍵字類似,END關鍵字允許指定一段腳本,gawk會在處理完數(shù)據后執(zhí)行這段腳本
$gawk 'BEGIN {print "請每次輸入一個單詞!"}
> {print $1}
> END {print "輸入結束"}' data3.txt
請每次輸入一個單詞!
apple red
apple
old new
old
hello world
hello
輸入結束
特殊變量FS是定義字段分隔符的另一種方法。無須依靠腳本用戶通過命令行選項定義字段分隔符.
BEGIN {
print "The latest list of users and shells"
print "UserID \t Shell"
print "------- \t -------"
FS=":"
}
{
print $1 " \t " $7
}
END {
print "This concludes the listing"
}
$ gawk -f script4.gawk /etc/passwd
The latest list of users and shells
UserID Shell
-------- -------
root /bin/bash
daemon /usr/sbin/nologin
...
sshd /usr/sbin/nologin
This concludes the listing
更多gawk進階
8.2.sed編輯器基礎命令
選項 | 描述 |
---|---|
-e commands | 處理輸入時,加入額外的sed命令 |
-f file | 處理輸入時,將file中指定的命令添加到已有命令中 |
-n | 不產生輸出,使用p(print)命令完成輸出 |
8.2.1.更多的替換選項
8.2.1.1.替換標志
替換命令在替換多行中的文本時使用,默認情況下它只替換每行中出現(xiàn)的第一處匹配文本。
$ cat data4.txt
This is a test of the test script.
This is the second test of the test script.
$ sed 's/test/trial/' data4.txt
This is a trial of the test script.
This is the second trial of the test script.
要想替換每行中所有的匹配文本,必須使用替換標志(substitution flag)。替換標志在替換命令字符串之后設置。
sed 's/pattern/replacement/flags' file
有4種可用的替換標志:
- 數(shù)字,指明新文本將替換行中的第幾處匹配。
- g,指明新文本將替換行中所有的匹配。
- p,指明打印出替換后的行。
- w file,將替換的結果寫入文件。
數(shù)字替換標志,替換指定匹配處:
$ sed 's/test/trial/2' data4.txt
This is a test of the trial script.
This is the second test of the trial script.
g替換標志,替換所有:
$ sed 's/test/trial/g' data4.txt
This is a trial of the trial script.
This is the second trial of the trial script.
p替換標志,打印出包含替換命令中指定匹配模式的文本行。通常和sed的-n選項配合使用.
-n選項會抑制sed編輯器的輸出,替換標志p會輸出替換后的行。配合使用只輸出被替換命令修改過的行。
$ cat data5.txt
This is a test line.
This is a different line.
$ sed -n 's/test/trial/p' data5.txt
This is a trial line.
w替換標志,將替換的結果(僅替換的行)輸出保存到指定文件中.
sed編輯器的正常輸出會被保存在STDOUT中。
$ sed 's/test/trial/w test.txt' data5.txt
This is a trial line.
This is a different line.
$ cat test.txt
This is a trial line.
8.2.1.2.替換字符
在字符串中遇到不方便在替換模式中使用的字符。比如正斜線(/)替換文件中的路徑會比較煩瑣。
由于正斜線被用作替換命令的分隔符,因此它在匹配模式和替換文本中出現(xiàn)時,必須使用反斜線來轉義。
如果想將/etc/passwd文件中的bash shell替換為C shell:
$ sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd
為了解決這個問題,sed編輯器允許選擇其他字符作為替換命令的替代分隔符:
$ sed 's!/bin/bash!/bin/csh!' /etc/passwd
以上感嘆號(!)被用作替換命令的分隔符.
8.2.2.使用地址
默認情況下,在sed編輯器中使用的命令會應用于所有的文本行。如果只想將命令應用于特定的某一行或某些行,可使用行尋址。
sed編輯器中有兩種形式的行尋址。
- 以數(shù)字形式表示的行區(qū)間。
- 匹配行內文本的模式。
行尋址的格式:
[address]command
# 將針對特定地址的多個命令分組
address {
command1
command2
command3
}
sed編輯器會將指定的各個命令應用于匹配指定地址的文本行。
8.2.2.1.數(shù)字形式行尋址
在使用數(shù)字形式的行尋址時,可以用行號來引用文本流中的特定行。
sed編輯器會將文本流中的第一行編號為1,第n行編號為n.
在命令中指定的行地址既可以是單個行號,也可以是用起始行號、逗號以及結尾行號指定的行區(qū)間。
$可表示最后一行.
$ cat data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
$ sed '2s/dog/cat/' data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
$ sed '2,3s/dog/cat/' data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.
$ sed '2,$s/dog/cat/' data1.txt ##
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
8.2.2.2.使用文本模式過濾
sed編輯器允許指定文本模式來過濾出命令所應用的行.
必須將指定的模式(patten)放入正斜線內。sed編輯器會將該命令應用于包含匹配模式的行。
sed '/pattern/command' file
$ grep /bin/bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
christine:x:1001:1001::/home/christine:/bin/bash
rich:x:1002:1002::/home/rich:/bin/bash
$ sed '/rich/s/bash/csh/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
...
rich:x:1002:1002::/home/rich:/bin/csh
在文本模式中引入正則表達式來創(chuàng)建匹配效果更好的模式.
8.2.2.3.命令組
在單行中執(zhí)行多條命令,可以用 {} 組合在一起,sed編輯器會執(zhí)行匹配地址中列出的所有命令.
可以在一組命令前指定行區(qū)間.
$ sed '2{
> s/fox/toad/
> s/dog/cat/
> }' data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown toad jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
8.2.3.刪除行(d)
文本替換命令并非sed編輯器唯一的命令。如果需要刪除文本流中的特定行,可以使用刪除(d)命令。
刪除命令會刪除匹配指定模式的所有行。使用該命令時如果忘記加入尋址模式,則流中的所有文本行都會被刪除.
同樣的,sed編輯器的刪除命令也不會修改原始文件.
$ cat data1.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
$ sed 'd' data1.txt
從數(shù)據流中刪除特定的文本行:
- 通過行號指定:
$ cat data6.txt This is line number 1. This is line number 2. This is the 3rd line. This is the 4th line. $ sed '3d' data6.txt This is line number 1. This is line number 2. This is the 4th line.
- 通過特定行區(qū)間指定:
$ sed '2,3d' data6.txt This is line number 1. This is the 4th line.
- 特殊的末行字符指定:
$ sed '3,$d' data6.txt This is line number 1. This is line number 2.
- 模式匹配:
$ sed '/number 1/d' data6.txt This is line number 2. This is the 3rd line. This is the 4th line.
- 使用兩個文本模式來刪除某個區(qū)間內的行:
一個模式會“啟用”行刪除功能,第二個模式會“關閉”行刪除功能,sed編輯器會刪除包含兩個指定行與之間的所有行.
只要sed編輯器在數(shù)據流中匹配到了開始模式,就會啟用刪除功能.第二個包含開始模式的行也會觸發(fā)刪除命令,未找到結束模式,則剩余的行會被全部刪除.
$ cat data7.txt
This is line number 1.
This is line number 2.
This is the 3rd line.
This is the 4th line.
This is line number 1 again; we want to keep it.
This is more text we want to keep.
Last line in the file; we want to keep it.
$ sed '/1/,/3/d' data7.txt
This is the 4th line.
8.2.4.插入和附加文本(a\i)
sed編輯器也可以向數(shù)據流中插入和附加文本行。
- 插入(insert)(i)命令會在指定行前增加一行。
- 附加(append)(a)命令會在指定行后增加一行。
命令不能在單個命令行中使用。必須指定 插入還是附加 到另一行.
sed '[address]command\
new line' file
插入與附加
$ echo "Test Line 2" | sed 'i\Test Line 1'
Test Line 1
Test Line 2
$ echo "Test Line 2" | sed 'a\Test Line 1'
Test Line 2
Test Line 1
$ echo "Test Line 2" | sed 'i\
> Test Line 1'
Test Line 1
Test Line 2
向數(shù)據流內部插入或附加數(shù)據,須用地址指定數(shù)據出現(xiàn)在什么位置。
只能指定一個行地址。不能用行區(qū)間.(只能將文本插入或附加到某一行而不是行區(qū)間的前后)
$ cat data6.txt
This is line number 1.
This is line number 2.
This is the 3rd line.
This is the 4th line.
$ sed '3i\
> This is an inserted line.
> ' data6.txt
This is line number 1.
This is line number 2.
This is an inserted line.
This is the 3rd line.
This is the 4th line.
$
$ sed '$a\
> This line was added to the end of the file.
> ' data6.txt
This is line number 1.
This is line number 2.
This is the 3rd line.
This is the 4th line.
This line was added to the end of the file.
在數(shù)據流的起始位置增加一個新行。只要在第一行之前插入新行。
在數(shù)據流的末尾位置增加一個新行。只要在最后一行之后附加新行。
添加多行
要插入或附加多行文本,必須在要插入或附加的每行新文本末尾使用反斜線(\):
$ sed '1i\
> This is an inserted line.\
> This is another inserted line.
> ' data6.txt
This is an inserted line.
This is another inserted line.
This is line number 1.
This is line number 2.
This is the 3rd line.
This is the 4th line.
8.2.5.修改行?
修改(c)命令允許修改數(shù)據流中整行文本的內容。必須在sed命令中單獨指定一行.
$ sed '2c\
> This is a changed line of text.
> ' data6.txt
This is line number 1.
This is a changed line of text.
This is the 3rd line.
This is the 4th line.
$ sed '/3rd line/c\
> This is a changed line of text.
> ' data6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is the 4th line.
文本模式修改命令會修改所匹配到的任意文本行.
在修改命令中使用地址區(qū)間,會將區(qū)間內的所有內容替換為一個文本內容,而不會將每一行替換一次.
$ cat data6.txt
This is line number 1.
This is line number 2.
This is the 3rd line.
This is the 4th line.
$ sed '2,3c\
> This is a changed line of text.
> ' data6.txt
This is line number 1.
This is a changed line of text.
This is the 4th line.
8.2.6.轉換命令(y)
轉換(y)命令是唯一可以處理單個字符的sed編輯器命令。
sed '[address]y/inchars/outchars/' file
轉換命令會對inchars和outchars的字符進行一對一的映射。
inchars中的第一個字符會被轉換為outchars中的第一個字符,inchars中的第n字符會被轉換成outchars中的第n字符。
如果inchars和outchars的長度不同,sed編輯器會產生錯誤消息。
轉換命令是一個全局命令,會對文本行中匹配到的所有指定字符進行轉換,不考慮字符出現(xiàn)的位置,也無法對特定位置字符的轉換進行限制:
$ cat data9.txt
This is line 1.
This is line 5.
This is line 1 again.
This is line 3 again.
This is the last file line.
$ sed 'y/123/789/' data9.txt
This is line 7.
This is line 5.
This is line 7 again.
This is line 9 again.
This is the last file line.
$ echo "Test #1 of try #1." | sed 'y/123/678/'
Test #6 of try #6.
8.2.7.打印
打印數(shù)據流中的信息。
- 打?。╬)命令用于打印文本行。
- 等號(=)命令用于打印行號。
- 列出(l)命令用于列出行。
打印行§
和替換命令中的p標志類似,打印命令用于打印sed編輯器輸出中的一行。
$ echo "this is a test" | sed 'p'
this is a test
this is a test
常見的用法是打印包含匹配文本模式的行,通過-n抑制其他行輸出:
$ cat data6.txt
This is line number 1.
This is line number 2.
This is the 3rd line.
This is the 4th line.
$ sed -n '/3rd line/p' data6.txt
This is the 3rd line.
在使用替換或修改命令做出改動之前查看相應的行:
$ sed -n '/3/{
> p
> s/line/test/p
> }' data6.txt
This is the 3rd line.
This is the 3rd test.
打印數(shù)據流中的部分行:
$ sed -n '2,3p' data6.txt
This is line number 2.
This is the 3rd line.
打印行號(=)
=等號命令會打印文本行在數(shù)據流中的行號。行號由數(shù)據流中的換行符決定。
sed編輯器在實際文本行之前會先打印行號。
$ cat data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
$
$ sed '=' data1.txt
1
The quick brown fox jumps over the lazy dog.
2
The quick brown fox jumps over the lazy dog.
3
The quick brown fox jumps over the lazy dog.
4
The quick brown fox jumps over the lazy dog.
讓sed編輯器只顯示包含匹配文本模式的文本行的行號和內容:
$ cat data7.txt
This is line number 1.
This is line number 2.
This is the 3rd line.
This is the 4th line.
This is line number 1 again; we want to keep it.
This is more text we want to keep.
Last line in the file; we want to keep it.
$ sed -n '/text/{
> =
> p
> }' data7.txt
6
This is more text we want to keep.
列出行(l)
列出命令可以打印數(shù)據流中的文本和不可打印字符。
在顯示不可打印字符的時候,可以使用反斜杠加八進制值,或使用標準的C語言命名規(guī)范,比如\t用于代表制表符.
$ cat data10.txt
This line contains tabs.
This line does contain tabs.
$ sed -n 'l' data10.txt
This\tline\tcontains\ttabs.$
This line does contain tabs.$
8.2.8.處理文件
寫入文件(w)
命令格式:
sed '[address]w filename' file
filename可以使用相對路徑或絕對路徑.用戶必須有文件的寫權限。地址可以是任意類型的尋址方式.
$ sed '1,2w test.txt' data6.txt
This is line number 1.
This is line number 2.
This is the 3rd line.
This is the 4th line.
$ cat test.txt
This is line number 1.
This is line number 2.
可以使用-n選項抑制輸出.
根據一些文本值,從主文件(比如下面的郵件列表)中創(chuàng)建一份數(shù)據文件,使用寫入命令會非常方便:
$ cat data12.txt
Blum, R Browncoat
McGuiness, A Alliance
Bresnahan, C Browncoat
Harken, C Alliance
$ sed -n '/Browncoat/w Browncoats.txt' data12.txt
$ cat Browncoats.txt
Blum, R Browncoat
Bresnahan, C Browncoat
讀取數(shù)據?
?。╮)命令允許將一條獨立文件中的數(shù)據插入數(shù)據流。將文件內容插入指定地址之后.
無法使用地址區(qū)間,只能指定單個行號或文本模式地址。
命令格式:
sed '[address]r filename' file
$ cat data13.txt
This is an added line.
This is a second added line.
$ sed '3r data13.txt' data6.txt
This is line number 1.
This is line number 2.
This is the 3rd line.
This is an added line.
This is a second added line.
This is the 4th line.
$ sed '/number 2/r data13.txt' data6.txt
This is line number 1.
This is line number 2.
This is an added line.
This is a second added line.
This is the 3rd line.
This is the 4th line.
$ sed '$r data13.txt' data6.txt
This is line number 1.
This is line number 2.
This is the 3rd line.
This is the 4th line.
This is an added line.
This is a second added line.
讀取命令和刪除命令配合使用,利用一個文件中的數(shù)據來替換另一個文件中的占位文本。
假如你保存在文本文件中的套用信件如下:
$ cat notice.std
Would the following people:
LIST
please report to the ship's captain.
在占位文本后插入名單,只需使用讀取命令即可。但這樣的話,占位文本仍然會留在輸出中。為此,可以用刪除命令刪除占位文本
$ sed '/LIST/{
> r data12.txt
> d
> }' notice.std
Would the following people:
Blum, R Browncoat
McGuiness, A Alliance
Bresnahan, C Browncoat
Harken, C Alliance
please report to the ship's captain.
文件夾下文件
-s選項可以告知sed將目錄內的各個文件作為單獨的流
$ sed -sn '1s!/bin/sh!/bin/bash!' OldScripts/*.sh
將 OldScripts文件夾下的.sh文件第一行內的/bin/sh的替換為/bin/bash.文章來源:http://www.zghlxwxcb.cn/news/detail-637138.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-637138.html
到了這里,關于【Linux命令行與Shell腳本編程】第十八章 文本處理與編輯器基礎的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!