0

我试图在除了文件中的第一行和最后一行之外的每一行中插入一个字符串,但无法完成它,任何人都可以提供一些线索如何实现?提前致谢。

如何使用unix将一行中的最后一个逗号替换为字符串xxxxx(第一行和最后一行除外)

原始文件

00,SRI,BOM,FF,000004,20120808030100,20120907094412,"GTEXPR","SRIVIM","8894-7577","SRIVIM@GTEXPR."
10,SRI,FF,NMNN,3112,NMNSME,U,NM,GEB,,230900,02BLYPO
10,SRI,FF,NMNN,3112,NMNSME,U,NM,TCM,231040,231100,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,UPW,231240,231300,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,UFG,231700,231900,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,FTG,232140,232200,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,BOR,232340,232400,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,BAY,232640,232700,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,RWD,233400,,01
10,SRI,FF,BUN,0800,NMJWJB,U,NM,CCL,,101400,02CHLSU
10,SRI,FF,BUN,0800,NMJWJB,U,NM,PAR,101540,101700,01
10,SRI,FF,BUN,0800,NMJWJB,U,NM,MCE,101840,101900,01
10,SRI,FF,BUN,0800,NMJWJB,U,NM,SSS,102140,102200,09
10,SRI,FF,BUN,0800,NMJWJB,U,NM,FSS,102600,,01
10,SRI,FF,BUN,0802,NMJWJB,U,NM,CCL,,103700,01CHLSU
10,SRI,FF,BUN,0802,NMJWJB,U,NM,PAR,103940,104000,01
10,SRI,FF,BUN,0802,NMJWJB,U,NM,MCE,104140,104200,01
10,SRI,FF,BUN,0802,NMJWJB,U,NM,SSS,104440,104500,09
10,SRI,FF,BUN,0802,NMJWJB,U,NM,FSS,105000,,01
10,SRI,FF,BUN,3112,NMNSME,U,NM,GEB,,230900,02BLYSU
10,SRI,FF,BUN,3112,NMNSME,U,NM,TCM,231040,231100,01
10,SRI,FF,BUN,3112,NMNSME,U,NM,UPW,231240,231300,01
10,SRI,FF,BUN,3112,NMNSME,U,NM,UFG,231700,231900,01
10,SRI,FF,BUN,3112,NMNSME,U,NM,FTG,232140,232200,01
10,SRI,FF,BUN,3112,NMNSME,U,NM,BOR,232340,232400,01
10,SRI,FF,BUN,3112,NMNSME,U,NM,BAY,232640,232700,01
10,SRI,FF,BUN,3112,NMNSME,U,NM,RWD,233400,,01
99,SRI,FF,28

预期文件

00,SRI,BOM,FF,000004,20120808030100,20120907094412,"GTEXPR","SRIVIM","8894-7577","SRIVIM@GTEXPR."
10,SRI,FF,NMNN,3112,NMNSME,U,NM,GEB,,230900,xxxxx02BLYPO
10,SRI,FF,NMNN,3112,NMNSME,U,NM,TCM,231040,xxxxx231100,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,UPW,231240,xxxxx231300,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,UFG,231700,xxxxx231900,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,FTG,232140,xxxxx232200,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,BOR,232340,xxxxx232400,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,BAY,232640,xxxxx232700,01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,RWD,233400,,xxxxx01
10,SRI,FF,BUN,0800,NMJWJB,U,NM,CCL,,101400,xxxxx02CHLSU
10,SRI,FF,BUN,0800,NMJWJB,U,NM,PAR,101540,101700,xxxxx01
10,SRI,FF,BUN,0800,NMJWJB,U,NM,MCE,101840,101900,xxxxx01
10,SRI,FF,BUN,0800,NMJWJB,U,NM,SSS,102140,102200,xxxxx09
10,SRI,FF,BUN,0800,NMJWJB,U,NM,FSS,102600,,xxxxx01
10,SRI,FF,BUN,0802,NMJWJB,U,NM,CCL,,103700,xxxxx01CHLSU
10,SRI,FF,BUN,0802,NMJWJB,U,NM,PAR,103940,104000,xxxxx01
10,SRI,FF,BUN,0802,NMJWJB,U,NM,MCE,104140,104200,xxxxx01
10,SRI,FF,BUN,0802,NMJWJB,U,NM,SSS,104440,104500,xxxxx09
10,SRI,FF,BUN,0802,NMJWJB,U,NM,FSS,105000,,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,GEB,,230900,xxxxx02BLYSU
10,SRI,FF,BUN,3112,NMNSME,U,NM,TCM,231040,231100,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,UPW,231240,231300,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,UFG,231700,231900,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,FTG,232140,232200,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,BOR,232340,232400,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,BAY,232640,232700,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,RWD,233400,,xxxxx01
99,SRI,FF,28
4

1 回答 1

1

awk对于操作像这样的数据文件非常有用。这是一个或多或少你想要的单线。它将字符串“xxxxx”添加到每个至少有十二个字段的输入行的第十二个字段。

$ awk 'BEGIN{FS=OFS=","}NF>11{$12="xxxxx"$12}{print}' 16006747.txt 
00,SRI,BOM,FF,000004,20120808030100,20120907094412,"GTEXPR","SRIVIM","8894-7577","SRIVIM@GTEXPR."
10,SRI,FF,NMNN,3112,NMNSME,U,NM,GEB,,230900,xxxxx02BLYPO
10,SRI,FF,NMNN,3112,NMNSME,U,NM,TCM,231040,231100,xxxxx01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,UPW,231240,231300,xxxxx01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,UFG,231700,231900,xxxxx01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,FTG,232140,232200,xxxxx01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,BOR,232340,232400,xxxxx01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,BAY,232640,232700,xxxxx01
10,SRI,FF,NMNN,3112,NMNSME,U,NM,RWD,233400,,xxxxx01
10,SRI,FF,BUN,0800,NMJWJB,U,NM,CCL,,101400,xxxxx02CHLSU
10,SRI,FF,BUN,0800,NMJWJB,U,NM,PAR,101540,101700,xxxxx01
10,SRI,FF,BUN,0800,NMJWJB,U,NM,MCE,101840,101900,xxxxx01
10,SRI,FF,BUN,0800,NMJWJB,U,NM,SSS,102140,102200,xxxxx09
10,SRI,FF,BUN,0800,NMJWJB,U,NM,FSS,102600,,xxxxx01
10,SRI,FF,BUN,0802,NMJWJB,U,NM,CCL,,103700,xxxxx01CHLSU
10,SRI,FF,BUN,0802,NMJWJB,U,NM,PAR,103940,104000,xxxxx01
10,SRI,FF,BUN,0802,NMJWJB,U,NM,MCE,104140,104200,xxxxx01
10,SRI,FF,BUN,0802,NMJWJB,U,NM,SSS,104440,104500,xxxxx09
10,SRI,FF,BUN,0802,NMJWJB,U,NM,FSS,105000,,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,GEB,,230900,xxxxx02BLYSU
10,SRI,FF,BUN,3112,NMNSME,U,NM,TCM,231040,231100,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,UPW,231240,231300,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,UFG,231700,231900,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,FTG,232140,232200,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,BOR,232340,232400,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,BAY,232640,232700,xxxxx01
10,SRI,FF,BUN,3112,NMNSME,U,NM,RWD,233400,,xxxxx01
99,SRI,FF,28
于 2013-04-15T02:49:57.093 回答