shell - capture-specific-columns and mask the column -
i trying write script capture , mask specific column.i need have 4 column clear text , mask in output file .i not sure how mask same column
pls me in rewriting below command or new command
input.txt --------- aa | bb | cc | 123456 output.txt --------- bb | 123456 | 12xx56 script wrote
cat input.txt | nawk -f '|' '{print $2 "|" $4 "|" $4} >output.txt
nawk -f '|' '{print $2 "|" $4 "|" substr($4, 1,3) "xx" substr($4,6,2)}' input.txt > output.txt output
bb | 123456| 12xx56 assuming don't need leading , trailing spaces, make it
nawk -f '|' '{gsub(/ */, "", $0);print $2 "|" $4 "|" substr($4, 1,2) "xx" substr($4,5,2)}' input.txt > output.txt cat output.txt bb|123456|12xx56 final solution
echo "aa | bb | cc | 12345678" \ | awk -f '|' '{gsub(/ */, "", $0) #dbg print "length$4=" (length($4)-4) masking=sprintf("%"(length($4)-4)"s", " ") ; gsub(/ /, "x", masking) print $2 "|" $4 "|" substr($4, 1,2) masking substr($4,(length($4)-1),2) }' bb|12345678|12xxxx78 i using echo "..." simplfy testing process. can take out, replace input.txt > output.txt , end of line , work before.
i've added (length($4)-1) make position of 2nd last char on $4 dynamic, based on length of ever word in $4.
ihth
Comments
Post a Comment