0

dear all i have INPUT.txt like this (look at last column)

INPUT.txt
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.txt
j feat1
a feat2
.
.
.

and

TABLE2.txt
j sp1
a sp2
.
.

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

output.txt
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 ] ]
.
.
.
  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem. – Kusalananda Feb 7 '18 at 10:54
0

If you have GNU awk, then you could use a mutlidimensional version of the canonical NR==FNR {a[$1]=$2; next} $NF in a {print $0, a[$NF]} with the following modifications:

  1. you can't use the NR==FNR trick to decide whether you are building the lookup table or using it; you will need to count how many files have been processed

  2. you will need to loop over the primary index explicitly and test for the match key in each subarray

Ex.

gawk '
  BEGINFILE{k++}
  k<3 {a[k][$1]=$2; next} 
  {nf = NF; for (l in a) {if ($nf in a[l]) {$(NF+1) = a[l][$nf]}}}
  {print}
' TABLE1.txt TABLE2.txt INPUT.txt 

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 ] ]

If you want column headers, print them in a BEGIN rule.

0

You can try this awk too

awk '
FILENAME!=fn{
  a[$1]=a[$1]FS$2
  next
}
$NF in a{
  $0=$0a[$NF]
}
1
' fn='INPUT.txt' TABLE1.txt TABLE2.txt INPUT.txt
0

I have done this by below 2 steps

step1

awk 'FNR==NR{a[FNR]=$2;next};{$(NF + 1)=a[FNR]}1' file1 input.txt  >> final_final.txt

step2

awk 'FNR==NR{a[FNR]=$2;next};{$(NF + 1)=a[FNR]}1' file2 final_final.txt

output

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 ] ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.