Pass command line arguments to awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass command line arguments to awk
# 1  
Old 12-15-2009
Pass command line arguments to awk

I am trying to pass max as a sommand line argument when I call awk.

Made the modification in the BEGIN but it is not working

I'm getting an error as below:

Code:
awk: txsrx.awk:82: (FILENAME=jcd.tx FNR=4161) fatal: cannot open file `40' for reading (No such file or directory)

Somehow it is reading 40 and interpreting it as a file.

Code:
# 1: Absolute value of a number
function abs(val) {
    return val > 0 ? val : -val
}

# 2: Read file twice, set maximum S-R distance
BEGIN {
#    ARGV[ARGC++] = ARGV[ARGC-1]
    print "0." ARGV[0]
    print "1." ARGV[1]
    print "2." ARGV[2]
    print ""
    ARGV[ARGC++] = ARGV[2]
    ARGV[ARGC-2] = ARGV[ARGC-3]
    max = 40
    print ARGC
    print "0." ARGV[0]
    print "1." ARGV[1]
    print "2." ARGV[2]
    print "3." ARGV[3]
}

# 3: First time round, store source location
FNR == NR {
    />/ && idx[FNR] = ++i
    $2 || val[i] = $1
    next
}

# 4: Second time round, get source location
FNR in idx {
    v = val[idx[FNR]]
}

# 5: Calculate S-R distance
{ !/>/ && dist = abs( $1 - v ) }

# 6: Print only if S-R distance less than max, create third column
# For the print statement can also use { OFS = ""; print $0, $2; OFS = " " }
/>/ || dist < max && NF == 2 { sub(/[ \t]+$/, ""); print }

# 2  
Old 12-15-2009
Hi
Can you show us how you are passing 40 to awk? because it is seems that you are not writing it correctly and awk is considering the argument as a filename
# 3  
Old 12-15-2009
I'm doing like below

Code:
awk -f txsrx.awk jcd.tx 40 > temp

Did not delete the arg containing the 40 so it is tying to open it as a file.

Doing it like this seems to work

Code:
BEGIN {
#    ARGV[ARGC++] = ARGV[ARGC-1]
    print "0." ARGV[0]
    print "1." ARGV[1]
    print "2." ARGV[2]
    print ""
    max = ARGV[ARGC-1]
    # Delete args so awk does not open them as files
    ARGV[ARGC-1] = ARGV[1]
    print ARGC
    print "0." ARGV[0]
    print "1." ARGV[1]
    print "2." ARGV[2]
    print "3." ARGV[3]
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read file lines and pass line values as arguments.

Dears, Need help to implement below requirement A file (detail.txt)contain : 1st column: Stream 2nd column: PathAddress 3rd column: Counterlimit 4th column: TransactionDateColumn 5th column: DateType 6th column: SleepValue 7th column: Status Need to write a... (1 Reply)
Discussion started by: sadique.manzar
1 Replies

2. Shell Programming and Scripting

awk command line arguments not taking

# more minusf.awk #!/bin/awk -f BEGIN { FS=":"; } { if ( $2 == "" ) { print $1 ": no password!"; } } # ./minusf.awk aa aa aa aa awk: can't open aa (6 Replies)
Discussion started by: sri.phani
6 Replies

3. Shell Programming and Scripting

How to pass command line arguments to awk program?

#!/bin/awk -f BEGIN { FS=":"; } { if ( $7 == "" ) { print $1 ": no password!"; } } I want to execute this program for a particular user to check for his password from the file /etc/passwd (as the input file) and the user details to be given... (1 Reply)
Discussion started by: sri.phani
1 Replies

4. Shell Programming and Scripting

Need to pass shell arguments into Nawk/awk

Hi, I am in critical need of help, Thanks a ton for your help. I need to know how to pass the shell argument into nawk code in AIX. so that my file gets passed into that awk script and it can execute it part. To be detail, i have more than 100 files and in those files a particular field... (6 Replies)
Discussion started by: Selva_2507
6 Replies

5. Shell Programming and Scripting

How can I pass arguments to system command in a awk script?

Hi I need your help, please How can I pass arguments to system command in a awk script?... for example: byte=substr(cadena,pos,2); system("grep -n byte mapeo.txt"); Does it exist a way? Thanks for advance. (4 Replies)
Discussion started by: solaris21
4 Replies

6. Shell Programming and Scripting

Pass awk field to a command line executed within awk

Hi, I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date). All my attempts failed this far. Here's an example. It works fine with timestamp hard-codded into the command echo "1381653229 something" |awk 'BEGIN{cmd="date -d... (4 Replies)
Discussion started by: tuxer
4 Replies

7. Programming

How to pass the command line arguments to the shell script in c language?

hi, I am new in the shell script, and c programming with linux. I am looking to pass the arguments in c program that should be executed by the shell script. e.g. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { int i; for (i=1;i<argc; i++) { ... (2 Replies)
Discussion started by: sharlin
2 Replies

8. Shell Programming and Scripting

How can i pass 2 command line arguments with a range between them

Hi, I am writting a script. i am not sure what i am trying to do is possible or not. thats why asking the best of the best. the script i want to write will recieve as input parameters 2 different options. as in MODE 1 -- start_date / end_date (2 dates has 2 go at a time MODE 2... (2 Replies)
Discussion started by: dazdseg
2 Replies

9. Shell Programming and Scripting

How do we pass multiple arguments into awk

How do we pass multiple arguments into awk : name=john age=12 now i have to pass both age and name into awk.. how to do it? like : awk -v var=... (4 Replies)
Discussion started by: abhinav192
4 Replies

10. Shell Programming and Scripting

using command line arguments as columns for pattern matching using awk

Hi, I wish to use a column, as inputted by a user from command line, for pattern matching. awk file: { if($1 ~ /^8/) { print $0> "temp2.csv" } } something like this, but i want '$1' to be any column as selected by the user from command line. ... (1 Reply)
Discussion started by: invinclible0009
1 Replies
Login or Register to Ask a Question