Awk, sed, shell all words in INPUT.txt find in column1 of TABLE.txt and replce with column2 in

Hi dears
i have text file like this:
INPUT.txt

001_1_173    j                          nuh                        ]az
001_1_174    j                          ]esma.                        nuh                        ]/.xori
.
.
. 

and have another text
like this
TABLE.txt

j j
nuh word1
]az word2
]esma. word3
]/.xori word 4
.
.
.

i want to that all words in INPUT.txt find in column1 of TABLE.txt and replce with column2 in.

for example output must like that
OUTPUT.txt

001_1_173 j word1 word2
001_1_174 j word3 word1 word4

column1 in INPUT.txt must copy in column1 of OUTPUT.txt .
number of columns in INPUT.txt not the same.

thanks all.

Hello alii...

First of all put your requirements inside code tags

 something like this 

as per the site's requirements.
Next, what have you attempted so far.
Which Operating System you are using.
Which shell is your preference.
What are the OS and Shell versions that you have.
Your _preferred_ utilities to attain the results...

awk '
NR==FNR {w[$1]=$2; next;}
{for (i=2; i<=NF; i++) if ($i in w) $i=w[$i]; print $0;}
' TABLE.txt INPUT.txt
1 Like

One might also consider explaining how the 3rd column in TABLE.txt is supposed to be used.

It appears that you want ]/.xori to be translated to word4 , but column 2 in TABLE.txt on the line with ]/.xori has word in column 2 on that line and 4 in column 3; not word4 , Your description didn't say anything about a column 3 in TABLE.txt . :confused:

thanks all
i use UBUNTU 16.04 LTS
it's ok.
may explain your code?

so another question
i have INPUT
like this
see last column
number of columns different in one
some row have 12 , some 11

INPUT
CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic 
63 M 27 BS/BA TEHRANI 3 4 298320 310050 11730 j j 
63 M 27 BS/BA TEHRANI 3 4 310050 311430 1380 ( a 
63 M 27 BS/BA TEHRANI 3 4 311430 312080 650 ] ] 

and two TABLE text like this.have two columns.

TABLE1
j feat1
a feat2
.
.
.
TABLE2
j sp1
a sp2
.
.

I want to add two columns to INPUT
search last column of INPUT in TABLEs text and add correspond columns to INPUT
(i think can merge TABLEs like j feat1 sp1)
and output like this

output
CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic feat sp
63 M 27 BS/BA TEHRANI 3 4 298320 310050 11730 j j feat1 sp1
63 M 27 BS/BA TEHRANI 3 4 310050 311430 1380 ( a feat2 sp2
63 M 27 BS/BA TEHRANI 3 4 311430 312080 650 ] ]
Moderator comments were removed during original forum migration.
1 Like

sorry about that
thanks for your guides