unix shell:用字典替换
我有一个包含一些数据的文件,像这样
2011-01-02 100100 1
2011-01-02 100200 0
2011-01-02 100199 3
2011-01-02 100235 4
,并且在单独的文件中有一些“字典”
100100 Event1
100200 Event2
100199 Event3
100235 Event4
,我知道
0 - warning
1 - error
2 - critical
etc...
我需要一些带有 sed/awk/grep 的脚本或其他可以帮助我接收这样的数据的脚本,我
100100 Event1 Error
100200 Event2 Warning
100199 Event3 Critical
etc
将不胜感激如何以最好的方式做到这一点,或者对于工作示例
更新
有时我有这样的数据
2011-01-02 100100 1
2011-01-02 sometext 100200 0
2011-01-02 100199 3
2011-01-02 sometext 100235 4
,其中sometext =任意6个字符(也许这是有用的信息)
在这种情况下我需要整个数据:
2011-01-02 sometext EventNameFromDictionary Error
或者没有“sometext”
I have file which contains some data, like this
2011-01-02 100100 1
2011-01-02 100200 0
2011-01-02 100199 3
2011-01-02 100235 4
and have some "dictionary" in separate file
100100 Event1
100200 Event2
100199 Event3
100235 Event4
and I know that
0 - warning
1 - error
2 - critical
etc...
I need some script with sed/awk/grep or something else which helps me receive data like this
100100 Event1 Error
100200 Event2 Warning
100199 Event3 Critical
etc
will be grateful for ideas how to do this in best way, or for working example
update
sometimes I have data like this
2011-01-02 100100 1
2011-01-02 sometext 100200 0
2011-01-02 100199 3
2011-01-02 sometext 100235 4
where sometext = any 6 characters (maybe this is helpful info)
in this case I need whole data:
2011-01-02 sometext EventNameFromDictionary Error
or without "sometext"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为新要求添加新答案,并且由于注释中的格式选项有限:
如果您使用GNU awk,则无需转义三级运算符内的新行。
某些 awk 实现可能在这部分存在问题:
如果您使用其中之一,
修改为:
好的,评论补充:
Adding a new answer for the new requirement and because of the limited formatting options inside a comment:
You won't need to escape the new lines inside the tertiary operator if you're using GNU awk.
Some awk implementations may have problems with this part:
If you're using one of those,
change it to:
OK, comments added:
我希望 perl 也可以:
输出:
I hope perl is ok too:
Output: