awk programming [message #113398] |
Sat, 02 April 2005 03:21 |
vishamr2000
Messages: 18 Registered: April 2005
|
Junior Member |
|
|
Hi to all,
I would like to know if it's possible to use $1 and $2 from an awk command directly in a shell script.
#!/bin/sh
awk '' $root/home/bin/myfile
echo "$1 $2"
#########
Is sth like the above possible?
Regards,
Visham
|
|
|
Re: awk programming [message #113401 is a reply to message #113398] |
Sat, 02 April 2005 05:01 |
William Robertson
Messages: 1643 Registered: August 2003 Location: London, UK
|
Senior Member |
|
|
In awk, $1, $2 etc refer to fields separated by whitespace (the default) or your specified separator.
In the shell, they refer to command-line arguments passed to the current process.
You can use awk to extract data from a file and read it into variables, for example (using ksh):
$: cat test.dat
English: one two three four
French: un deux trois quatre
$: awk '/^English/ {print $2,$3}' test.dat | read v1 v2
$: print $v1
one
$: print $v2
two
$: set -A numbers $(awk '/^English/ {print $2,$3,$4,$5}' test.dat)
$: print ${numbers[0]}
one
|
|
|
Re: awk programming [message #113411 is a reply to message #113398] |
Sat, 02 April 2005 09:46 |
vishamr2000
Messages: 18 Registered: April 2005
|
Junior Member |
|
|
Hi William,
First of all, many many thx for your promt reply.
I've been stuck with this for weeks now. What I wanted to do is as follows:
I have a file which contains ip addresses and mac addresses. The two are separated by a space. For example:
192.168.10.10 00:0D:09:42:38:0C
There are more than one lines like this. I would like to use these values as follows:
ebtables -t nat -A PREROUTING -p IPv4 --ip-src $dip -i eth0 -j dnat --to-destination $dmac
where dip and dmac are the values of the ip addresses and corresponding mac addresses extracted from the file.
I have to use some kind of FOR loop to go through the whole list of ip addrs and their corresponding mac addrs. I'm unable to do it.
Can you help?
Regards,
Visham
|
|
|
Re: awk programming [message #113412 is a reply to message #113411] |
Sat, 02 April 2005 09:57 |
William Robertson
Messages: 1643 Registered: August 2003 Location: London, UK
|
Senior Member |
|
|
In ksh, zsh or (if you must) bash:
while read dip dmac
do
echo IP=${dip} MAC=${dmac}
ebtables -t nat -A PREROUTING -p IPv4 --ip-src ${dip} -i eth0 -j dnat --to-destination ${dmac}
done < test.dat
|
|
|
|