encrytion/substitution issue in a file

1) ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547425069636596::6:N:mrs charles:N:PH:00010031:0001'

OUTPUT - ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547******636596::6:N:mrs charles:N:PH:00010031:0001'

The requirement here was to encrypt the 15th field appearing in the DEF section if the length is greater than 15

so 4547425069636596 was replaced by 4547******636596

awk -F":" '
BEGIN {OFS=":"}
$0 ~/\+DEF/ {
for(i=1;i<=NF;i++)
{
if($i=="+DEF")
{
if( $(i+12) ~ /[^0-9]/ )
{
next;
}
else
{
if(length($(i+12))>=15)
{
$(i+12)=substr($(12+i),1,4)"******"substr($(12+i),11,16)
}
}
}
}
}1' file_name.txt

the above code in working if have only one DEF appearing in the line, but it doesn't work if there are multiple DEF in a line it just replaces only the
first occurence of the DEF in the line and not all the occurences of DEF in the line

2) ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547425069636596::6:N:mrs charles:N:PH:00010031:0001+DEF:BAL:1:N::::7:450.00:0.
00:2009-04-29:XT:5286838183351672::7:N:britton:N:PH:00010031:0001+DEF:FPT:3:N::::7:10.13:0.00:2009-04-29:XT:5286838183351672::7:N:britton:N:MS:00616121:2601'

current output that i get

ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547******636596::6:N:mrs charles:N:PH:00010031:0001+DEF:BAL:1:N::::7:450.00:0.00:2009-04-29:XT:5286838183351672::7:N:britton:N:PH:00010031:0001+DEF:FPT:3:N::::7:10.13:0.00:2009-04-29:XT:5286838183351672::7:N:britton:N:MS:00616121:2601'

and i require

ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547636596::6:N:mrs charles:N:PH:00010031:0001+DEF:BAL:1:N::::7:450.00:0.00:2009-04-29:XT:5286******351672::7:N:britton:N:PH:00010031:0001+DEF:FPT:3:N::::7:10.13:0.00:2009-04-29:XT:5286351672::7:N:britton:N:MS:00616121:2601'

Please Advice

To keep the forums high quality for all users, please take the time to format your posts correctly.

  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags and by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote

# awk -F: '/\+DEF/{for(i=0;++i<NF;){if($i=="+DEF"&&length($(i+12))>=15){sub(substr($(12+i),5,6),"******",$(12+i))}}}1' OFS=":" file_name.txt
ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547******636596::6:N:mrs:charles:N:PH:00010031:0001:+DEF:BAL:1:N::::7:450.00:0.00:2009-04-29:XT:5286******351672::7:N:britton:N:PH:00010031:0001:+DEF:FPT:3:N::::7:10.13:0.00:2009-04-29:XT:5286******351672::7:N:britton:N:MS:00616121:2601'

... how you want to solve the problem when you have mistakes in the sample file :cool:

hi Danmero,

I will make sure for the post to be in proper format going fwd.

The tried with the updated script but i am still getting only the
first occurence as replaced and not all

Please advice

---------- Post updated at 07:08 AM ---------- Previous update was at 06:41 AM ----------

The problem is :0001+DEF: is been treated a whole field and +DEF is not recognised

Yep , please edit your first post and add [code] tags :wink:

)

 
ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547425069636596::6:N:mrs charles:N:PH:00010031:0001'
 
OUTPUT - ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547******636596::6:N:mrs charles:N:PH:00010031:0001'
 

The requirement here was to encrypt the 15th field appearing in the DEF section if the length is greater than 15

so 4547425069636596 was replaced by 4547******636596

 
awk -F":" '
BEGIN {OFS=":"}
$0 ~/\+DEF/ {
for(i=1;i<=NF;i++)
{
if($i=="+DEF")
{
if( $(i+12) ~ /[^0-9]/ )
{
next;
}
else
{
if(length($(i+12))>=15)
{
$(i+12)=substr($(12+i),1,4)"******"substr($(12+i),11,16)
}
}
}
}
}1' file_name.txt

the above code in working if have only one DEF appearing in the line, but it doesn't work if there are multiple DEF in a line it just replaces only the
first occurence of the DEF in the line and not all the occurences of DEF in the line

 
2) ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547425069636596::6:N:mrs charles:N:PH:00010031:0001+DEF:BAL:1:N::::7:450.00:0.
00:2009-04-29:XT:5286838183351672::7:N:britton:N:PH:00010031:0001+DEF:FPT:3:N::::7:10.13:0.00:2009-04-29:XT:5286838183351672::7:N:britton:N:MS:00616121:2601'
 
current output that i get
 
ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547******636596::6:N:mrs charles:N:PH:00010031:0001+DEF:BAL:1:N::::7:450.00:0.00:2009-04-29:XT:5286838183351672::7:N:britton:N:PH:00010031:0001+DEF:FPT:3:N::::7:10.13:0.00:2009-04-29:XT:5286838183351672::7:N:britton:N:MS:00616121:2601'

and i require

 
ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547******636596::6:N:mrs charles:N:PH:00010031:0001+DEF:BAL:1:N::::7:450.00:0.00:2009-04-29:XT:5286******351672::7:N:britton:N:PH:00010031:0001+DEF:FPT:3:N::::7:10.13:0.00:2009-04-29:XT:5286******351672::7:N:britton:N:MS:00616121:2601'
 

Please Advice

Let's try this one :wink:

awk -F: '/\+DEF/{for(i=0;++i<NF;){if($i~/\+DEF/&&length($(i+12))>=15){sub(substr($(12+i),5,6),"******",$(12+i))}}}1' OFS=":" file