Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
I need your help. I have the following input files:
file1:
T02349,Apple,3434
T24230,Blue,12390
T09398,Tennis,67567file2:
Blue,Green
Tennis,GolfI need a script that will:
1) extract the string populated in field 2 of file1
2) if string from 1)can be found in the first field in file2, extract the corresponding string from the 2nd field from file2. Then replace field 2 of file1 with this extracted string.
3) if string from field 2 of file1 cannot be found in file2, leave the line unchanged.In my example, the expected output will be:
T02349,Apple,3434
T24230,Green,12390
T09398,Golf,67567I appreciate your help.

#! /bin/ksh
cat file1 | while IFS=, read num1 word num2 ; do
if grep $word file2 > /dev/null 2>&1 ; then
print "${num1},`grep $word file2 | cut -d, -f2 `,${num2}"
else
print "${num1},${word},${num2}"
fidone > output

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |