Skip to main content
edited body
Source Link
peter
  • 23
  • 3

file1.txt (tab delimiter, with the second column containing a string with spaces):

A   Golden fog
B   Vibrant rainbow and sunny
C   Jumping, bold, and bright
D   Chilly/cold/brisk air

file2.txt (tab delimiter):

D01 Ti600   A
D02 Ti500   B
D16 Ti700   C
D20 Ti800   B

desired output for file3.txt (having a tab delimiter):

D01 Ti600   A   Golden fog
D02 Ti500   B   Vibrant rainbow and sunny
D16 Ti700   C   Jumping, bold, and bright
D20 Ti800   DB   Vibrant rainbow and sunny

or at least this for file3.txt:

D01 Ti600   Golden fog
D02 Ti500   Vibrant rainbow and sunny
D16 Ti700   Jumping, bold, and bright
D20 Ti800   Vibrant rainbow and sunny

I have tried

awk 'NR==FNR{a[$1]=$2;next}{$3=a[$1];}1' file1.txt file2.txt > file3.txt

But I only get:

D01 Ti600 
D02 Ti500 
D16 Ti700 
D20 Ti800 

Which has a space deliminator instead of tabs, as well as a space after column 2, but no value in column 3.

Thanks so much for any help with getting the desired output.

file1.txt (tab delimiter, with the second column containing a string with spaces):

A   Golden fog
B   Vibrant rainbow and sunny
C   Jumping, bold, and bright
D   Chilly/cold/brisk air

file2.txt (tab delimiter):

D01 Ti600   A
D02 Ti500   B
D16 Ti700   C
D20 Ti800   B

desired output for file3.txt (having a tab delimiter):

D01 Ti600   A   Golden fog
D02 Ti500   B   Vibrant rainbow and sunny
D16 Ti700   C   Jumping, bold, and bright
D20 Ti800   D   Vibrant rainbow and sunny

or at least this for file3.txt:

D01 Ti600   Golden fog
D02 Ti500   Vibrant rainbow and sunny
D16 Ti700   Jumping, bold, and bright
D20 Ti800   Vibrant rainbow and sunny

I have tried

awk 'NR==FNR{a[$1]=$2;next}{$3=a[$1];}1' file1.txt file2.txt > file3.txt

But I only get:

D01 Ti600 
D02 Ti500 
D16 Ti700 
D20 Ti800 

Which has a space deliminator instead of tabs, as well as a space after column 2, but no value in column 3.

Thanks so much for any help with getting the desired output.

file1.txt (tab delimiter, with the second column containing a string with spaces):

A   Golden fog
B   Vibrant rainbow and sunny
C   Jumping, bold, and bright
D   Chilly/cold/brisk air

file2.txt (tab delimiter):

D01 Ti600   A
D02 Ti500   B
D16 Ti700   C
D20 Ti800   B

desired output for file3.txt (having a tab delimiter):

D01 Ti600   A   Golden fog
D02 Ti500   B   Vibrant rainbow and sunny
D16 Ti700   C   Jumping, bold, and bright
D20 Ti800   B   Vibrant rainbow and sunny

or at least this for file3.txt:

D01 Ti600   Golden fog
D02 Ti500   Vibrant rainbow and sunny
D16 Ti700   Jumping, bold, and bright
D20 Ti800   Vibrant rainbow and sunny

I have tried

awk 'NR==FNR{a[$1]=$2;next}{$3=a[$1];}1' file1.txt file2.txt > file3.txt

But I only get:

D01 Ti600 
D02 Ti500 
D16 Ti700 
D20 Ti800 

Which has a space deliminator instead of tabs, as well as a space after column 2, but no value in column 3.

Thanks so much for any help with getting the desired output.

Source Link
peter
  • 23
  • 3

Adding a new column in file1 that outputs a string in a reference file file2 that matches the value of another column in file1

file1.txt (tab delimiter, with the second column containing a string with spaces):

A   Golden fog
B   Vibrant rainbow and sunny
C   Jumping, bold, and bright
D   Chilly/cold/brisk air

file2.txt (tab delimiter):

D01 Ti600   A
D02 Ti500   B
D16 Ti700   C
D20 Ti800   B

desired output for file3.txt (having a tab delimiter):

D01 Ti600   A   Golden fog
D02 Ti500   B   Vibrant rainbow and sunny
D16 Ti700   C   Jumping, bold, and bright
D20 Ti800   D   Vibrant rainbow and sunny

or at least this for file3.txt:

D01 Ti600   Golden fog
D02 Ti500   Vibrant rainbow and sunny
D16 Ti700   Jumping, bold, and bright
D20 Ti800   Vibrant rainbow and sunny

I have tried

awk 'NR==FNR{a[$1]=$2;next}{$3=a[$1];}1' file1.txt file2.txt > file3.txt

But I only get:

D01 Ti600 
D02 Ti500 
D16 Ti700 
D20 Ti800 

Which has a space deliminator instead of tabs, as well as a space after column 2, but no value in column 3.

Thanks so much for any help with getting the desired output.