我有以下文件(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:+12223120051@vzpps.com;|sip:+13123120022@vzpps.com|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:+12223121162@vzpps.com;|sip:+13123121133@vzpps.com|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'

推荐答案

结论:

  • +只出现在第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:+12223121162@vzpps.com;|sip:+13123121133@vzpps.com|0

Linux相关问答推荐

我想强调某些条件是否与Linux中的全部输出匹配

Ansible-删除两个注释之间的代码块(包括注释本身)

条件句if的正确写法是怎样的?

为什么waitpid(2)可以指定非子进程?

"‘operator<<’匹配失败(可能是因为我的C++/GCC版本问题)"

如何将一个变量的 2 行添加到另一个变量的特定行?

在不编写任何代码的情况下,是否有一个命令可以检索当前 shell 的亲和力中的可用内核数?

有没有办法确定什么代码使 linux 共享对象inflating ?

bind(): "无法分配请求的地址"

在 bash 中解析变量

Bash shift 改变了关联数组的期望值

是否可以使用 shell 脚本递归地创建文件夹?

从 .war 文件外部化 Tomcat webapp 配置

使用 Scp 时防止覆盖文件

为什么 JVM 报告的已提交内存比 linux 进程驻留集大小更多?

从linux命令行写入串口

Monit 守护程序 - 连接到 monit 守护程序时出错

为什么两次使用 grep 时没有显示输出?

如何让我的 Golang Web 服务器在后台运行?

仅当文件存在于 shell 脚本中时才移动