0

Tried couple of answers from similar questions but not quite getting correct results. Trying to search second file for variable and replace with second variable if there, otherwise keep original...

File1.txt

a
2
c
4
e
f

File2.txt

2 b
4 d

Wanted Output.txt

a
b
c
d
e
f

So far what I have seems to sort of work, but anywhere the replacement is happening I'm getting a blank row instead of the new variable... Current Output.txt

a

c

e
f

Curent code....

awk -F'\t' 'NR==FNR{a[$1]=$2;next} {print (($1 in a) ? a[$1] : $1)}' file2.txt file1.txt > output.txt

Also tried and got same results...

awk -F'\t' 'NR==FNR{a[$1]=$2;next} {$1 = a[$1]}1' file2.txt file1.txt > output.txt 

Sorry first wrote incorrectly..fixed the key value issue.

Did try what you did, still not getting missing in output.txt

awk -F'\t' 'NR==FNR{a[$1]=$2;next} $1 in a{$1 = a[$1]}1' file2.txt file1.txt > output.txt
4
  • Also just a comment, these files are about 2million lines for file1 and 1.5million for file2. I only want to change file1 when match is found in file2. I've checked number of lines in my output file to make sure it is correct, which it is, but when I check some that I know should have been changed, that's when I found they are blank lines instead of having the new variable. So it seems to be finding the variable that matches, but then replacing it with nothing. Commented Oct 24, 2016 at 14:49
  • Curent code is missing a closing parenthesis. Commented Oct 24, 2016 at 17:04
  • fixed the parenthesis, was in my actual code, so not what's causing the problem. Thanks. Commented Oct 24, 2016 at 18:47
  • No it's not. It's gotta be something with your data. Codes are fine. Double tabs or something. Or a tab and a space. Commented Oct 24, 2016 at 18:57

2 Answers 2

1

your key value pair is not right... $1 is the key, $2 is the value.

$ awk -F'\t' 'NR==FNR{a[$1]=$2;next} $1 in a{$1=a[$1]}1' file.2 file.1
a
b
c
d
e
f
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, I wrote it in here for more simplicity and mixed the key value up. I just tried what you wrote and still giving me missing values...and a little confused as how
'$1 in a{$1=a[$1]}' is different that what I wrote above?
yes, you wrote $1 = a[$2], which is also not correct.
Hi, Sorry. Because I had originally mixed up the key value above, I thought you're {1=a[$1]} was different than mine because of that, but it's not. I just changed that one part and it works. Can you explain that part. I thought it was saying make column 1 of file1 now equal the value of a[$2] from file2?
Think of array "a" as a lookup. You are looking for the value stored for the key, which is $1.
0

try below solution -

awk 'NR==FNR{a[$1]=$NF;next} {print (a[$NF]?a[$NF]:$1)}' file2.txt file1.txt

a
b
c
d
e
f

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.