Reputation: 235
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'
Upvotes: 2
Views: 145
Reputation: 204488
Using 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
That produces the expected output without testing for just the 3rd or 4th fields and without testing for 0x
because there are no strings in fields other than 3 or 4 that match the target regexp, and though you said to ignore 0
followed by x
, in reality you're simply always changing the first 11 digits and any 0
that's followed by x
is the 12th digit.
You could do the same using any POSIX awk with:
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
Upvotes: 3
Reputation: 35316
Assumptions:
+
only shows up in the 3rd/4th fields and then only at the front of a phone numberOne 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
Upvotes: 2
Reputation: 786021
Using gnu-awk
, you can do this:
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
Upvotes: 3