linuxbashshellcsvmaskedinput

Masking delimited columns shellscript


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

Solution

  • 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.