AWK:有条件地抓取文件中的文本
我需要在文件中找到一个具有 id(例如 valueID1)且小于某个值的值。由此,我需要找到与 valueID1 关联但具有不同值 id 且位于不同行(例如 valueID2)的第一个值
假设我想在名为“birthday”的文件中找到一个 id,例如birthday = xx/xx/xxxx 我想找到特定日期以下的第一个生日(我必须使用 $3 来获取实际的数值)然后,我想获取接近该日期的第二个 id 的值首先,所以“名字”如“名字=格雷格”。我想在那里输出“Greg”。我只想要第一个结果,而不是所有小于第一个指定值的结果。
关于如何做到这一点有什么想法吗?这就是我能做的一切,但根本不起作用。
{
if((/valueID1/ $3) < 0.1) print /valueID2/ $3; else
/valueID2/
}
I need to find a value in a file that has an id (say valueID1) and is less than a certain value. From that, I need to find the first value that is associated with valueID1 but has a different value id and is on a different line (say valueID2)
Say I wanted to find an id in the file called "birthday", such as birthday = xx/xx/xxxx I'd want to find the first birthday below a specific date (I'd have to use $3 to grab the actual numeric value) Then, I would want to grab the value of the second id that is near the first, so "name" as in "name = Greg." I'd want to output 'Greg' there. I only want the first result, not all of the results less than the first specified value.
Any thoughts on how to do this? This is all I've been able to do, and it doesn't work at all.
{
if((/valueID1/ $3) < 0.1) print /valueID2/ $3; else
/valueID2/
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是我从你的描述中了解到的内容,你的数据可能看起来像这样:
在这种情况下,你想打印出“George”,因为他的生日小于 1999 年。如果这些假设是正确的,那么我是如何解决这个问题的:
讨论
更新
如果年份始终是最后一个字段,则以下内容将起作用。如果仍然无法正常工作,请提供输入数据和预期输出。当我不完全理解问题时,很难给出准确的结果。祝你好运。
Here are what I understand from your description, your data might look something like this:
In this case, you want to print out "George" because his birthday is less than 1999. If these assumptions are correct, here is how I solve it:
Discussion
Update
If the year is always the last field, the the following would work. If it is still not working, please supply input data and expected output. It is hard to give accurate result when I don't fully understand the problem. Good luck.