Nope, it won't work.
The primes within the AWK program are illegal, you would have to use quotes instead. Unfortunately the $i variable also belongs to Bash.
Here is a fix of the code that should be put in a file and run from the command line as described at the end of this article.
( for i in $*
do
awk -v abc=$i '
BEGIN{ print abc }
'
done ) > temp
1. The brackets tell bash that the process should be run in a shell of it's own, so $* represents all the arguments on the command line.
2. The -v flag defines and initializes the awk variable with the text that follows; or in this case the expansion of $i. This is so because at this point we are calling the AWK script so are still in a Bourn or Bash shell.
3. The primes delimit the bounds of the AWK program proper.
4. BEGIN is an AWK reserved pattern, the action is delimited by the braces.
5. Issues that may be of interest: printing primes or semi-colons in awk require them to be escaped with a backslash or expressed as octal literals.
Code layout on multiple lines is merely my preference and makes it easier, I think, to follow the structure of things. The indentation of the three lines between do and done is lost in the forum. I won't try to fix it as it will be easier to cut and paste as it is.
There are of course many other ways to do the same thing depending on your input requirements.
The code submitted here is in a file called aaa.sh (and chmodded to make it executable) can be called from wherever you write it as:
./aaa.sh now is the time for all good men to come to the aid of the party
If the directory is in your current PATH then it won't need the ./
The content of temp will be:
now
is
the
. . .
of
the
party
One downside is if you wish your data items to contain a space. No amount of quoting the data will allow it. You could of course substitute such spaces with say an underscore and fix it back with another line in the AWK script.
Why not replace:
> temp
with
| sort > temp
adding whatever sort options you wish and temp will contain the list sorted just as you wish.
I hope this has helped. Good luck and good scripting.
PAix
Last edited by PAix; 10-15-2007 at 07:10 PM.
Reason: For reader sanity - assume nothing, explain all..
|