help required with shell script

Hi,

My input file as follws
$ cat 1.txt
-------

a aa aaa 11 
b bb bbb 22

I am able to extract first and last column of a given line as follows.

$ nawk '{print $1}' FS= RS= 1.txt | awk '{ $NF = ""; print }'

a

$ nawk '{print $1}' FS= RS= 1.txt | awk '{ print $NF}'

11

however, the same is not working by using shell script. My script as follows.

$ cat 11.ksh
-----

#!/usr/bin/ksh
for i in 1 2
do
 nawk '{print $$i}' FS= RS= 1.txt | awk '{ $NF = ""; print }'
 nawk '{print $$i}' FS= RS= 1.txt | awk '{print $NF}'
done

--

I am getting the below error.
--

$ sh 11.ksh
awk: 0602-562 Field $() is not correct.
 The input line number is 1. The file is 1.txt.
 The source line number is 1.
awk: 0602-562 Field $() is not correct.
 The input line number is 1. The file is 1.txt.
 The source line number is 1.
awk: 0602-562 Field $() is not correct.
 The input line number is 1. The file is 1.txt.
 The source line number is 1.
awk: 0602-562 Field $() is not correct.
 The input line number is 1. The file is 1.txt.
 The source line number is 1.

---
Possibly, the error is because of '$$i' in shell script. Any help to fix this much appreciated.

Thansk in advance.

Hi,

Try like this,

for i in 1 2
do
awk -v fld="$i" '{print $fld;}' file
done

I dont about your exact requirement. so i cant comment on your code but you can use the below to get first and last field.

awk  '{print $1,$NF;}' file

Cheers,
Ranga:)

Thanks Ranga for your quick reply.

I would like to read first and last column of a given line (line by line not the entire file) and store in a table.

Can you help to match this requirement?

---------- Post updated at 02:45 AM ---------- Previous update was at 02:39 AM ----------

It is working fine now. I replaced nawk with you code. Thanks again.

Can you give me some sample input and expected output ?
In a single file you are going to read selected line( like 5th, 6th and 20th lines) ?

while read line
do
   echo $line | awk '{print $1, $NR}'
done<filename