tags:

views:

247

answers:

2

I have an awk script that I have defined thusly:

#!/usr/bin/env awk
BEGIN { if (!len) len = 1; end = start + len }
{ for (i = start; i < end; i++) { print $1 } }

I have saved it as columns and chmod +x'd it. I want invoke it so that start and end are defined as it traverses over a file. I was thinking this should work:

cat some_file | columns -v start=2

But it doesn't. Help!

+1  A: 

Try using:

#!/usr/bin/awk -f

as an interpreter

agsamek
A: 

What do you mean "it doesn't work"? Other than the fact that I had to add a -f to the shebang, it works fine for me:

#!/usr/bin/env awk -f

The trouble may be that your program behaves exactly the same regardless of the value of start, printing the first field len times.

William Pursell
This does not work portably across different flavors of UNIX: see http://en.wikipedia.org/wiki/Shebang_(Unix)#Portability (i.e. it may work in modern FreeBSD but not in historical BSD nor Linux).
ephemient