Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
810 views
in Technique[技术] by (71.8m points)

sed - unix shell: replace by dictionary

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"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...