2

我有 nawk 命令,我需要根据长度格式化数据。一直以来,我都需要保留前 6 位和后 4 位,并在中间设置 xxxx。你能帮忙微调下面的脚本吗

#!/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

输入.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

输出.txt

"X"|"ST"|"245552544555201"|"245552xxxxx5201"
"Y"|"VT"|"245652544555200"|"245652xxxxx5200"
"X"|"ST"|"3445625445552023"|"344562xxxxxx2023"
"Y"|"VT"|"24532254455524"|"245322xxxx5524"
4

1 回答 1

1

试试这个:

$ 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"
于 2014-04-03T20:35:51.337 回答