transfer a variable in a shell script?

Hey guys, seems I can't transfer the $ip into gawk in the following bash script, where the problem is?

#!/bin/bash
while read ip
do
gawk '$1~"$ip"' /tmp/source
done < /tmp/ip

++
cat /tmp/ip

192.16.1.1|192.168.1.2|192.168.1.3
10.1.1.1|10.1.1.2
10.8.1.0|10.8.1.1|10.8.1.2

--
cat /tmp/source
192.1.1.1 192.1.2.5 a
10.1.1.1 192.1.1.1 b
.....

Thank you!

gawk '"'$1'" ~ "'$ip'"' source

hello anbu23, thank you for your reply, but it does not work, I already double checked.

and btw, do you have some recommendation reading on this? transfer variable within shell script?

try this

gawk -v par="$1" -v ipaddr="$ip" ' par ~ ipaddr ' source

chk this link
http://www.faqs.org/faqs/computer-lang/awk/faq/

hate to say this, but still it does not work.

I will dig up the link

use awk. if it still doesn't work show us the trace of script

error messages go like this,
--
awk: syntax error near line 1
awk: bailing out near line 1

AH, thank you for your link, anbu23

using awk like this will work!

awk '$1~ /'"$ip"'/' sourcefile

can you show ur script?

sure, anbu23, as follow,

#!/bin/bash
while read hosts
do
echo $hosts
awk '$1~ /'"$hosts"'/' /tmp/source > /tmp/$hosts.src
done < /tmp/test

i didn't see this message .