簡體   English   中英

Unix命令了解sed腳本

[英]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腳本中絕不是這種情況。

根據 手冊頁

N正在指定將輸入的下一行讀/追加到模式空間。

D刪除圖案空間中的第一個嵌入式換行符。 從下一個周期開始,但是如果模式空間中仍有數據,則跳過從輸入中讀取的操作。

$匹配最后一行。

! 表示它與N實際定義不匹配

t label如果自讀最后一條輸入線和最后一條tT命令以來,一個s///已成功替換,則跳轉到label; 如果省略了label,則跳轉到腳本結尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM