Question

I have the nawk command where I need to format the data based on the length .All the time I need to keep first 6 digit and last 4 digit and make xxxx in the middle. Can you help in fine tuning the below script

#!/bin/bash 
FILES=/export/home/input.txt

           cat $FILES | nawk -F '|' '{
           if (length($3) >= 13  )
             print $1 "|" $2 "|" substr($3,1,6) "xxxxxx" substr($3,13,4) "|" $4"|" $5 
           else 
             print $1 "|" $2 "|" $3 "|" $4 "|" $5"|
           }' > output.txt 
done

input.txt

"2"|"X"|"A"|"ST"|"245552544555201"|"1111-11-11"|75.00 
"6"|"Y"|"D"|"VT"|"245652544555200"|"1111-11-11"|95.00 
"5"|"X"|"G"|"ST"|"3445625445552023"|"1111-11-11"|75.00 
"3"|"Y"|"S"|"VT"|"24532254455524"|"1111-11-11"|95.00

output.txt

"X"|"ST"|"245552544555201"|"245552xxxxx5201"
"Y"|"VT"|"245652544555200"|"245652xxxxx5200"
"X"|"ST"|"3445625445552023"|"344562xxxxxx2023"
"Y"|"VT"|"24532254455524"|"245322xxxx5524"
Was it helpful?

Solution

Try this:

$ awk '
BEGIN {FS = OFS = "|"}
length($5)>=13 {
    fld5=$5
    start = substr($5,1,7)
    end = substr($5,length($5)-4)
    gsub(/./,"x",fld5)
    sub(/^......./,start,fld5)
    sub(/.....$/,end,fld5)
    $1=$2; $2=$4; $3=$5; $4=fld5; NF-=3;       
}1' file
"X"|"ST"|"245552544555201"|"245552xxxxx5201"
"Y"|"VT"|"245652544555200"|"245652xxxxx5200"
"X"|"ST"|"3445625445552023"|"344562xxxxxx2023"
"Y"|"VT"|"24532254455524"|"245322xxxx5524"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top