[英]awk print the number of columns between a selected range from awk
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.