Replacement by dictionary possible with AWK or Sed? -


you have dictionary, dictionary.txt, , input file, infile.txt. dictionary tells possible translations. solution similar problem in unix shell: replace dictionary seems hardcode things here cannot understand. can come better replacement technique dictionary awk/sed script should able read in multiple files, in simplest case 1 dictionary file , 1 infile.

how replace elegantly dictionary awk or sed?


example

dictionary.txt

1 1 2 two  3 three 4 fyra 5 fem 

infile.txt

one 1 hello hallo 2 3 hallo 5 five 

output command, after command awk/sed {} dictionary.txt infile.txt

one 1 hello hallo 2 3 hallo fem fem 

awk example selected replacements one-one replacements not working.

awk 'begin {  lvl[1] = "one"  lvl[2] = "two"  lvl[3] = "three"  # todo: not work   # lvl[four] = "fyra"  # lvl[five] = "fem"  # lvl[one] = "one"  # lvl["hello"] = "hello"  # lvl[hallo] = "hallo"  # lvl[three] = "three"  } nr == fnr {   evt[$1] = $2; next   } {    print $1, evt[$2], $3, $4, evt[$5], $6, $7, evt[$8], evt[$9]    #todo: dos not work, eg. one-one mapping       #   print evt[$1], evt[$2], evt[$3], evt[$4], evt[$5], evt[$6], evt[$7], evt[$8], evt[$9]   }' dictionary.txt infile.txt 

$ awk 'nr==fnr{map[$1]=$2;next} { (i=1;i<=nf;i++) $i=($i in map ? map[$i] : $i) } 1' filea fileb 1 one hello hallo 2 3 hallo fem fem 

note compress chains of contiguous white space single blank char. tell if issue.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -