![]() |
The name of script itself
I am experienced with awk, I use it for data manipulation. My work is to
make many scripts which each makes some little corrections in data. Each script writes its sign into data (in special data field in data). I must write the name of script into the source to print it, but I would like to get the name of the script from a variable and have it reliably and with no work. I did not find no way in manual. ARGV[0] is 'awk' ARGV[1] is name of datafile. I run awk with "awk -f script.awk dataXX". Thanks for the answer. Marek |
The name of script itself
(Using terminology that should be familiar to clc readers)
The answer is version and platform specific and cannot be answered in a general way. (Putting this a bit more kindly) Please tell us what platform and version of AWK you are using. |
The name of script itself
Yes, that's the way it works. (The reason *may* be that the argument
order would be inconsistent if executing a program in-line.) As far as I can tell you need some kludge. The easiest is to assign the name of the awk program from the shell environment to an awk variable awk -v scriptname=script.awk -f script.awk dataXX If you have multiple scripts, as you say, you may have some shell code like... for awkprog in script_1.awk script_2.awk ... script_N.awk do awk -v scriptname="$awkprog" -f "$awkprog" dataXX done ....and need not define the awk program names twice. Janis |
The name of script itself
It is Fedora Linux 3.x, GNU Awk 3.1.3
Marek |
The name of script itself
Well, then you are in luck!
Try this: BEGIN { getline t < "/proc/self/cmdline";split(t,T,"\0") print "My name is:",T[3] } Put this in a file and run it as: gawk -f myfile |
The name of script itself
:-)
dirty hack, but working. I consider using it. Thanks Marek |
The name of script itself
As Kenny said, any solution will be platform-specific so a "dirty hack"
is all you're going to get. The only alternative that might be more platform idependent in some ways if you can guarantee the version of "ps" you have and apply a bunch of other caveats would be to do something like this: BEGIN{ while ("ps" | getline cmd > 0) print cmd } using the right "ps" arguments to display the full command plus it's arguments, and parse the output. There may be a better command than "ps". If you're not sure what the best shell command is for the job, follow up at comp.unix.shell. Ed. |
The name of script itself
ps is notoriously unportable. Yes, I know it is standardized, but that
helps more in theory than in practice. The funny thing is that my first reaction to this thread was as detailed below, but then just as I was about to post, I remembered about the "cmdline" file. I figured that would be easier for the novice to use. In fact, using "cmdline" pretty much binds you to Linux (note that most modern Unixes have some sort of implementation of /proc, but that details vary greatly [just like ps]), while the solution detailed below should be portable to any platform that has an internet connection (so can get the GAWK source code) and a C compiler (and a usable editor...) Anyway, some time ago, I had need for something like this and so I added a feature to GAWK to create an array called orig_ARGV that contained the original argv array, before GAWK had done any processing on it. The change was quite simple; added about 10 lines to main.c, and recompiled. The trick is figuring out exactly where to put it (you have to look at the sequence of events and place it accordingly). Unfortunately, I lost the sources for this in a hard disk crash, and have not had need to re-do it since then. So, you'll have to figure it out on your own... |
The name of script itself
You could use "history" and extract the script name from the last command.
$ cat q.awk NR==FNR {script=$6} NR!=FNR && FNR==1 {print script} #etc $ $ history | gawk -f q.awk - q.dat q.awk $ $ history | tail -3 1064 cat q.awk 1065 history | gawk -f q.awk - q.dat 1066 history | tail -3 $ The script name is the 6th column in the line " 1065 history | gawk -f q.awk - q.dat" Hope this helps. Alan Linton |
The name of script itself
Nice, but not usable for me. I run the awk from shell script, so I have
that script in history, not the awk command. Marek |