Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk syntax error 2

Status
Not open for further replies.

Duane61

Programmer
Jun 4, 2003
19
0
0
US
Okay, I bet this is easy, but can't seem to pull it out...

I want to do a for loop substitution in an awk line, sorta like this:

for i in 1 2 3 4 5 6 7 8 9
do
user=`finger $LOGNAME | awk '{print ${@}}'`
echo "Hello $user"
done


If I put a number in for {@}, I get that field, but I just wanted a quick trick to print out all of the fields, so I don't have to count (& recount) to pull the field I need.

As it is, I get awk: syntax error near line 1
awk: illegal statement near line 1
awk: bailing out near line 1
Hello
...repeat...
appreciate the help,
Duane
 
I don't quite understand what you're trying to do here and what's your particular output of 'finger' is.

Could you provide a sample output of 'finger' AND a desired output, pls!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
One way to loop in awk is

awk '{for (i=1;i<10;i++) print $i}'

or to loop through all fields

awk '{for (i=1;i<=NF;i++) print $i}'


Greg.
 
finger $LOGNAME results in:

login name: dawa In real life: David Wilson x 4223
Directory: /home/dawa Shell: /usr/bin/csh
On since Jul 2 11:13:50 on pts/55 from 191.x.xxx.xx:0.0
40 seconds idle time
No unread mail
No plan.
- - - - - - - - - - - - -
The desired output is simply to recursively print each field so that I can see what is in each field. That way when I need to extract information from a certain field it won't have to be count/trial-n-error.

as an example: user=`finger dawa | awk '{print $7}' yields: David pts/55

and I desire just &quot;David&quot;
- - - - - - - - - - - - - - - - - - - - - -
Greg:
Substituting awk '{for (i=1;i<=NF;i++) print $i}'
I get:
Login
Login
Login
Login
Login
Login
Login
Login
Directory:
Directory:
Directory:
Directory:
On
On
On
On
On
On
On
On
31
31
31
31
No
No
No
No
=================
 
something like this with debugging printf?

finger dawa | nawk '{for(i=1; i <=NF; i++) printf(&quot;record->[%d] fieldN->[%d] fieldV->[%s]\n&quot;, FNR, i, $i)}'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Users 1st name is always the 7th field on the 1st line on the finger output. So

finger $LOGNAME|head -1|awk '{print $7}'

should always work.

Greg.
 
Thanks to you both...
I had used finger $LOGNAME|awk '{print $7}' | head -1, but I was hoping to add a tool to the library to identify/extract various fields without having to look it up or remember it.
vlad's debugging print fits that need! :)

you guys are both great -
Duane
 
Or just do it all with (g)awk...

Code:
function parray(arr,cnt) {
i=0

     while (i < cnt) {
           printf(&quot;%s\n&quot;,arr[i])
           i++
     }
} 

BEGIN {
      if (ARGC < 2) {
          printf &quot;You must specify a username(1) and host address(2) to connect to...\n&quot;
          exit
      }
      
      #socket
      x=0
      clientsock = &quot;/inet/tcp/0/&quot;ARGV[2]&quot;/79&quot;
      nstring = ARGV[1]&quot;@&quot;ARGV[2]
      #open socket
      print nstring |& clientsock

      while ((clientsock |& getline var[x]) > 0) {
              x++
             printf &quot;read %d records...\n&quot;, x
      }

      close(clientsock)
      parray(var,x)
}





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top