您的回答已經由user提供,只是check here。
一些參考
在sed的最簡單的調用,它在模式空間,即一行文本。來自輸入的1行\ n分隔文本。模式空間中的單行沒有\ n ...這就是爲什麼你的正則表達式沒有找到任何東西。
您可以在模式空間中讀取多行,並且操作起來令人驚訝,但使用超過正常的努力.. Sed有一組允許這種類型的命令的命令...這是一個指向sed的命令摘要。這是我找到的最好的一個,讓我滾動。
但是,一旦開始使用sed的微指令,忘記了「單行」的想法。將它像結構化程序一樣佈置,直到你感覺到它爲止是非常有用的......它非常簡單,同樣很不尋常。您可以將其視爲文本編輯的「彙編語言」。簡介:使用sed來做簡單的事情,也許更多一些,但是總的來說,當它超出單行使用時,大多數人更喜歡別的東西... 我會讓別人建議別的東西..我真的不知道什麼是最好的選擇將是(我會用sed,但那是因爲我不知道perl的不夠好。)
sed '/^a test$/{
$!{ N # append the next line when not on the last line
s/^a test\nPlease do not$/not a test\nBe/
# now test for a successful substitution, otherwise
#+ unpaired "a test" lines would be mis-handled
t sub-yes # branch_on_substitute (goto label :sub-yes)
:sub-not # a label (not essential; here to self document)
# if no substituion, print only the first line
P # pattern_first_line_print
D # pattern_ltrunc(line+nl)_top/cycle
:sub-yes # a label (the goto target of the 't' branch)
# fall through to final auto-pattern_print (2 lines)
}
}' alpha.txt
這是同一個腳本,凝結成顯然難以閱讀和處理,但有些人會把這種可疑的稱爲單線式
sed '/^a test$/{$!{N;s/^a test\nPlease do not$/not a test\nBe/;ty;P;D;:y}}' alpha.txt
這是我的命令「作弊表」
: # label
= # line_number
a # append_text_to_stdout_after_flush
b # branch_unconditional
c # range_change
d # pattern_delete_top/cycle
D # pattern_ltrunc(line+nl)_top/cycle
g # pattern=hold
G # pattern+=nl+hold
h # hold=pattern
H # hold+=nl+pattern
i # insert_text_to_stdout_now
l # pattern_list
n # pattern_flush=nextline_continue
N # pattern+=nl+nextline
p # pattern_print
P # pattern_first_line_print
q # flush_quit
r # append_file_to_stdout_after_flush
s # substitute
t # branch_on_substitute
w # append_pattern_to_file_now
x # swap_pattern_and_hold
y # transform_chars
如果有一個更通用的[mcve]來查看什麼是常量,哪些不是:是否必須在'/ var/www'上?另外,使用'sed'作爲多行似乎不是最乾淨的方式:'awk'可以更好地處理這種方式。 – fedorqui