sed編輯器基礎

2021-10-04 21:09:27 字數 4555 閱讀 1302

1. 替換標記

sed的-s選項用來替換檔案中的內容,但是僅僅作用於每行的第一處,若需要替換其他地方則需要替換標記

s/pattern/replacement/flags

4種可用替換標記

1. 數字 表示文字每行記錄的第幾處進行替換

2. g 表示替換所有

3. p 表示原先行的內容要列印出來

4. w file 將替換結果寫到檔案中

-n 與 -p 使用效果是只輸出被修改的行

-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.

$

2. 行定址方式

預設情況下,在sed編輯器中使用的命令會作用於文字資料的所有行。如果只想將命令作用

於特定行或某些行,則必須用行定址(line addressing)

行定址方式:

1、數字形式表示區間

2、文字模式進行過濾出行

只修改第2行

$ 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

$修改2-3行

$ 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

$修改第2行至最後一行,$表示最後一行

$ 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

$$ sed '/samantha/s/bash/csh/' /etc/passwd

表示匹配包含samantha的行,並將bash值修改為csh值

3. 刪除行

刪除命令d名副其實,它會刪除匹配指定定址模式的所有行。使用該命令時要特別小心,如

果你忘記加入定址模式的話,流中的所有文字行都會被刪除

$ sed 'd' data1.txt

刪除檔案所有行

$ sed '3d' data6.txt

刪除第3行

$ sed '2,3d' data6.txt

刪除第2-3行

$ sed '3,$d' data6.txt

刪除第3行到最後一行

4. 插入和附加文字

插入(insert)命令(i)會在指定行前增加乙個新行;

# sed '[address]command\new line'

$ echo "test line 2" | sed 'i\test line 1'

test line 1

test line 2

test line 2該行前插入test line 1

$ echo "test line 2" | sed 'a\test line 1'

test line 2

test line 1

$test line 2該行後附加test line 1

sed '3i\

> this is an inserted line.' data6.txt

表示第3行前插入行 this is an inserted line.

$ sed '3a\

$ sed '1i\

> this is one line of new text.\

> this is another line of new text.' data6.txt

this is one line of new text.

this is another line of new text.

this is line number 1.

this is line number 2.

this is line number 3.

this is line number 4.

$表示第1行前插入行 this is one line of new text. 與行 this is another line of new text.

5. 修改行

修改(change)命令允許修改資料流中整行文字的內容。它跟插入和附加命令的工作機制

一樣,你必須在sed命令中單獨指定新行

$ sed '3c\

> 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 line number 4.

$

6. 轉換命令

[address]y/inchars/outchars/

$ echo "this 1 is a test of 1 try." | sed 'y/123/456/'

this 4 is a test of 4 try.

$$ sed 'y/123/789/' data8.txt

this is line number 7.

this is line number 8.

this is line number 9.

this is line number 4.

this is line number 7 again.

this is yet another line.

this is the last line in the file.

$

7. 列印

p命令用來列印文字行;

等號(=)命令用來列印行號;

l(小寫的l)命令用來列出行。

$ cat data6.txt

this is line number 1.

this is line number 2.

this is line number 3.

this is line number 4.

$$ sed -n '/number 3/p' data6.txt

this is line number 3.

$$ sed -n '2,3p' data6.txt

this is line number 2.

this is line number 3.

$$ sed -n '/3/' data6.txt

this is line number 3.

this is test number 3.

$sed編輯器命令會查詢包含數字3的行,然後執行兩條命令。首先,指令碼用p命令來列印出原

始行;然後它用s命令替換文字,並用p標記列印出替換結果。輸出同時顯示了原來的行文字和新

的行文字

$ 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

1the quick brown fox jumps over the lazy dog.

2the quick brown fox jumps over the lazy dog.

3the quick brown fox jumps over the lazy dog.

4the quick brown fox jumps over the lazy dog.

$

sed編輯器基礎

一 更多的替換選項 替換標記 root localhost sed cat data4.txt this is a test of the test script.this is the second test of the test script.root localhost sed sed s ...

sed編輯器基礎操作

sed e 操作 檔案1檔案2 sed n e 操作 檔案1檔案2 sed f 指令碼檔案 檔案1 檔案2 sed e 操作 檔案1 檔案2 sed e n 檔案1 檔案2 e或 expression 表示用指定命令來處理輸入的文字檔案,只有乙個操作命令時可省略,一般在執行 多個操作命令使用 f 或...

Linux基礎 sed 流編輯器

sed stream editer 流編輯器 sed r 擴充套件正澤 可以用小括號 n 靜默模式 不但輸出 i 直接寫入 sed 3d test 第三行 sed 3c 11111 test 第三行改成11111 sed 1d 3d test 刪除第一行 第三行 sed 1,3d test 刪除第一...