請解釋下面提供的 awk 腳本

請解釋下面提供的 awk 腳本

任何人都可以請我一步步解釋一下下面寫的 AWK 腳本。

我在腳本中編寫了以下程式碼來格式化平面文件資料。只是想了解一下,以便我可以重複使用——我不是 UNIX 人員,但任務已分配給我。

awk -vsep=$SEPARATOR 'NR>2{if(NF){if(!s){gsub(" *"sep"[ \t]*",sep);printf "%d%s\n",NR-2,$0}}else s=1}' file_name > new_file

 # where $SEPARATOR = ';'

提前致謝。

答案1

命令列選項-vsep=$SEPERATOR將 awk 變數sep(在搜尋/替換中使用)設定為您指定的任何內容。;在你的情況下。

# NR = Number of current Record, or line number
# Skip the first line
if ( NR > 2 ) {

  # NF = Number of fields in the current record
  # If the line contains something other than a blank line or the 
  # awk field separator characters (whitespace by default)
  if ( NF ) {

    # If we have not seen a blank line (script flag s)
    if ( !s ) {

      # Search the current line repeatedly (gsub) for any number of spaces (" *") 
      # before a ";" then any number of spaces or tabs ([ \t]*) after the `;`
      # and replace it all with just a ";"
      gsub( " *"sep"[ \t]*", sep );

      # Print the line number, 0 based (NR-2) as a signed decimal integer (`%d`)
      # then the complete line ($0) followed by a new line character (\n)
      printf "%d%s\n", NR-2, $0;
    }

  } else { 

    # Set the "seen a blank line" flag
    s = 1
  }

}

file_name > new_file將輸出寫入一個名為的新文件new_file

順便說一句,如果您按如下方式建立腳本,那麼如果空行後出現大量數據,則閱讀起來會容易得多,並且速度會更快。

awk -vsep=$SEPERATOR '{

# Skip the first line
if (NR == 1) { next; }

# Stop processing if we see a blank line
if (NF == 0) { exit; }

# Remove spaces before and spaces/tabs after separator
gsub( " *"sep"[ \t]*", sep );

# Print the line with a record number starting from 0
printf "%d%s\n", NR-2, $0;

}' file_name > new_file

相關內容