gpt4 book ai didi

AWK:如何编号自动递增?

转载 作者:行者123 更新时间:2023-12-04 08:09:14 25 4
gpt4 key购买 nike

我有一个 file.file 内容是:

20210126000880000003|3|33.00|20210126|15:30
1|20210126000000000000000000002207|1220210126080109|1000|100000000000000319|100058110000000325|402041000012|402041000012|PT07|621067000000123645|收款方户名|2021-01-26|2021-01-26|10.00|TN|NCS|12|875466

2|20210126000000000000000000002208|1220210126080110|1000|100000000000000319|100058110000000325|402041000012|402041000012|PT06|621067000000123645|收款方户名|2021-01-26|2021-01-26|20.00|TN|NCS|12|875466

3|20210126000000000000000000002209|1220210126080111|1000|100000000000000319|100058110000000325|402041000012|402041000012|PT08|621067000000123645|收款方户名|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
如何自动增加?
谢谢!

最佳答案

你可以试试这个gnu awk命令:

awk -M 'BEGIN {FS=OFS="|"} NR == 1 {hdr = $1; next} NF>2 {print ++hdr; print $2, $3; print "-------------------"}' file

20210126000880000004
20210126000000000000000000002207|1220210126080109
-------------------
20210126000880000005
20210126000000000000000000002208|1220210126080110
-------------------
20210126000880000006
20210126000000000000000000002209|1220210126080111
-------------------
一个更易读的版本:
awk -M 'BEGIN {
FS=OFS="|"
}
NR == 1 {
hdr = $1
next
}
NF > 2 {
print ++hdr
print $2, $3
print "-------------------"
}' file

这是一个不需要的 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 "-------------------"}' file

20210126000880000004
20210126000000000000000000002207|1220210126080109
-------------------
20210126000880000005
20210126000000000000000000002208|1220210126080110
-------------------
20210126000880000006
20210126000000000000000000002209|1220210126080111
-------------------

关于AWK:如何编号自动递增?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66065678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com