Is dictionary replacement possible with AWK or Sed?

You have a dictionary, Dictionary.txt and an inFile.txt input file. The dictionary talks about possible translations. The solution to a similar problem in unix shell: replace with a dictionary seems to be making hard codes here , which I cannot fully understand. You can come up with a better replacement technique than a dictionary, but an AWK / Sed script should be able to read in multiple files, in the simplest case only one dictionary file and one infile.

How to elegantly replace the dictionary with AWK or Sed?


Example

Dictionary.txt

1 one 2 two 3 three four fyra five fem 

inFile.txt

 one 1 hello hallo 2 three hallo five five 

The output from the command, we will follow the command as awk/sed {} Dictionary.txt inFile.txt

 one one hello hallo two three hallo fem fem 

An AWK example in which replacements are specifically selected, but one single replacement does not work.

 awk 'BEGIN { lvl[1] = "one" lvl[2] = "two" lvl[3] = "three" # TODO: this does 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: this 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 
-eight
source share
4 answers
 $ awk 'NR==FNR{map[$1]=$2;next} { for (i=1;i<=NF;i++) $i=($i in map ? map[$i] : $i) } 1' fileA fileB one one hello hallo two three hallo fem fem 

Note that it compresses any chains of adjacent spaces to a single char. Let us know if this is a problem.

+4
source

if you have gnu sed, it supports a script file with -f :

 `-f SCRIPT-FILE' `--file=SCRIPT-FILE' Add the commands contained in the file SCRIPT-FILE to the set of commands to be run while processing the input. 

you could write your permutations in "c.sed", for example,

 sed -f c.sed file 

example c.sed :

 s/1/one/g s/2/two/g ... 

EDIT

you just did not mark the awk question, of course awk one-liner will be simpler: (with your example)

 awk '$1=$2' file 

Test:

 kent$ echo "1 one 2 two 3 three four fyra five fem"|awk '$1=$2' one one two two three three fyra fyra fem fem 
+4
source

EDIT

This answers the original post. doesn't answer a multiple edited and restructured question ... on top of that, I get -1 from the OP who asked this question ... Damn!

Yes, much simpler in awk:

This will print both columns as a value for the second column:

 awk '{print $2, $2}' file 

If you want to flip the second column first:

 awk '{print $2, $1}' file 
+3
source

If ReplaceLeftWithRight_where_you_do_not_replace_things.txt contains pairs of line replacements, where any occurrence of text in the first column should be replaced by the second column,

 1 one 2 two 3 three four fyra five fem 

then this can be trivially expressed as a sed script.

 s/1/one/g s/2/two/g s/3/three/g s/four/fyra/g s/five/fem/g 

and you can trivially use sed to create this sed script:

 sed 's%.*%s/&/g%;s% %/%' ReplaceLeftWithRight_where_you_do_not_replace_things.txt 

then pass the output of this to the second sed instance:

 sed 's%.*%s/&/%;s% %/%' ReplaceLeftWithRight_where_you_do_not_replace_things.txt | sed -f - someFile_Where_You_Replace_Things.txt 

to replace all matches in someFile_Where_You_Replace_Things.txt and print the output to standard output.

Unfortunately, not all sed dialogs support the -f - option to read a script from standard input, but this should work, at least on most Linux.

Sorry if I misunderstood your problem.

+3
source

Source: https://habr.com/ru/post/1492272/


All Articles