国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Linux命令行與Shell腳本編程】第十八章 文本處理與編輯器基礎

這篇具有很好參考價值的文章主要介紹了【Linux命令行與Shell腳本編程】第十八章 文本處理與編輯器基礎。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

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í)行下列操作:

  1. 從輸入中讀取一行數(shù)據。
  2. 根據所提供的編輯器命令匹配數(shù)據。
  3. 按照命令修改數(shù)據流中的數(shù)據。
  4. 將新的數(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.

【Linux命令行與Shell腳本編程】第十八章 文本處理與編輯器基礎,Linux命令行與shell腳本編程,linux,編輯器,bash,運維文章來源地址http://www.zghlxwxcb.cn/news/detail-637138.html

到了這里,關于【Linux命令行與Shell腳本編程】第十八章 文本處理與編輯器基礎的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!

本文來自互聯(lián)網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • Linux Shell 腳本編程學習之【第3章 正則表達式 (第二部分) grep命令】

    1、文本搜索工具 2、GREP 是Global search Regular Expression and Print out the line的簡稱,即全面搜索正則表達式并把行打印出來。 3、grep命令的模式十分靈活,可以是字符串,也可以是變量,還可以是正則表達式。模式中包含空格,則必須用雙引號括起來。 選 項 意 義 -c 只輸出匹配行

    2024年02月16日
    瀏覽(19)
  • Shell腳本——編程規(guī)范與echo命令

    Shell腳本——編程規(guī)范與echo命令

    目錄 一.Shell腳本編程概述 1.基本概念 2.作用 3.Linux系統(tǒng)中常見的Shell腳本種類 4.應用場景 5.問題補充 5.1 為什么系統(tǒng)上合法的Shel1要寫入/etc/she1ls這個文件? 5.2 用戶什么時候可以取得shell來工作?用戶默認會取得哪一個shell? 二.Shell腳本的構成規(guī)范 三.?Shell腳本的執(zhí)行 1.腳本執(zhí)行

    2024年02月05日
    瀏覽(40)
  • Linux 系統(tǒng)shell腳本編程筆記——腳本入門

    Linux 系統(tǒng)shell腳本編程筆記——腳本入門

    目錄 1、創(chuàng)建shell腳本文件 ?2、顯示消息 3、?環(huán)境變量 4、用戶變量 5、命令替換 ?編輯 ?6、重定向輸入與輸出 6.1、輸出重定向 ?6.2、輸入重定向 ?編輯 7、執(zhí)行數(shù)學運算 7.1、expr命令 7.2、bc的基本用法 ?8、退出腳本 完整筆記請前往此處獲?。篽ttps://download.csdn.net/download/qq

    2024年02月06日
    瀏覽(28)
  • Linux——Shell腳本編程(1)

    Linux——Shell腳本編程(1)

    1)Linux運維工程師在進行服務器集群管理時,需要編寫Shell程序來進行服務器管理。 2)對于 JavaEE 和 Python 程序員來說,工作的需要,要求你編寫一些 Shell腳本進行程序或者是服務器的維護,比如編寫一個定時備份數(shù)據庫的腳本。 3) 對于大數(shù)據程序員來說,需要編寫Shell程序來管

    2024年02月09日
    瀏覽(44)
  • Linux_5_Shell腳本編程

    Linux_5_Shell腳本編程

    程序:算法+數(shù)據結構 數(shù)據:是程序的核心 算法:處理數(shù)據的方式 數(shù)據結構: 數(shù)據在計算機中的類型和組織方式 面向過程語言 做一件事情,排出個步驟,第一步干什么,第二步干什么,如果出現(xiàn)情況A,做什么處理,如果出現(xiàn)了情況B,做什么處理 問題規(guī)模小,可以步驟化,按部

    2024年02月13日
    瀏覽(19)
  • Linux實驗4 shell腳本編程基礎

    Linux實驗4 shell腳本編程基礎

    1.假設在/tmp下有以當前用戶的帳號命名的目錄,請在命令行中臨時修改環(huán)境變量PATH的值,要求該目錄的路徑附加到該變量的最后。 2.請在命令行中臨時設置命令輸入提示行格式為:“當前系統(tǒng)時間-用戶#”。 3.在命令行定義一個字符串變量str,并且賦值為“test for shell”,然

    2024年04月17日
    瀏覽(22)
  • Linux shell編程學習筆記44:編寫一個腳本,將md5sum命令執(zhí)行結果保存到變量中,進而比較兩個文件內容是否相同

    Linux shell編程學習筆記44:編寫一個腳本,將md5sum命令執(zhí)行結果保存到變量中,進而比較兩個文件內容是否相同

    在? Linux shell編程學習筆記42:md5sum https://blog.csdn.net/Purpleendurer/article/details/137125672?spm=1001.2014.3001.5501 中,我們提到編寫一個在Linux系統(tǒng)下比較兩個文件內容是否相同的腳本。 基本思路是: 其中有兩個難點: 1.文件的md5值的獲取 2.md5值的比較 對于第1個難點,我們的解決辦法是

    2024年04月10日
    瀏覽(28)
  • Linux系統(tǒng)Shell腳本編程之條件語句

    Linux系統(tǒng)Shell腳本編程之條件語句

    Shell 環(huán)境根據命令執(zhí)行后的返回狀態(tài)值 \\\" $? \\\" 來判斷是否執(zhí)行成功,當返回值為0時表示成功,否則表示失敗或異常(非0值)。 使用專門的測試工具 test 命令,可以對特定條件進行測試,并根據返回值(值為0)來判斷是否成立。 test命令格式 文件測試指的是根據給定的路徑名

    2024年01月25日
    瀏覽(24)
  • 3.7 Linux shell腳本編程(分支語句、循環(huán)語句)

    3.7 Linux shell腳本編程(分支語句、循環(huán)語句)

    目錄 分支語句(對標C語言中的if) 多路分支語句(對標C語言中的swich case) 分支語句(對標C語言中的if) 語法結構: ? ? ? ? ?if ? ?表達式 ? ? ? ? ??? ??? ?then ?命令表 ? ? ? ? ?fi ? ? 如果表達式為真, 則執(zhí)行命令表中的命令; 否則退出if語句, 即執(zhí)行fi后面的語句。

    2024年02月02日
    瀏覽(24)
  • Linux shell編程學習筆記29:shell自帶的 腳本調試 選項

    Linux shell編程學習筆記29:shell自帶的 腳本調試 選項

    Linux shell腳本的調試方法比較多,上次我們探討和測試了shell內建命令set所提供的一些調試選項,其實 shell 本身也提供了一些調試選項。我們以bash為例來看看。 purleEndurer @ csdn ~ $ bash --help GNU bash, version 4.2.46(2)-release-(x86_64-redhat-linux-gnu) Usage: ?bash [GNU long option] [option] ... ? ? ?

    2024年02月04日
    瀏覽(16)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包