Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix shell: replace by dictionary

Tags:

grep

shell

sed

awk

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"

like image 687
Vitaliy Avatar asked Jul 10 '26 11:07

Vitaliy


1 Answers

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
like image 170
Dimitre Radoulov Avatar answered Jul 12 '26 00:07

Dimitre Radoulov