簡體   English   中英

awk 打印 awk 選定范圍之間的列數

[英]awk print the number of columns between a selected range from awk

  1. 我有下一行顯示文件中的一系列文本。 這是正確的,但我想在每行的第一行顯示行數。 我用不同的 s 選項證明沒有結果。
    buffer=`awk -v desde="$DESDE" -v hasta="$HASTA" 'NR>=desde&&NR<=hasta' "$FICH"

以下命令的 output 是下一個:

1:20220413:20:Curso Astrología:5:Vicente Ferrer

1:10042022:0:Donación instituto Samye:103:Propia

14:20220428:0:Candelario Yeshe Nyimpo Inc:9:Dudjom Tersar

1:20220512:60:Ayuda por el Hambre y Violencia:6:Vicente Ferrer

 Total Lineas: 43 | Total Pag: 2  |Buffer: De 0  hasta 26 | 27

但我想要下一個結果:

1 1:20220413:20:Curso Astrología:5:Vicente Ferrer

2 1:10042022:0:Donación instituto Samye:103:Propia

3 14:20220428:0:Candelario Yeshe Nyimpo Inc:9:Dudjom Tersar

4 1:20220512:60:Ayuda por el Hambre y Violencia:6:Vicente Ferrer

 Total Lineas: 43 | Total Pag: 2  |Buffer: De 0  hasta 26 | 27

謝謝你先進

第一個解決方案:使用您顯示的示例,請嘗試使用以下awk代碼。 我改進了您嘗試的代碼,還注意以防行號與hasta值交叉,它將簡單地從程序中退出以節省一些周期和時間。

awk -v desde="$DESDE" -v hasta="$HASTA" '
NR>hasta{ exit }
NR>=desde && NR<=hasta{
  if(/^ Total/ || !NF){print; next }
  if(NF){print ++count,$0}
}
' "$FICH"


如果您想這樣做,您顯示的 output 的第二個解決方案將在您顯示的 output 上運行:

awk '/^ Total/ || !NF{print;next} NF{print ++count,$0}' Input_file

說明:為以上代碼添加詳細說明。

awk '                 ##Starting awk program from here.
/^ Total/ || !NF{     ##Checking condition if line starts from space Total OR its NULL then.
  print               ##Print that line.
  next                ##next will skip all statements from here.
}
NF{                   ##Checking condition if NF is NOT NULL for current line then:
  print ++count,$0    ##Printing count value(increasing it with 1 before printing) followed by current line value.
}
' Input_file          ##Mentioning Input_file name here.

暫無
暫無

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

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