2

What must not be in an AWK script?

I am asking as I don't find any tool to debug an AWK script. I have a script taking a lot of CPU so I need to know if I am doing something terribly wrong with the script.

Just for an example, I keep looking for output of a logfile via 'tail -f filename' till my script gets killed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hari
  • 9,439
  • 27
  • 76
  • 110
  • To get the best help from Stack Overflow, you should post a small piece of code that demonstrates the actual problem you're having. Otherwise, this is a vague question with no real answer. – Greg Hewgill Jul 30 '10 at 22:11
  • My bad, its a little vague but its a huge awk script doing a lot of things. I just wanted to get a sense of in general what should be avoided. I tried googling but couldn't find much useful. – hari Jul 30 '10 at 22:30
  • Both this and your [previous, almost identical, question](http://stackoverflow.com/questions/3358253/does-an-awk-script-take-up-a-lot-of-cpu) are too vague. You'll have to show some code. It's possible that you're doing something inefficiently, but without seeing it, it would only be guesswork. – Dennis Williamson Jul 30 '10 at 23:50
  • Sorry for the confusion. I think I got the answer by @ghostdog74. Thanks all. – hari Jul 31 '10 at 18:40

2 Answers2

2

awkdb has not been updated since 2000. It has some limitations as well according to the web site. If you are using gawk, and look at its man page reference, you can see some options, like --profile, --optimize, --dump-variables, etc. You can try those options. Another option is to use pgawk as indicated in the man page as well.

Generally, if your script is slow in execution, either you have a REALLY large file, or your algorithm is causing the problem. You should at least show the code that you think is hogging up the CPU. Some of the things you should avoid doing if possible, for example

  1. Calling the big file a 2nd (or more files)

    awk '{}' file file

  2. Iterating a file with a while. Loop as you iterate a file

    awk '{while(( getline line<"file2") >0 ) {} )}' file

  3. Storing values into arrays for a big file takes up memory. Try to clear some of the elements (or delete the array) if not in use anymore.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

I have never used it, but there is awkdb. Caveat Emptor.

If you are using gnu awk, recent versions have --profile or pgawk that should at least let you know what is going on at an abstract level.

Vicente Bolea
  • 1,409
  • 16
  • 39
deinst
  • 18,402
  • 3
  • 47
  • 45