Computing.Net > Forums > Unix > convert string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

convert string

Reply to Message Icon

Name: n
Date: November 19, 2002 at 19:49:59 Pacific
OS: unix
CPU/Ram: 256
Comment:

Hi all,

I need your help. I have the following input files:

file1:
T02349,Apple,3434
T24230,Blue,12390
T09398,Tennis,67567

file2:
Blue,Green
Tennis,Golf

I 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,67567

I appreciate your help.



Sponsored Link
Ads by Google

Response Number 1
Name: someone
Date: November 19, 2002 at 22:37:01 Pacific
Reply:

Looks suspiciously like homework!


0

Response Number 2
Name: Jimbo
Date: November 20, 2002 at 01:23:32 Pacific
Reply:

#! /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}"
fi

done > output


0

Response Number 3
Name: n
Date: November 20, 2002 at 07:08:14 Pacific
Reply:

Hi Jimbo,

Your script works. Thanks a million!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: convert string

string to number www.computing.net/answers/unix/string-to-number/5537.html

HPUX www.computing.net/answers/unix/hpux/1134.html

Wrong output for permission string www.computing.net/answers/unix/wrong-output-for-permission-string/5102.html