Easy Awk Question 
Author Message
 Easy Awk Question

I have a file that looks like this

Joe_Bloggs:jbloggs:3434

I want to use awk to echo the first field then echo the second
and run a command using the value of the third field but
display the result of the command in the third field

I.e.

awk '{print $1":"$2":" `cat $3`}'

I know the above example doesn't work, can anyone let me know how to do this.

Cheers,
Kev



Sat, 06 Sep 2003 22:52:11 GMT  
 Easy Awk Question

Quote:

> I have a file that looks like this

> Joe_Bloggs:jbloggs:3434

> I want to use awk to echo the first field then echo the second
> and run a command using the value of the third field but
> display the result of the command in the third field

> I.e.

> awk '{print $1":"$2":" `cat $3`}'

> I know the above example doesn't work, can anyone let me know how to do this.

If you're on Solaris, use nawk, not awk. "awk" on Solaris is an old awk
(called "oawk" on most other systems).

nawk '
{  
  Cmd = "cat "$3
  Cmd | getline CmdOutput
  close(Cmd)

  print $1":"$2":"CmdOutput

Quote:
}'

Regards...
                Michael


Sat, 06 Sep 2003 23:51:25 GMT  
 Easy Awk Question


Quote:

>I have a file that looks like this

>Joe_Bloggs:jbloggs:3434

>I want to use awk to echo the first field then echo the second
>and run a command using the value of the third field but
>display the result of the command in the third field

>I.e.

>awk '{print $1":"$2":" `cat $3`}'

>I know the above example doesn't work, can anyone let me know how to do this.

When you say "run a command", do you mean that it in the general sense, or
do you really just want to include the contents of the file named by the 3rd
field?  I.e., should we take the "cat" above literally?

Or, is your file format really:

        Joe_Bloggs:jbloggs:ls foo.bar

In the first case, I'd do:

BEGIN { OFS=FS=":" }
{
getline t < $3;close($3)
print $1,$2,t

Quote:
}

In the second case, I'd do:

BEGIN { OFS=FS=":" }
{
$3 | getline t;close($3)
print $1,$2,t

Quote:
}

Note that in both cases, I assume a single line of input from $3 - the
generalization of this is left as an exercize for the reader...


Sun, 07 Sep 2003 00:25:52 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Easy Awk question

2. HELP, EASY EASY QUESTION

3. HELP, EASY EASY QUESTION

4. Shell vars into Awk executable script not so easy

5. 31 bit vs 24 bit - two easy questions

6. easy question

7. Pretty easy (I think) shell script question

8. Quick, easy newbie question

9. Easy Question

10. Easy question about Date Class

 

 
Powered by phpBB® Forum Software