UNIX外壳:由字典更换 [英] unix shell: replace by dictionary

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

问题描述

我有文件,其中包含一些数据,像这样

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

和在单独的文件中一些字典

  100100事件1
100200 EVENT2
100199 EVENT3
100235 Event4

和我知道,

  0  - 警告
1 - 错误
2 - 关键
等等...

我需要SED / AWK / grep或者别的一些脚本,可以帮助我收到这样的数据

  100100事件1错误
100200 EVENT2警告
100199 EVENT3关键
等等

会很感激的想法如何做最好的这种方式,或者工作的例子。

更新

有时我有这样的数据。

  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错误

或无SomeText则会


解决方案

 的awk'BEGIN {
 LVL [0] =警告
 LVL [1] =错误
 LVL [2] =关键
 }
NR == FNR {
  EVT [$ 1] = $ 2;下一个
  }
{
  打印$ 2,EVT [$ 2],LVL [$ 3]
  }字典INFILE

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"

解决方案

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外壳:由字典更换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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