[英]Unix command understanding a sed script
我需要幫助來了解以下代碼。 為什么在這里使用N和D? 他們在這里是什么意思?
這是一個sed
命令,用於打印重復的輸入行。
sort file | sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'
這是腳本的翻譯:
for each line of input: # sed does this loop automatically
if it's not the last line: # This is what $! means
append a \n (newline) to the current line # N means this
append the next line of input to the current line # and this
# Note that the "current line" may now contain a \n in the middle!
if the current line matches some string, followed by a \n,
followed by that same string again: # s command's pattern
delete the \n and the second copy of the string # s command's action
if the s command matched: # t means this
go to endOfScript # and this
delete everything up to the first \n in the current line # D means this
go to endOfLoop # and this
endOfScript:
print the current line, followed by \n, to stdout # sed does this automatically
endOfLoop:
# just return to the top of the loop for the next line of input
D命令實際上稍微復雜一點:如果當前行還剩下任何字符,它會禁止讀取循環頂部的下一行。 但是,在此sed腳本中絕不是這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.