Being new to awk I have a really basic question. It just has to be in the archives but it didn't bite me when I went looking for it.
I've written an awk script, placed it in a file, added the "#!/usr/bin/awk -f" at the top of the script and away I go. "% myAwk <inputfile>" gives me exactly what I expect. Now I want to add a command line option to my script to alter its behavior. Something like "% myAwk -opt1 <inputfile>" but awk is interpreting my option as just another input file rather than some internal variable (like: argv[1] ...)
Is what I'm trying to do even possible? If so, what would the awk script look like internally?
- Tom
This gets a bit tricky, especially if you want the awk programme to process a file other than stdin.
Simple case, awk programme will process standard input:
This programme reads and prints the command line arguments, and then prints all of the lines from stdin. All you have to do is interpret the stuff in ARGV. When executing with a file on stdin that has two input lines, this is the output:
The magic here is setting ARGC to 1 in the BEGIN block which causes awk to ignore the rest of the command line. To complicate things, if you want to supply the input file name(s) on the command line, then you'll have to process your parameters and reset ARGV and ARGC accordingly. This is a simple example that accepts a -f value and/or -v parameter(s), and then shifts the file name(s) down:
The command line would be something like:
Personally, I prefer to wrap my awk with a shell script and let it do all of the command line parsing and other error checking. The script then invokes awk with one or more -v var=value options to pass in the desired data.
Hope this gets you going again.
Last edited by agama; 06-29-2011 at 11:33 PM..
Reason: typo correction
Wow! Thanks for the great explanation! It was easy to follow and exactly what I needed. As you point out, it's not as concise as you could do with a shell script but your implementation is still clean and self-contained.
The only problem I had was with "--exec". Though the awk documentation indicates this is exactly what I need for some reason it didn't like that option and kept displaying the usage message. I changed this to "-f" and used the "--" characters to terminate the awk options ... good enough though not ideal.
Thanks again! ;-)
Hi All,
Do we know how to read input file within awk script and send output toanother log file. All this needs to be in awk script, not in command line. I am running this awk through crontab.
Cat my.awk
#!/bin/awk -f
function test(var){
some code}
{
}
END
{
print"test code"
} (5 Replies)
Hi all
I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis.
The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
I have the following code and I am calling it using
./raytrac.bash -u
and getting problems. For some reason opt_usage is still 0.
opt_usage=0
iarg=0
narg=$#
while (($iarg < $narg))
do
(( iarg = $iarg + 1 ))
arg=$argv
usrInputFlag=`echo $arg | awk '/=/ {print 1}; ! /=/... (22 Replies)
I'm reading about debugging aids in bash and have come across the set command. It says in my little book that an addition to typing
set
you can also use them "on the command line when running a script..." and it lists this in a small table:
set -o option Command Line... (5 Replies)
Hi,
I am writing a shell script to build Java options dynamically in a variable array and pass them to java.exe. If an option value contains a space, I cannot find a way to get it interpreted correctly. Here is my example:
#!/bin/bash
JAVA_HOME=/opt/jvm/jre1.5.0_18
JAVA_OPTS=("-Xms256m... (4 Replies)
Hello everyone,
I have a perl script which takes various command line options from user like :
test.pl -i <input_file> -o <output_file> -d <value> -c <value>
Now I have multiple input files in a directory:
<input_file_1>
<input_file_2>
<input_file_3>
<input_file_4>
.....
....
...... (6 Replies)
Hi all,
I want to get options from command line by perl.
usage()
options:
-h Show this help message and exit
-t Name of tester
--timeout Set the timeout
-l ... (1 Reply)
my description from another thread...
here's my code:
#!/bin/bash
IFS=$'\n'
function OutputName() {
input=$1
echo $input
input=`echo "$input" | sed -e 's/.//'`
input=`echo "$input".avi`
output_name=$input
}
if ]; then
echo... (5 Replies)
I am looking to populate an (associated) array with a command line argument.
The command line would look something like this:
alert -action test -priority '10' -module test_module . . .
The associated array would look like this after the data is read in
flag=(action=test
priority=10... (1 Reply)
Can someone please tell me how to modify/add to this code so that it recognizes UNIX command options (all beginning with "-") and executes the command with options?
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv)
{
int i;
system("stty -echo");
... (8 Replies)