awk - replacing stings in file1 with column1 in file2

Hello,

I've never used awk before, but from what I've read, it will best suit what I'm trying to do. I have 2 files. I need to replace strings in file1 with the first column of a matching string in file2. Below are examples:

File1:
random-string1
1112
1232
3213
2131
random-string2
1231
1112
3213

File2:
DD 3213
CCA 1231
AABC 1112
ADFDFF 1232
DDFSCC 2131

Output File3:
random-string1
AABC
ADFDFF
DD
DDFSCC
random-string2
CCA
AABC
DD

I tried something like:
grep `cat file1` file2 | awk {'print $1'} > file3

I lose the random-strings with my output. I'm sure there's an easier way to do it with awk alone and drop the grep.

Thanks

try this

awk 'FILENAME=="file2"{A[$2]=$1}
FILENAME=="file1"{if(A[$1]){print A[$1]}else{print $0}}' file2 file1

Thank you for the quick reply. I apologize if this is a novice question, but so I'm clear, I would run it like this?

file1=master.txt
file2=ref.txt

awk 'FILENAME=="ref.txt"{A[$2]=$1}
FILENAME=="master.txt"{if(A[$1]){print A[$1]}else{print $0}}' ref.txt master.txt

When I try that, I have no output - I'm sure I'm not using it correctly.

Thanks

you are doing it correctly..
you are not getting any errors right??
and are you sure that file contents are same as you given in your question.. i mean no special character and all.

I see where I went wrong.. my examples were not accurate. My apologies.

in file2 (ref.txt) the values look more like this:

File2:
DD 3213
CCA 1231
AA BC 1112
ADFDFF 1232
DDF SCC 2131

I ran this:

awk 'FILENAME=="ref.txt"{A[$2]=$1}
FILENAME=="master.txt"{if(A[$1]){print A[$1]}else{print $0}}' ref.txt master.txt > output.txt

Any string in file 2 that had spaces (AA BC, DDF SCC) did not work for obvious reasons. I've regenerated file 2 to look like this now with sed:

DD:3213
CCA:1231
AA BC:1112
ADFDFF:1232
DDF SCC:2131

Is there any way I can use the awk script you gave me to recognize the columns separated by the colon? I can change the colon to any other value as well if there is something that will work better with awk.

Thanks for the help and patience.

---------- Post updated at 01:39 AM ---------- Previous update was at 01:18 AM ----------

I tried this but no lucK:

awk 'FILENAME=="ref.txt"{FS = : ; A[$2]=$1}
FILENAME=="master.txt"{if(A[$1]){print A[$1]}else{print $0}}' ref.txt master.txt > results.txt

awk: FILENAME=="ref.txt"{FS = : ; A[$2]=$1}
awk: ^ syntax error

---------- Post updated at 01:53 AM ---------- Previous update was at 01:39 AM ----------

Thank you for the help vidyadhar85.

I was able to get it to work with this:

awk '
BEGIN{FS=":"}
FILENAME=="ref.txt"{A[$2]=$1}
FILENAME=="master.txt"{if(A[$1]){print A[$1]}else{print $0}}' ref.txt master.txt > results.txt

even this will do..

awk 'FILENAME=="file2"{FS=":";A[$2]=$1}
FILENAME=="file1"{if(A[$1]){print A[$1]}else{print $0}}' file2 file1

I'm not sure if I should create a new thread or not - I'm seeing now that before I run the awk script, I need to adjust File1.

Again:
File1:
random-string1
1112
1232
3213
2131
random-string2
1231
1112
3213

I need to insert some text in front of each of the random-strings. The random strings always begin with letters. Looking for output:

File1:
PAGE-random-string1
1112
1232
3213
2131
PAGE-random-string2
1231
1112
3213

Thanks

small change..

awk 'FILENAME=="file2"{FS=":";A[$2]=$1}
FILENAME=="file1"{if(A[$1]){print A[$1]}else{print "PAGE-"$0}' file2 file1