一口气执行多个 awk 程序

Multiple awk programs in one go

数据文件说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 = [=13=];
    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\}\),/,/' inputfile

为了附加 _ 我们总是可以使用:

{print  "_" }

所以我的问题是:有没有其他方法可以一次通过管道传输所有这些内容?或者任何其他方式。但请仅使用 awk 和 sed 来限制答案。

您发布的预期输出与您发布的输入不匹配,也不符合您声明的要求,所以这只是一个猜测,但也许这就是您想要的:

$ 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(,/[[:digit:]]/)
     = substr(,1,RSTART-1) sprintf("%08d",substr(,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

如果不是,请修复您想要的输出以澄清。