Masking delimited columns shellscript

80 views Asked by At

I am trying to mask the 6th column in this delimited file. Currently my attempt masks the entire file. I was wondering what I may be doing incorrectly.

Current:

awk 'BEGIN{FS=OFS="^^"} {gsub(/./, "X", $1)} 6' $1

Input:

00000000001^^00023^^111112233^^C^^ ^^Iwanttomaskthis                   ^^ ^^               ^^U^^W^^ ^^   ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^Iwanttomaskthis                   ^^ ^^               ^^X^^W^^ ^^   ^^333^^9^^88

Expected:

00000000001^^00023^^111112233^^C^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^               ^^U^^W^^ ^^   ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^               ^^U^^W^^ ^^   ^^222^^6^^77
3

There are 3 answers

1
anubhava On BEST ANSWER

You can use this awk:

awk 'BEGIN{FS="\\^\\^"; OFS="^^"} {gsub(/./, "X", $6)} 1' file

00000000001^^00023^^111112233^^C^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^               ^^U^^W^^ ^^   ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^               ^^X^^W^^ ^^   ^^333^^9^^88

Here we need to escape ^ because ^ is a special regex meta-characters.

0
Claes Wikner On

Changes even the last few fields.

awk '{sub(/Iwanttomaskthis                   /,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")sub(/X..W/,"U^^W")sub(/..333..9..88/,"^^222^^6^^77")}1' file

00000000001^^00023^^111112233^^C^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^               ^^U^^W^^ ^^   ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^               ^^U^^W^^ ^^   ^^222^^6^^77
0
RavinderSingh13 On

Could you please try one more approach and let me know if this helps you.

awk '{match($0,/\^\^  +\^\^ /);while((RLENGTH)>1){val=val?val "X":"X";RLENGTH--};sub(/\^\^  +\^\^ /,val,$0);print;val=""}'   Input_file

Explanation:

awk '
{
 match($0,/\^\^  +\^\^ /);  ##Using match functionality of awk to match regex which will look from ^^ space and ^^ space.
 while((RLENGTH)>1){        ##Now starting a while loop which will run till the value of RLENGTH is NOT NULL, not here RSTART and RLENGTH are variables which will be set once a match is found by provided regex in match function of awk.
    val=val?val "X":"X";    ##creating a variable here and concatenating its value with stating X only each time it comes in loop.
    RLENGTH--               ##Decrement the value of RLENGTH each time it comes in while loop.
};
sub(/\^\^  +\^\^ /,"^^"val"^^ ",$0)##Substitute regex ^^ space(all spaces till) ^^ space with value of val
 print;                     ##printing the current line. It could edited/non-edited depending upon regex is found by match or not.
 val=""                     ##Nullifying the value of variable val here.
}
' file98997                 ##Mentioning the Input_file here.