2

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"
Cœur
  • 37,241
  • 25
  • 195
  • 267
Senthil
  • 83
  • 2
  • 11

1 Answers1

1

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"
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • If i need to hijack few column in the input file with the same logic how that works i.e Here is my input file in the but no change in the output file "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 Also I can't find the tick mark to answer as correct – Senthil Apr 07 '14 at 18:54
  • Sorry i am new user .I have edited the input and the output file .I will improve better in my next question – Senthil Apr 07 '14 at 20:55