unix – 仅在匹配记录后打印匹配标题的字段
发布时间:2020-12-15 20:55:53 所属栏目:安全 来源:网络整理
导读:我正在尝试从我的文件中提取特定字段.基本上,输出字段仅包含匹配的表达式,输出在匹配的记录之后开始. 这是我输入的一个例子.有时字段的顺序不同,并且在我尝试匹配的标题之前有不同的行数. 我很难找到如何使用cut和sed命令实现这一点,并且找不到awk方法. CGAT
我正在尝试从我的文件中提取特定字段.基本上,输出字段仅包含匹配的表达式,输出在匹配的记录之后开始.
这是我输入的一个例子.有时字段的顺序不同,并且在我尝试匹配的标题之前有不同的行数. 我很难找到如何使用cut和sed命令实现这一点,并且找不到awk方法. CGATS.17 FORMAT_VERSION 1 KEYWORD "SampleID" KEYWORD "SAMPLE_NAME" NUMBER_OF_FIELDS 45 WEIGHTING_FUNCTION "ILLUMINANT,D50" WEIGHTING_FUNCTION "OBSERVER,2 degree" BEGIN_DATA_FORMAT SampleID SAMPLE_NAME CMYK_C CMYK_M CMYK_Y CMYK_K LAB_L LAB_A LAB_B nm380 nm390 nm400 END_DATA_FORMAT NUMBER_OF_SETS 182 BEGIN_DATA 1 1 40 40 40 0 62.5 6.98 4.09 0.195213 0.205916 0.212827 2 2 0 40 40 0 73.69 25.48 24.89 0.200109 0.211081 0.218222 3 3 40 40 0 0 63.95 12.14 -20.91 0.346069 0.365042 0.377148 4 4 0 70 70 0 58.91 47.69 35.54 0.080033 0.084421 0.087317 END_DATA 这是我使用的脏代码,它主要完成了工作,但没有字段标题条件搜索. awk命令只是删除输出周围的空行. cut -f 7-9 -s input.txt | sed -E 's/(LAB_.)//g' | awk 'NF' > file.txt 我期望的输出看起来像这样.它仍然以制表符分隔,仅包含直接从(LAB_.)开始的字段的值 62.5 6.98 4.09 73.69 25.48 24.89 63.95 12.14 -20.91 58.91 47.69 35.54 解决方法
脚本:
#!/usr/bin/awk -f # We look for line starting with BEGIN_DATA_FORMAT do the getline function and # store location of fields that have "LAB" in their name on the next line. /^BEGIN_DATA_FORMAT/{ getline for (i=1;i<=NF;i++) if ($i~/LAB/) a[i]=$i } # In this regex range we look for lines that have more than 2 fields. For those # lines we loop thru each field and see if the location matches to the ones # captured in our earlier array (i.e location number of fields that have "LAB" # in their name). If we find a match we print those fields. /^BEGIN_DATA$/,/^END_DATA$/{ s=""; if (NF<2) next; else for (j in a) s=s?s"t"$j:$j print s; } 测试: [jaypal:~/Temp] ./script.awk file 62.5 6.98 4.09 73.69 25.48 24.89 63.95 12.14 -20.91 58.91 47.69 35.54 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- shell – 在鱼启动时添加一个相对路径到$PATH
- angularjs – angular mock“module”导致'[
- 用于CORBA IDL的vim-ctags生成器?
- angularjs – 当typeahead-editable为false时,设
- scala – 找不到模块:com.eed3si9n #sbt-assemb
- Angular 2向上和向下滑动动画
- JavaSpringmvc操作Redis和Redis集群
- angularjs – 无法取消$interval返回的链式承诺
- angularjs – Angular ui.bootstrap分页 – 当前
- angularjs – 根据控制器中的变量显示或隐藏元素
热点阅读