如何在 Unix 命令行中搜索文件内的模式并删除这些行?
我需要在文件中搜索模式。 例如文件的内容如下:
3555005!K!00630000078!C!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!47001231000000!0!336344324!1!1!POST!USAGE!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
这里我想搜索带有 !D! 的行并且该行中的第 7 个字段小于系统日期,那么我想删除该行并保存文件。
这可能吗?
I need to search for a pattern in files.
For example the content of the file is below:
3555005!K!00630000078!C!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!47001231000000!0!336344324!1!1!POST!USAGE!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
Here I want to search for lines with !D! and the 7th field in the line is less than the system date, then I want to delete the line and save the file.
Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的东西应该可以解决问题...如果这不是您设置字段格式的方式,您可能需要解析时间
Something like this should do the trick... you may want to parse the time if this is not how you have the field formatted
如果您更喜欢AWK...
其中logstrip.awk看起来
应该可以解决问题。
但请注意,您的字段 #7 包含奇数日期格式。我想我认识到最近的纪元值(123...),但它前面有 4 个显然不相关的数字。在与 StopDate 进行比较之前,可以轻松删除这些内容
If you prefer AWK...
where logstrip.awk looks something like
should do the trick.
Attention, however, your field #7 contains an odd date format. I think I recognize an recent epoch value (123...) but it is preceded by 4 apparently unrelated digit. These can easily be removed before comparing to StopDate
基于 mjv 的答案,但进行了简化并使用(假设)第五个字段作为日期(为了可读性分为两行):
Based on mjv's answer, but simplified and using (assuming) the fifth field for the date (broken into two lines for readability):
我用文件中的示例数据进行了测试
,但它正在删除包含 !D! 的所有行。
我使用了以下 awk 脚本
我做错了什么吗?
i tested with the sample data in a file
but it's is deleting all the lines which consists of !D!.
I used the following awk script
Am I doing anything wrong?