linux 通过添加1位数字替换最后4位数字(不包括0x)

8dtrkrch  于 2023-11-17  发布在  Linux
关注(0)|答案(3)|浏览(131)

我有以下文件(test.txt),我想改变逻辑如下:

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

字符串
我想更改最后4位数,但当x与0x相邻时,0不计数。例如:在第一行'+121345633210x'中,最后4个是3321。在第二行'+12223120011'中,最后4个是0011和'+12223120051',最后4个是0051输出应该如下所示:

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


它需要排除'+1',然后计算'10'位数,其中我想替换第三和第四列中的最后4位。

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


当列只是一个数字时,我使用了下面的逻辑。但在这种情况下,它有其他的东西,所以下面的逻辑不起作用,但通过添加1位数来转换数字是正确的。

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'

yrefmtwq

yrefmtwq1#

假设:

  • 一个+只显示在第3/第4字段,然后只在前面的电话号码

一种awk方法:

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

字符串
这将产生:

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

cl25kdpy

cl25kdpy2#

使用gnu-awk,你可以这样做:

awk '
function repl(s,  i, r, ch) {
   for (i=1; i<=length(s); ++i) {
      ch = substr(s,i,1)
      r = r (ch == 9 ? 0 : ch+1)
   }
   return r
}
BEGIN {FS=OFS="+"}
{
   for (j=2; j<=NF; ++j) {
      if (match($j, /^([0-9]+)([0-9]{4})(0x|[^x0-9]|$)(.*)/, a))
         $j = a[1] repl(a[2]) a[3] a[4]
   }
} 1' file

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

字符串

sauutmhj

sauutmhj3#

使用GNU awk for the 3rd arg to match():

$ awk '{
    head = ""
    while ( match($0,/([^+]+\+[0-9]{7})([0-9]{4})/,a) ) {
        digs = ""
        for ( i=1; i<=4; i++ ) {
            digs = digs ((substr(a[2],i,1) + 1) % 10)
        }
        head = head a[1] digs
        $0 = substr($0,RSTART+RLENGTH)
    }
    print head $0
}' test.txt
69|1074330570|1,sip:+121345644320x3Bverstat=TN-Validation-Passed|tel:+12134566645|0
69|1077822111|2,;tel:+12223121122~sip:[email protected];|sip:[email protected]|0

字符串
这会产生预期的输出,而不需要测试第3个或第4个字段,也不需要测试0x,因为除了3或4之外,没有匹配目标regexp的字段中的字符串,尽管您说过忽略0后跟x,实际上,你只是一直在改变前11位数字,任何0后面跟着x就是第12位数字。
你可以使用任何POSIX awk来做同样的事情:

awk '{
    head = ""
    while ( match($0,/\+[0-9]{11}/) ) {
        digs = ""
        for ( i=1; i<=4; i++ ) {
            digs = digs ((substr($0,RSTART+RLENGTH-5+i,1) + 1) % 10)
        }
        head = head substr($0,1,RSTART+RLENGTH-5) digs
        $0 = substr($0,RSTART+RLENGTH)
    }
    print head $0
}' test.txt

相关问题