首頁>Linux>source

我有 stdout 有很多看起来像這樣的文字塊:

% QUESTION
Who played drums for The Beatles?

% QUESTION
Who played
guitar
for The Beatles?

% QUESTION
Who played
bass for The Beatles
?

這裏的想法是將檔案分為"塊",其中每个塊均以 % QUESTION行開頭 .我想編寫一个指令碼,將print此資料的第n个塊。

例如,發行 nthchunk 3 應该print

Who played
bass for The Beatles
?

我该怎麼做呢?

最新回復
  • 5月前
    1 #

    awk 支援正則表達式作為記錄分隔符的實現( RS ),例如GNU awk ,您可以這樣做:

    awk -v n=3 -v RS='(\n+|^)% QUESTION\n' 'NR == n+1 {print; exit}' < questions.txt
    

  • GNOME如何發音?
  • bash:查詢唯一名稱的频率