AWK:有条件地抓取文件中的文本

发布于 2025-01-05 03:58:17 字数 426 浏览 0 评论 0原文

我需要在文件中找到一个具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙厌世 2025-01-12 03:58:17

以下是我从你的描述中了解到的内容,你的数据可能看起来像这样:

Birthday = 2011
Name = Anna
Birthday = 1987
Name = George

在这种情况下,你想打印出“George”,因为他的生日小于 1999 年。如果这些假设是正确的,那么我是如何解决这个问题的:

awk '$1=="Birthday"{birthday = $3} $1=="Name" && birthday<1999 {print $3}' birthday.txt

讨论

  • 我假设你的数据文件名为birthday.txt
  • 第一部分 $1=="Birthday"{birthday = $3} 将生日字段保存到变量 birthday
  • 接下来,如果该行包含“名称”我们检查保存的生日并打印

更新

如果年份始终是最后一个字段,则以下内容将起作用。如果仍然无法正常工作,请提供输入数据和预期输出。当我不完全理解问题时,很难给出准确的结果。祝你好运。

awk '/BirthdayYear/{birthday=$NF} $1=="Name" && birthday<1999 {print $3}' birthday.txt

Here are what I understand from your description, your data might look something like this:

Birthday = 2011
Name = Anna
Birthday = 1987
Name = George

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:

awk '$1=="Birthday"{birthday = $3} $1=="Name" && birthday<1999 {print $3}' birthday.txt

Discussion

  • I assume your data file is called birthday.txt
  • The first part $1=="Birthday"{birthday = $3} saves the birthday field into the variable birthday
  • Next, if the line contains "Name" we check the birthday which we saved and print

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.

awk '/BirthdayYear/{birthday=$NF} $1=="Name" && birthday<1999 {print $3}' birthday.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文