如果列中的模式匹配,则提取标题 [英] extract header if pattern in a column matches

查看:85
本文介绍了如果列中的模式匹配,则提取标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果该特定列中的模式匹配,我正在尝试提取并打印文件的标题。

I am trying to extract and print header of a file if the pattern in that particular column matches.

这里是一个示例:

[user ~]$ cal |sed 's/July 2014//'  
Su Mo Tu We Th Fr Sa  
       1  2  3  4  5  
 6  7  8  9 10 11 12  
13 14 15 16 17 18 19  
20 21 22 23 24 25 26  
27 28 29 30 31  

预期的输出:

如果输入日期= 31,则在31日打印日期。

if input date =31 then print the day on 31st.

请注意,我不能使用date -d标志,因为它不受操作系统支持。可能需要在这里用awk破解问题。

Just to be clear, I cannot use date -d flag as its not supported by my OS.Probably would need awk here to crack the question.

[user ~]$ date -d 20140731 +%A  
Thursday  

我希望我能够清楚地表达我的问题和关注。

I hope I am able to convey my question and concern clearly.

推荐答案

这是 gnu awk 解决方案:

cal | awk -v date=31 -v FIELDWIDTHS="3 3 3 3 3 3 3 3" 'NR==2 {split($0,a)} {for (i=1;i<=NF;i++) if ($i==date) print a[i]}'
Th

您设置了日期您希望将其显示为变量,因此可以将其更改为所需的内容。

You set the date that you like to be displayed as a variable, so it can be change to what you like.

可以这样写:

cal | awk 'NR==2 {split($0,a)} {for (i=1;i<=NF;i++) if ($i==date) print a[i]}' FIELDWIDTHS="3 3 3 3 3 3 3 3" date=31

PS FIELDWIDTH 是在 gnu awk 2.31

这篇关于如果列中的模式匹配,则提取标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆