免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5041 | 回复: 13

[已解决,感谢。]请教awk拼接文件的问题 [复制链接]

论坛徽章:
0
发表于 2010-05-10 14:00 |显示全部楼层
本帖最后由 presidentccj 于 2010-05-11 09:23 编辑

[root@ccj o]# more input_pls_w1_O1.csv
"","O1"
"Intercept",-30.51650990
"PP1",0.00319170
"PP2",-2.47842822
"PP3",1.57993810
"PP4",-0.05710640
"PP5",-0.02903434
"PP6",-0.36125662
"PP7",0.01962486
"PP8",-0.31542687
"PP9",0.16647481
"PP10",0.00237926
"PP11",1.80586398
"PP12",-1.41433261
"PP13",-0.18511727
"PP14",2.62974369
"PP15",1.88932998
"PP16",-0.47941766
"PP17",-0.47578103
"PP18",-0.57763351
"PP19",-0.56852826
"PP20",-0.35175435
"PP21",-0.10231441

[root@ccj o]# more input_pls_w1_O2.csv
"","O2"
"Intercept",54.28675643
"PP1",0.00103916
"PP2",-0.96187289
"PP3",-0.14169508
"PP4",-0.01728666
"PP5",-0.02639999
"PP6",0.03903553
"PP7",0.00553046
"PP8",0.01156455
"PP9",0.11150413
"PP10",-0.38960775
"PP11",0.17548350
"PP12",0.02029578
"PP13",-0.48698791
"PP14",0.12402927
"PP15",0.21574689
"PP16",-0.09860723
"PP17",0.15232612
"PP18",0.27399351
"PP19",-0.02459994
"PP20",0.38002676
"PP21",0.22541298

一共有这样的文件18个
怎么能把每个文件的第二个域按O1,O2,...,O18放到一个文件中
找到以前的awk 'NR==FNR{a[NR]=$0}NR>FNR{print a[FNR]" "$0}'  
[root@ccj o]# awk 'NR==FNR{a[NR]=$0}NR>FNR{print a[FNR]" "$0}' input_pls_w1_O1.csv  input_pls_w1_O2.csv
"","O2"
"Intercept",54.28675643
"PP1",0.00103916
"PP2",-0.96187289
"PP3",-0.14169508
"PP4",-0.01728666
"PP5",-0.02639999
"PP6",0.03903553
"PP7",0.00553046
"PP8",0.01156455
"PP9",0.11150413
"PP10",-0.38960775
"PP11",0.17548350
"PP12",0.02029578
"PP13",-0.48698791
"PP14",0.12402927
"PP15",0.21574689
"PP16",-0.09860723
"PP17",0.15232612
"PP18",0.27399351
"PP19",-0.02459994
"PP20",0.38002676
"PP21",0.22541298

结果却不太对

论坛徽章:
0
发表于 2010-05-10 14:24 |显示全部楼层
用paste
或者lz把print a[FNR]" "$0改成print a[FNR],$0
试试

论坛徽章:
0
发表于 2010-05-10 14:39 |显示全部楼层
回复 2# lkk2003rty


   很奇怪,结果都不对
[root@ccj o]# paste -d,   input_pls_w1_O1.csv  input_pls_w1_O2.csv
,"","O2"
,"Intercept",54.28675643
,"PP1",0.00103916
,"PP2",-0.96187289
,"PP3",-0.14169508
,"PP4",-0.01728666
,"PP5",-0.02639999
,"PP6",0.03903553
,"PP7",0.00553046
,"PP8",0.01156455
,"PP9",0.11150413
,"PP10",-0.38960775
,"PP11",0.17548350
,"PP12",0.02029578
,"PP13",-0.48698791
,"PP14",0.12402927
,"PP15",0.21574689
,"PP16",-0.09860723
,"PP17",0.15232612
,"PP18",0.27399351
,"PP19",-0.02459994
,"PP20",0.38002676
,"PP21",0.22541298
,

论坛徽章:
0
发表于 2010-05-10 14:49 |显示全部楼层
回复 3# presidentccj


    paste -d "," file1 file2
lz是想要这样的?

论坛徽章:
0
发表于 2010-05-10 15:06 |显示全部楼层

  1. awk 'BEGIN{FS=OFS=",";N=0}
  2. > FNR==1{a[++N]=$2}
  3. > FNR>1{b[a[N] "_" $1]=$2;c[FNR-1]=$1}
  4. > END{
  5. > $1="\"\"";for(i=1;i<=N;i++)$(i+1)=a[i];print
  6. > for(j=1;j<FNR;j++){$1=c[j];for(i=1;i<=N;i++)$(i+1)=b[a[i] "_" c[j]];print}
  7. > }'  input_pls_w1_O1.csv  input_pls_w1_O2.csv .....

复制代码

论坛徽章:
0
发表于 2010-05-10 15:22 |显示全部楼层
回复 4# lkk2003rty


    命令按理是这样,但结果不是

想要:

"","O1","O2","O3","O4","O5","O6","O7","O8","O9","O10","O11","O12","O13","O14","O15","O16","O17","O18"
3,64.329584730953,91.732373722583,88.640810418112,77.02558291445,66.984414495698,66.77021028031,84.556779557446,90.366800694822,70.9
68649692953,43.603810730728,42.303986394296,48.179211836195,55.047851684394,62.464292380522,51.430152660268,48.999667338449,28.66650
3753091,19.998027865548
7,57.700311598862,93.92473687999,90.603673507885,76.786660835575,64.043630710995,58.2396107826119,87.605068594385,92.597751696182,72
.141142991134,42.328467922346,40.437683955083,47.968898795898,64.760450333821,72.459625523118,52.447911991396,50.475889937976,34.437
267663801,23.65749486675
11,56.924899980845,89.073856716503,86.913275919037,70.629997402075,56.115216641853,53.660623942762,81.057874959491,89.755989228074,6
4.892629174317,35.997958686644,35.212325144459,41.130597921253,47.070710534495,56.4288204174,50.236274981594,52.504633294165,26.7801
18886062,20.196498090298
15,65.820255652833,89.879408461643,85.755752035742,66.5701855649,59.180804398568,56.52124486441,75.902493919316,86.112885318882,62.4
04408483453,40.375854620928,39.291890663786,43.691097590095,33.520298972364,41.394365988722,48.223232695788,48.179886857389,19.95902
5996841,14.361061932648
19,55.619670233258,87.997431578466,86.607111013139,72.301958643515,55.987519800371,57.170756882602,82.288479969167,90.377905928836,6
5.03541035328,34.008386681172,33.408067254705,39.167965577938,48.613401794419,58.479909009542,50.898481588292,52.960578081124,27.401
100053723,20.846290466866
23,64.859373768279,93.668734354949,90.614398962506,80.71434348352,70.714411775254,63.90429296372,87.114107239348,92.077948608606,76.
273860169809,48.869670566504,47.653499193138,54.939586125355,65.720748261812,72.402939763606,51.845231968494,51.032020239497,32.7421
13634223,23.329499019924
27,56.387738243381,89.987891044999,88.559155427911,76.340333977725,60.267424043049,56.530171780192,85.248009358093,92.092014413038,7
0.240960987703,38.93400883754,38.291065591141,45.285044431183,59.267317336333,68.352371409524,51.57565982486,54.222017238003,31.4865
46716944,23.846054035854
31,38.821188211056,83.969448626476,83.533006167449,56.509636564965,31.783869247181,26.34996731219,74.793703911057,88.915658037064,51
.001029893766,17.605361899696,17.449955979907,23.51659924954,31.331620327393,45.011721644054,48.383384656876,58.837397594378,24.0896
51176387,20.737089761186
35,58.80273848658,91.040855382104,90.277556992016,80.36373058947,64.289134360684,48.601183531716,85.944707138848,93.490330535122,77.
158495127156,47.316191399222,47.612195347992,56.385923146114,69.4323315314,77.7448268918,50.780170484982,59.46524570587,34.367267041
456,28.091039842424
39,60.463794728475,88.407308044945,86.683733864215,69.924319671495,56.463039613765,43.88543316058,77.450743836825,88.85946555018,67.
024889909865,41.70233438655,42.059590895645,48.597775696355,45.068694001525,54.07392119714,48.01045336649,56.932530843715,24.5320618
0909,21.06535558999

论坛徽章:
0
发表于 2010-05-10 15:26 |显示全部楼层
回复 5# springwind426


    [root@ccj o]# awk 'BEGIN{FS=OFS=",";N=0}
>
>  FNR==1{a[++N]=$2}
>
>  FNR>1{b[a[N] "_" $1]=$2;c[FNR-1]=$1}
>
>  END{
>
> $1="\"\"";for(i=1;i<=N;i++)$(i+1)=a;print
>
>  for(j=1;j<FNR;j++){$1=c[j];for(i=1;i<=N;i++)$(i+1)=b[a "_" c[j]];print}
>
>  }'  input_pls_w1_O1.csv  input_pls_w1_O2.csv
,"O2"1"
,54.28675643-30.51650990
,0.0010391619170
,-0.9618728942822
,-0.141695083810
,-0.0172866610640
,-0.0263999903434
,0.03903553125662
,0.0055304662486
,0.01156455542687
,0.1115041347481
,-0.3896077537926
,0.17548350586398
,0.020295781433261
,-0.48698791511727
,0.12402927974369
,0.21574689932998
,-0.09860723941766
,0.152326127578103
,0.273993517763351
,-0.02459994852826
,0.380026765175435
,0.225412980231441
,,

论坛徽章:
0
发表于 2010-05-10 15:29 |显示全部楼层
回复 7# presidentccj

我运行四楼的结果是这样的:
  1. "","O1","O2"
  2. "Intercept",-30.51650990,54.28675643
  3. "PP1",0.00319170,0.00103916
  4. "PP2",-2.47842822,-0.96187289
  5. "PP3",1.57993810,-0.14169508
  6. "PP4",-0.05710640,-0.01728666
  7. "PP5",-0.02903434,-0.02639999
  8. "PP6",-0.36125662,0.03903553
  9. "PP7",0.01962486,0.00553046
  10. "PP8",-0.31542687,0.01156455
  11. "PP9",0.16647481,0.11150413
  12. "PP10",0.00237926,-0.38960775
  13. "PP11",1.80586398,0.17548350
  14. "PP12",-1.41433261,0.02029578
  15. "PP13",-0.18511727,-0.48698791
  16. "PP14",2.62974369,0.12402927
  17. "PP15",1.88932998,0.21574689
  18. "PP16",-0.47941766,-0.09860723
  19. "PP17",-0.47578103,0.15232612
  20. "PP18",-0.57763351,0.27399351
  21. "PP19",-0.56852826,-0.02459994
  22. "PP20",-0.35175435,0.38002676
  23. "PP21",-0.10231441,0.22541298
复制代码

求职 : 技术支持/维
论坛徽章:
0
发表于 2010-05-10 15:34 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
发表于 2010-05-10 15:37 |显示全部楼层
回复 6# presidentccj
  1. awk -F "," 'NR==FNR{a[FNR]=$0}NR>FNR{a[FNR]=a[FNR]","$2}END{for(i=1;i in a;i++) print a[i]}' file1 file2 file3 ....
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP