linuxfunctionawkreplacesubstring

Replace last 4 digits by adding 1 digit (excluding 0x)


I have the following file (test.txt) which I want to change the logic as following:

0 becomes 1
1 becomes 2 
2 becomes 3 
3 becomes 4
4 becomes 5
5 becomes 6 
6 becomes 7 
7 becomes 8 
8 becomes 9
9 becomes 0

$ cat test.txt
69|1074330570|1,sip:+121345633210x3Bverstat=TN-Validation-Passed|tel:+12134565534|0
69|1077822111|2,;tel:+12223120011~sip:[email protected];|sip:[email protected]|0

I want to change the last 4 digits but when x is next to 0x, then 0 does not count. For Example: In The First Line '+121345633210x', last 4 are 3321. In The Second Line, '+12223120011', last 4 are 0011 and '+12223120051', last 4 are 0051 The output should look as following:

69|1074330570|1,sip:+121345644320x3Bverstat=TN-Validation-Passed|tel:+12134566645|0
69|1077822111|2,;tel:+12223121122~sip:[email protected];|sip:[email protected]|0

It needs to exclude '+1' and then count '10' digits in which I want to replace last 4 in the third and fourth columns.

121345633210 becomes 121345644320 / 12134565534 becomes 12134566645
12223120011  becomes 12223121122  / 12223120051 becomes 12223121162 / 13123120022 becomes 13123121133

I used the below logic when the column is just a number. But in this case, it has other stuff so below logic is not working but is correct to convert numbers by adding 1 digit.

awk -F"|" -v OFS="|" '
NR>0 {                                                      
    for (i=3;i<=4;i++) {                                   
        str = substr($i, 1, length($i) - 4)                 
        for (j = length($i) - 3; j <= length($i); j++) {    
            str = str (substr($i, j, 1) + 1) % 10           
        }
        $i = str                                            
    }
} 
1'

Solution

  • Assumptions:

    • a + only shows up in the 3rd/4th fields and then only at the front of a phone number

    One awk approach:

    awk '
    BEGIN { FS=OFS="+" }                               # split input on "+"
          { for (i=2; i<=NF; i++) {                    # loop through fields that start with a phone number
                oldfld = $i                            # save current field
                $i = ""                                # initialize new field
                d1 = substr(oldfld,1,1)                # get 1st digit
                len = (d1 == 1 ? 7 : 6)                # determine length of phone prefix
                $i = substr(oldfld,1,len)              # save everything up to the phone prefix
    
                for (j=(len+1); j<=(len+4); j++) {     # loop through last 4 digits of phone number
                    x = substr(oldfld,j,1)             # get current digit
                    $i = $i (x==9 ? 0 : x+1)           # increment and add to new field
                }
    
                $i = $i substr(oldfld,len+4+1)         # save rest of old field
             }
          }
    1                                                  # print current line
    ' test.txt
    

    This generates:

    69|1074330570|1,sip:+121345644320x3Bverstat=TN-Validation-Passed|tel:+12134566645|0
    69|1077822111|2,;tel:+12223121122~sip:[email protected];|sip:[email protected]|0