0

This question already has an answer here:

sample log file:

apple AAA rose
banana AAA tulip
orange AAA marigold

replacement-text file:

111
222
333

Expected output:

apple 111 rose
banana 222 tulip
orange 333 marigold

marked as duplicate by Stéphane Chazelas awk Oct 2 '18 at 14:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Any attempts / ideas / thoughts from your side? – RudiC Oct 2 '18 at 10:30
  • perl -lpe ' chomp(@A=qx/cat repl.txt/) if $. == 1; s/\h\KAAA(?=\h)/shift @A/e; ' input.txt – Rakesh Sharma Oct 3 '18 at 5:13
1

There are umpteen ways to skin a cat. Let's assume the connection between both files is on line No. Try e.g. this one, which pastes lines together and then shifts the trailing word to the field containing AAA using regex ""back references":

paste sample_log replacement-text | sed -r 's/AAA ([^[:space:]]*[[:space:]]*)(.*$)/\2 \1/'
apple 111 rose  
banana 222 tulip    
orange 333 marigold 

or this one, which makes awk read the replacements into an array, and then replaces the second field by the respective array element:

awk 'NR==FNR {T[NR] = $0; next } {$2 = T[FNR]} 1' replacement-text sample_log 
apple 111 rose
banana 222 tulip
orange 333 marigold
0
#!/bin/bash 
paste logfile.txt replacement.txt | while IFS= read -r line; do

echo $line | sed "s/`echo $line | awk '{print $2}'`/`echo $line | awk '{print $4}'`/g" | awk '{print $1 " " $2 " " $3 " "}'

done

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