AWK:如何自动增加数字?

AWK:如何自动增加数字?,awk,sed,Awk,Sed,我有一个文件。文件内容是: 2021012600088000003 | 3 | 33.00 | 20210126 | 15:30 1 | 20210126000000000000002207 | 122021016080109 | 1000 | 1000000000000319 | 100058110000000325 | 402041000012 | 402041000012 | PT07 | 6210670000123645|收款方户名|2021-01-26 | 2021-01-26 | 1

我有一个文件。文件内容是:

2021012600088000003 | 3 | 33.00 | 20210126 | 15:30
1 | 20210126000000000000002207 | 122021016080109 | 1000 | 1000000000000319 | 100058110000000325 | 402041000012 | 402041000012 | PT07 | 6210670000123645|收款方户名|2021-01-26 | 2021-01-26 | 10.00 | TN | NCS | 12 | 875466
2 | 20210126000000000000002208 | 12202101608010 | 1000 | 1000000000000319 | 100058110000000325 | 402041000012 | 402041000012 | PT06 | 620160700000123645|收款方户名|2021-01-26 | 2021-01-26 | 20.00 | TN | NCS | 12 | 875466
3 | 20210126000000000000002209 | 122021011608011 | 1000 | 1000000000000319 | 100058110000000325 | 402041000012 | 402041000012 | PT08 | 620160700000123645|收款方户名|2021-01-26 | 2021-01-26 | 3.00 | TN | NCS | 12 | 875466
我使用awk命令:

awk -F"|" 'NR==1{print $1};FNR==2{print $2,$3}'  testfile
得到以下结果:

20210126000880000003
20210126000000000000000000002207 1220210126080109
我想让数字自动增加:

awk -F"|" 'NR==1{print $1+1};FNR==2{print $2+1,$3+1}' testfile
但得到以下结果:

20210126000880001024
20210126000000000944237587726336 1220210126080110
有问题:

我想知道数字是自动增加的:希望结果是:

20210126000880000003
20210126000000000000000000002207|1220210126080109
-------------------------------------------------
20210126000880000004
20210126000000000000000000002208|1220210126080110
--------------------------------------------------
20210126000880000005
20210126000000000000000000002209|1220210126080111
如何自动增加


谢谢

Anubhava有最好的解决方案,但是对于不支持-M(大数字)的GNU awk的旧版本,您可以尝试以下方法:

awk -F\| 'NR==1 { print $1;hed=$1;hed1=substr($1,(length($1)-1));next; } !/^$/ {print $2" "$3 } /^$/ { print "--------------------------------------------------";printf "%s%s\n",substr(hed,1,((length(hed))-(length(hed1)+1))),++hed1 }' testfile
说明:

awk -F\| 'NR==1 {                                                   # Set field delimiter to | and process the first line
                   print $1;                                        # Print the first field
                   hed=$1;                                          # Set the variable hed to the first field
                   hed1=substr($1,(length($1)-1));                  # Set a counter variable hed1 to the last digit in hed ($1)
                   next; 
                 } 
           !/^$/ {
                   print $2" "$3                                     # Where there is no blank line, print the second field, a space and the third field
                 } 
            /^$/ { 
                   print "--------------------------------------------------";     # Where there is a blank field, process
                   printf "%s%s\n",substr(hed,1,((length(hed))-(length(hed1)+1))),++hed1                                                # print the header extract before the counter, followed by the incremented counter
                  }' testfile

您可以尝试此
gnu awk
命令:

awk-M'BEGIN{FS=OFS=“|”}NR==1{hdr=$1;next}NF>2{print++hdr;print$2,$3;print“-------------”}文件
20210126000880000004
20210126000000000000000000002207|1220210126080109
-------------------
20210126000880000005
20210126000000000000000000002208|1220210126080110
-------------------
20210126000880000006
20210126000000000000000000002209|1220210126080111
-------------------
更具可读性的版本:

awk-M'开始{
FS=OFS=“|”
}
NR==1{
hdr=1美元
下一个
}
NF>2{
print++hdr
打印$2,$3
打印“----------------------”
}"档案"

下面是一个POSIX awk解决方案,它不需要
-M

awk'BEGIN{FS=OFS=“|”}NR==1{hdr=$1;next}NF>2{“echo”hdr+1 | bc”| getline hdr;print hdr;print$2,$3;print“-------------”}文件
20210126000880000004
20210126000000000000000000002207|1220210126080109
-------------------
20210126000880000005
20210126000000000000000000002208|1220210126080110
-------------------
20210126000880000006
20210126000000000000000000002209|1220210126080111
-------------------

我有差不多完全相同的解决方案,但奇怪的是在GNU awk 4.0.2上,计数头从2021012600088000003变为20210126000880001024,并且没有改变。你使用的是什么版本的awk?@RamanSailopal:我有
GNU awk 5.1.0
。确保按照answer.Ah中的建议使用
-M
开关-M在4.0.2上不可用。我刚刚检查过,这是为了“大数字”,这可能就是为什么它在旧版本上不起作用的原因。谢谢。@RamanSailopal供您参考。@anubhava I构建并升级awk5.1,但运行awk命令获取警告:-M忽略:MPFR/GMP支持未在中编译………您是说您想对输入文件多次调用awk,每次调用时显示第1行中的$1和第2行中的$2,但每次调用awk时其值都会增加1?还是你在说别的?请澄清你的问题。还告诉我们,在你感兴趣的每一个数字中间都有大约十二个代码< 0 > /代码> s(例如,它确实有2个不同的数字,其中有代码< 0)/代码> s填充在中间。但是这个解决方案即使在任何一个AWK上也不起作用。当
$1
2021012600088999999
时测试它,您将看到问题。