unix shell:用字典替换 [英] unix shell: replace by dictionary

查看:23
本文介绍了unix shell:用字典替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些数据的文件,就像这样

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

我知道

0 - warning
1 - error
2 - critical
etc...

我需要一些带有 sed/awk/grep 的脚本或其他可以帮助我接收这样的数据的脚本

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

更新

有时我有这样的数据

2011-01-02 100100 1
2011-01-02 sometext 100200 0
2011-01-02 100199 3
2011-01-02 sometext 100235 4

where sometext = 任意 6 个字符(也许这是有用的信息)
在这种情况下,我需要完整的数据:

where sometext = any 6 characters (maybe this is helpful info)
in this case I need whole data:

2011-01-02 sometext EventNameFromDictionary Error

或者没有sometext"

or without "sometext"

推荐答案

awk 'BEGIN {
 lvl[0] = "warning"
 lvl[1] = "error"
 lvl[2] = "critical"
 }
NR == FNR {
  evt[$1] = $2; next
  } 
{
  print $2, evt[$2], lvl[$3]
  }' dictionary infile

这篇关于unix shell:用字典替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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