免费注册 查看新帖 |

Chinaunix

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

awk如何将带标题的表格数据格式化输出? [复制链接]

论坛徽章:
0
发表于 2011-11-02 00:53 |显示全部楼层
本帖最后由 cerius2 于 2011-11-03 22:25 编辑

示例的file:
============================================

  Fractional coordinates of asymmetric unit :

--------------------------------------------------------------------------------
   No.  Atomic       x           y          z         Charge      Occupancy
        Label      (Frac)      (Frac)     (Frac)        (e)         (Frac)  
--------------------------------------------------------------------------------
      1 C     c    0.913868    0.945745    0.500000      0.0000    1.000000   
      2 C     c    0.954887 *  0.158829 *  0.500000      0.0000    1.000000   
      3 C     c    0.955613 *  0.424480 *  0.500000      0.0000    1.000000   
      4 C     c    0.956641 *  0.689914 *  0.500000      0.0000    1.000000   
      5 C     c    0.999030 *  0.902254 *  0.500000      0.0000    1.000000   
--------------------------------------------------------------------------------
中间有n个---------------------------------------------------------------------
--------------------------------------------------------------------------------

  Final fractional coordinates of atoms :

--------------------------------------------------------------------------------
   No.  Atomic        x           y          z          Radius
        Label       (Frac)      (Frac)     (Frac)       (Angs)
--------------------------------------------------------------------------------
     1  C     c     0.913868    0.945745    0.500000    0.000000
     2  C     c     0.954931    0.159261    0.500000    0.000000
     3  C     c     0.955832    0.424808    0.500000    0.000000
     4  C     c     0.957040    0.690123    0.500000    0.000000
     5  C     c     0.999481    0.902896    0.500000    0.000000
--------------------------------------------------------------------------------
尾部还有n个------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

将  Fractional 和  Final fractional两部分的x,y,z那3列数据单独提取出来,并进行格式化输出:
   0.913868   0.945745   0.500000
    0.954887   0.158829   0.500000
    0.955613   0.424480   0.500000
    0.956641   0.689914   0.500000
    0.999030   0.902254   0.500000


谢谢了。

以前没读awk十三日,现在着急用,只好来求助了。


补充下要求吧:
找到指定行  Fractional coordinates和Final Fractional的位置;
向下6行,即越过2次------的区域到达我的数据区;
分列输出数据;直到遇到第3次--------行结束。

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
发表于 2011-11-02 01:00 |显示全部楼层
回复 1# cerius2
  1. awk '/^ +[0-9]/{gsub("*","");print $4,$5,$6}' OFS="\t" File
复制代码

论坛徽章:
0
发表于 2011-11-02 01:05 |显示全部楼层
忘了说了,Fractional coordinates of asymmetric unit :这一行后面的数据才是需要的
file里面此类数据有重复


需要判断一下Fractional coordinates of asymmetric unit :的位置及其后面的行号

论坛徽章:
0
发表于 2011-11-02 01:13 |显示全部楼层
$ awk '/  Fractional coordinates of asymmetric unit :/{for(i=5;i<=15;i++){getline;gsub("*","");print $4,$5,$6}}' OFS="\t" file
               
               
y        z        Charge
(Frac)        (e)        (Frac)
               
0.913868        0.945745        0.500000
0.954887        0.158829        0.500000
0.955613        0.424480        0.500000
0.956641        0.689914        0.500000
0.999030        0.902254        0.500000


修改了一下,貌似getline那里未处理正确,怎么带着部分表头出来了

论坛徽章:
0
发表于 2011-11-02 01:21 |显示全部楼层
很笨的法子,不过可以得到结果了

$ awk '/  Fractional coordinates of asymmetric unit :/{for(i=5;i<=15;i++){getline;gsub("*","");print $4,$5,$6}}' OFS="\t" file|tail -6|head -5
0.913868        0.945745        0.500000
0.954887        0.158829        0.500000
0.955613        0.424480        0.500000
0.956641        0.689914        0.500000
0.999030        0.902254        0.500000

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
发表于 2011-11-02 01:23 |显示全部楼层
本帖最后由 yinyuemi 于 2011-11-02 01:24 编辑

回复 4# cerius2
  1. awk '/Fractional coordinates of asymmetric unit :/{p=1}{p++}p<13&&p>4&&!/-/{gsub("*","");print /Label/?$2 OFS $3 OFS $4:(/Atomic/?$3 OFS $4 OFS $5:$4 OFS $5 OFS $6)}' OFS="\t"
复制代码

论坛徽章:
0
发表于 2011-11-02 09:31 |显示全部楼层
awk -vFS="[ \*]*" '$0 ~ /[0-9][0-9]* \<C\>/{print $5"\t"$6"\t"$7}' data

论坛徽章:
0
发表于 2011-11-02 15:24 |显示全部楼层
补充下要求吧:找到指定行  Fractional coordinates of asymmetric unit :;向下6行,即越过2次------的区域到达我的数据区;分列输出数据;直到遇到第3次--------行结束。


前面用tail的方式和p<13&&p>4的方式在正式的文本处理中不能用

论坛徽章:
0
发表于 2011-11-02 17:12 |显示全部楼层
$ awk '/Fractional coordinates of asymmetric unit :/{print NR}' file
4
$ awk '/--------/{print NR}' file
6
9
15

打印9-15行之间的数据

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
发表于 2011-11-02 23:50 |显示全部楼层
回复 9# cerius2
  1. awk '/Fractional coordinates of asymmetric unit :/{p=1}/------/{p++}p==3{gsub("*","");print $4,$5,$6}'
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP