Replace dictionary-based text

I need to do something similar to this post (but with a twist). That is why I ask.

unix shell: replace with dictionary

I have a dictionary (dict.txt). This space is divided, and it reads as follows:

V7 momentum

B6 Quanta p>

....

(the first column is the key, and the second column is the value, in a sense)

I have a user file (user.txt), it contains key entries (V7, B6, etc.). The twist is that the keys are not in their column (therefore, the method in the above record does not apply).

The user file (user.txt) can be viewed as a stream of characters. I just want to replace all occurrences of keys (for example, V7), regardless of whether they are space-limited or limited by another character to the value (Momentum) derived from the dictionary.

For instance:

β€œWe have V7 as input” β†’ should change to β†’ β€œWe have Momentum as input”

"We have something V7_as input" β†’ should change to β†’ "We have something Momentum_as input"

+3
source share
3 answers

Usage: awk -f foo.awk dict.dat user.dat
http://www.gnu.org/software/gawk/manual/html_node/String-Functions.html
http://www.gnu.org/software/gawk/manual/html_node/Arrays.html

 NR == FNR { rep[$1] = $2 next } { for (key in rep) gsub(key, rep[key]) print } 
+8
source

As long as your vocabulary keys contain nothing but alphanumeric characters, this Perl will do what you need.

 use strict; use warnings; open my $fh, '<', 'dict.txt' or die $!; my %dict = map { chomp; split ' ', $_, 2 } <$fh>; my $re = join '|', keys %dict; open $fh, '<', 'user.txt' or die $!; while (<$fh>) { s/($re)/$dict{$1}/g; print; } 
+3
source

This may work for you (GNU sed):

 sed '/./!d;s/\([^ ]*\) *\(.*\)/\\|\1|s||\2|g/' dict.txt | sed -f - user.txt 
+2
source

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


All Articles