Results 1 to 5 of 5
I'm writing a script that takes 1 argument and searches the standard input stream for it.
So my script is getRecords.awk
#!/bin/awk -f
BEGIN {
search = ARGV[3]
print(search)
delete ...
- 10-16-2009 #1Just Joined!
- Join Date
- Oct 2009
- Posts
- 10
awk script taking arguments from command line
I'm writing a script that takes 1 argument and searches the standard input stream for it.
So my script is getRecords.awk
#!/bin/awk -f
BEGIN {
search = ARGV[3]
print(search)
delete ARGV[3]
}
/ARGV[3]/{print $0}
So I use it by doing
cat sometxtfile | getRecords.awk charlie
and it should print out every line with the word charlie.
my problem is that awk always interprets charlie as a file and then exits when it can't open it.
that's why I put in the delete statement but it doesn't help.
I know this would be easier to accomplish with grep, but this is part of something bigger so I'm writing an awk script.Last edited by blackbox111; 10-16-2009 at 07:23 PM. Reason: changed title
- 10-17-2009 #2
I think it's that "-f " argument that causes the trouble. It tells awk to look for a file containing the script that it is supposed to run. Why do you use that argument?
"I'm just a little old lady; don't try to dazzle me with jargon!"
- 10-17-2009 #3
Try this:
Code:#!/bin/awk -f BEGIN { search = ARGV[1] print "search is:", search delete ARGV[1] } $0 ~ search
- 10-17-2009 #4Just Joined!
- Join Date
- Oct 2009
- Posts
- 10
thanks it works. So deleting argv[1] makes it so that awk doesn't try to open up the argument thinking it's a file? at what point in time does it try to do this? for example, by the time the delete statement is reached
doesn't the -f option on the command line tell awk to expect a file?
one more question when: when using fields (e.g. $0) how does it know whether it's referring to the input (in this case whatever I pipe to it when I got cat words | ...) or from the argument?
- 10-18-2009 #5
Exactly.
awk process the BEGIN block before reading any input. That's why you can actually preprocess the arguments in the BEGIN block. In the BEGIN and END blocks awk do not try to open it's arguments as a named files, it does it in the blocks that do not try to match the spetial patterns BEGIN and END.at what point in time does it try to do this? for example, by the time the delete statement is reached
You need the -f in the shebang line in order to make awk parse the rest of the file. From the man pages:doesn't the -f option on the command line tell awk to expect a file?
-f program-file
--file program-file
Read the AWK program source from the file program-file, instead
of from the first command line argument. Multiple -f (or
--file) options may be used.The source (STDIN rather than a named file) does not make any difference for awk's record processing.one more question when: when using fields (e.g. $0) how does it know whether it's referring to the input (in this case whatever I pipe to it when I got cat words | ...) or from the argument?
If you need to, consider the following:Code:% print a b | awk '{ print $2 }' - <(print c d) b d
Code:% cat infile c d % print a b | awk '{ print "Input from:", FILENAME ~ "-" ? "STDIN" : "named file", $2 }' - infile Input from: STDIN b Input from: named file d


Reply With Quote
