| |
 |
|
| Linux Forum Index » General Linux Discussion » Can't get awk to read a variable... |
|
Page 1 of 1 |
|
| Author |
Message |
| lrhorer... |
Posted: Wed Aug 05, 2009 5:08 am |
|
|
|
Guest
|
I have a script which runs against a file with a variable number of entries
on each line and variable amounts of white space. Each entry of interest
is preceded by an identifier for the following entry. Thus, a line might
look like:
1/1 First_Entry 0/1 Second_Entry 1/3 Third_Entry 2/2 Fourth_Entry
In each case, I will need to compare each field in the line to the entry ID
I want to retrieve, find its position in the line, and then retrieve the
next entry after the ID of interest. I tried the following, but awk
complains about the variable in its print statement:
Port="1/1"
Name=""
Num=0
until [ $Name = $Port ];
do
Num=$((Num + 1))
Pval="\$$Num"
Name=`grep $Port <filename> | awk '{print $Pval}'`;
done
Awk's complaint is:
awk: Field $() is not correct. |
|
|
| Back to top |
|
|
|
| Ron AF Greve... |
Posted: Thu Aug 06, 2009 5:49 pm |
|
|
|
Guest
|
Hi,
You have to specify it outside the quotes.
ronafgreve at (no spam) informationsuperhighway:~$ echo ron|nawk -v ron=10 ' { print
ron; }'
10
ronafgreve at (no spam) informationsuperhighway:~$
"lrhorer" <lrhorer at (no spam) satx.rr.com> wrote in message
news:7eGdncA_epiI9eTXnZ2dnUVZ_r1i4p2d at (no spam) giganews.com...
|
| I have a script which runs against a file with a variable number of
entries
| on each line and variable amounts of white space. Each entry of interest
| is preceded by an identifier for the following entry. Thus, a line might
| look like:
|
| 1/1 First_Entry 0/1 Second_Entry 1/3 Third_Entry 2/2
Fourth_Entry
|
| In each case, I will need to compare each field in the line to the
entry ID
| I want to retrieve, find its position in the line, and then retrieve the
| next entry after the ID of interest. I tried the following, but awk
| complains about the variable in its print statement:
|
| Port="1/1"
| Name=""
| Num=0
| until [ $Name = $Port ];
| do
| Num=$((Num + 1))
| Pval="\$$Num"
| Name=`grep $Port <filename> | awk '{print $Pval}'`;
| done
|
| Awk's complaint is:
|
| awk: Field $() is not correct.
--
Regards, Ron AF Greve
http://informationsuperhighway.eu |
|
|
| Back to top |
|
|
|
|
|
All times are GMT - 5 Hours
The time now is Sat Nov 21, 2009 8:20 pm
|
|