Delimited File Help

Hey guys I am new to unix programming and need some help with a task.

I have a file that is comma delimited with values like this

1,NEW.txt
1,OLD.txt
2,MONKEY.txt
3,FUN.txt
100,MONEY.txt

I have a user enter a value for the type they want (1,2,3,100).

I then want to compare the first column in the file to that entered value and return the second column in that file if it matches.

If 1 is entered I want returned:

NEW.txt
OLD.txt

I cannot figure out how to use awk, cut or sed to do this.

Any help would be greatly appreciated. Thanks in advance folks!

One way of doing it:

script

#!/bin/bash
filename="$1"
echo "Enter value "
read value

awk -F, -v var="$value"  '{if (var == $1 ) print $2}'  "$filename"

It should be run as :

./script your_file

Rubin... I am getting an error on the code given. I hardcoded some test data and tried to run.

awk -F, -v var="1"  '{if (var == $1 ) print $2}'  GO_FILE_LIST.dat   

awk: syntax error near line 1
awk: bailing out near line 1

Works here. Do you have a really old version of awk? The script itself is so trivial as to hardly contain any syntax at all -- does your awk understand -F and -v? If not, do you have access to nawk, or mawk, or gawk, ... or (gasp) Perl? (Hint: a2p)

Thats a very good question, I have no idea what version of awk I have...

I have access to nawk but not the other two. And I do have access to Perl but know nothing about it.

This ended up working for me... Can you see any issues with this?

$VAR_NAM=$1
$FILE_LIST=$2
$TEMP_FILE=$3

awk -F, '$1=='"$VAR_NAM"' {print $2}'  $FILE_LIST > $TEMP_FILE

Thanks for the help!

Update...
Using nawk the previous code worked fine with the IF statements thanks for the help folks!

Strictly speaking, you should put double quotes around the file names too. And obviously, when assigning to a variable, you don't put a dollar sign in front of the variable name. Other than that, schoolbook example -- good work.

Would still be interesting to learn if switching from awk to nawk would solve the problem you were having with the syntax error.

Yes switching to nawk solved the problem...

Here is the test code used...

AML_SER=$1
FILE_LIST=$2
TEMP_FILE=$3

nawk -F, -v var="$AML_SER"  '{if (var == $1 ) print $2}' "$FILE_LIST" > "$TEMP_FILE"