说数据文件sampleTest1.dat
Type,^*p,Pl@nt_!d,C^*mp@nyC^*de,S@P_Pr^*f!t_Center,USF_BrC^*de,D!v_Nbr,N@meSh^*rt,N@mePr^*per,@ddress1,@ddress2,_C!ty,Reg!^*n,P^*st@lC^*de,C^*untry,Ph^*ne,@C_Type,Sh@red_Fl@g_@C
@C,1,1220,1410,US0001,,,J@CKS^*N,"_ J@CKS^*N, LLC",44 M!LW@UKEE STREET,,J@CKS^*N,MS,320683,US@,60135411,B,Y
@C,3,1225,1400,US0003,,,J@CKS^*NV!LLE,"_ J@CKS^*NV!LLE, !NC.",15 LEW!S !NDUSTR!@L R^*@D,,J@CKS^*NV!LLE,FL,32540000,US@,,B,Y
@C,4,1095,1400,US0004,,,CENTR@@#L C@L!F^*RN!@,"_ CENTR@L C@L!F^*RN!@, !NC.",13 M@R!P^*S@ R^*@D,,M^*DEST^*,C@,95340000,US@,,B,Y
@C,5,1210,1400,US0005,,,!NTERM*UNT@!N,"_ !NTERM^*UNT@!N, !NC.",949 S^*UTH PR^*SPER!TY,,WEST J^*RD@N,UT,8408100,US@,,,N
@C,6,1150,1400,US0006,,,N^*RTH# &EX@S,"_ N^*RTH TEX@S, !NC.",80 TR!N!TY DR!VE,P.^*. B^*X 561000,LEW!SV!LLE,TX,7560000,US@,4646000,,N
现在,对于这些数据,您可以看到分隔符是','
现在我想做的是
因此输出数据看起来像前两行:
Type_P,Plntd,CmpnyCde,SP_Prft_Center,USF_BrCde,Dv_Nbr,NmeShrt,NmePrper,Ddress1,Ddress2,_Cty,Regn,PstCde,Cuntry,Phne,C_Type,Shred_Flg_C
C_1,1220,1410,US00000001,,,JCKSN,_JCKSN, LLC,44 MLWUKEE STREET,,JCKSN,MS,320683,US,60135411,B,Y
我几乎拥有所有程序:
删除特殊字符
sed 's/[!@#\$%^&*()]//g' filename
对于驼峰案例
{ text = $0;
split(text, words, /[^a-zA-Z]+/);
for (i=1; i<=length(words); i++) {
res = res toupper(substr(words[i],1,1)) tolower(substr(words[i],2));
}
print res
}
用于8填充
sed 's/[^,][^,]*/\n0000000&/2;s/\n[^,]*\(.\{8\}\),/\1,/' inputfile
要附加_
我们可以随时使用:
{print $1 "_" $2}
所以我的问题是:有没有其他方法可以一次性管理所有这些?或任何其他方式。但请用awk和sed限制答案。
答案 0 :(得分:2)
您发布的预期输出与您发布的输入不符,也不符合您的要求,所以这只是一个猜测,但也许这就是您想要的:
$ cat tst.awk
BEGIN { FS=OFS="," }
{
gsub(/[^[:alnum:][:space:]_,]/,"")
for (i=1;i<=NF;i++) {
split($i,a,/ /)
$i = ""
for (j=1;j in a;j++) {
$i = $i (j>1?" ":"") toupper(substr(a[j],1,1)) tolower(substr(a[j],2))
}
}
match($5,/[[:digit:]]/)
$5 = substr($5,1,RSTART-1) sprintf("%08d",substr($5,RSTART))
sub(/,/,"_")
print
}
$ awk -f tst.awk file
Type_P,Plnt_d,Cmpnycde,00000000,Usf_brcde,Dv_nbr,Nmeshrt,Nmeprper,Ddress1,Ddress2,_cty,Regn,Pstlcde,Cuntry,Phne,C_type,Shred_flg_c
C_1,1220,1410,Us00000001,,,Jcksn,_ Jcksn, Llc,44 Mlwukee Street,,Jcksn,Ms,320683,Us,60135411,B,Y
C_3,1225,1400,Us00000003,,,Jcksnvlle,_ Jcksnvlle, Nc,15 Lews Ndustrl Rd,,Jcksnvlle,Fl,32540000,Us,,B,Y
C_4,1095,1400,Us00000004,,,Centrl Clfrn,_ Centrl Clfrn, Nc,13 Mrps Rd,,Mdest,C,95340000,Us,,B,Y
C_5,1210,1400,Us00000005,,,Ntermuntn,_ Ntermuntn, Nc,949 Suth Prsperty,,West Jrdn,Ut,8408100,Us,,,N
C_6,1150,1400,Us00000006,,,Nrth Exs,_ Nrth Texs, Nc,80 Trnty Drve,P Bx 561000,Lewsvlle,Tx,7560000,Us,4646000,,N
如果没有,请修正所需的输出以澄清。