AWK: How to number auto-increment?

Viewed 166

I have a file.file content is:

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

I use awk command:

awk -F"|" 'NR==1{print $1};FNR==2{print $2,$3}'  testfile

Get the following result:

20210126000880000003
20210126000000000000000000002207 1220210126080109

I want the number to auto-increase:

awk -F"|" 'NR==1{print $1+1};FNR==2{print $2+1,$3+1}' testfile

But get follow result:

20210126000880001024
20210126000000000944237587726336 1220210126080110

have question:

I want to the numer is auto-increase: hope the result is:

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

How to auto_increase?

Thanks!

2 Answers

Anubhava has the best solution but for older versions of GNU awk that don't support -M (big numbers) you can try the following:

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

Explanation:

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

You may try this gnu awk command:

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
-------------------

A more readable version:

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

Here is a POSIX awk solution that doesn't need -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
-------------------
Related