want to pass parameters to awk script from shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting want to pass parameters to awk script from shell script
# 1  
Old 11-02-2010
want to pass parameters to awk script from shell script

Hello,

I have this awk script that I want to execute by passing parameters through a shell script.

I'm a little confused. This awk script removes duplicates from an input file.

Ok, so I have a .sh file called rem_dups.sh

#!/usr/bin/sh
#---------------------------------------------------------------------
# Program ....... rem_dups.sh
# Function ...... removes duplicates from input file
# Developer ..... script_op2a
# Date .......... November 2 2010
# Parameters .... $1 = Position of Key column in input file (Required)
$2 = Unix Script directory (Required)
# $3 = Input file name of file to remove duplicates from (Required)

awk '{FS="";split($NF,a,"_"); key=$pos;site=a[3];keysite=key "_" site;
if (b[keysite]<=a[4]a[5]) {b[keysite]=a[4]a[5];c[keysite]=$0;}}
END{for (i in b) print c[i]}' $dir $filename

It needs to have 3 variable parameters.

$pos (this value needs to be the awk field like $1 $2 $3 $4 or whatever)
$dir (this needs to specify the directory of the input file
$filename (this is the name of the input file)

I want to execute it from command line like:

rem_dups.sh 1 /home/scriptop/input_file_dups.txt > output_file_no_dups.txt
# 2  
Old 11-02-2010
Use the -v option ... short example :
Code:
awk -vkey="$pos" '{print key}' file

# 3  
Old 11-02-2010
ok so I modified my shell script like this:
-------------------------------------------
#!/usr/bin/sh
#---------------------------------------------------------------------
# Program ....... dups_filter.sh

pos="$1"
filename="$2"


awk -v key="$pos" -v infile="$filename" '{FS="";split($NF,a,"_"); site=a[3];keysite=key "_" site;
if (b[keysite]<=a[4]a[5]) {b[keysite]=a[4]a[5];c[keysite]=$0;}}
END{for (i in b) print c[i]}' infile
#end of .sh program
---------------------------------------------------------------

I have the .sh script in the same directory as the infile dups.txt.

I execute from the command line like:
./rem_dups.sh 1 dups.txt > no_dups.txt

and I get the error
awk: Cannot find or open file infile.
The source line number is 3.

Could somebody help me with this confusion?

Last edited by script_op2a; 11-02-2010 at 06:33 PM..
# 4  
Old 11-02-2010
Please post a representative sample of the input you have and output you expect.
Note that if you want to remove duplicate lines , you might want to use the uniq command
# 5  
Old 11-02-2010
Hello thank you,

I made a clearer post here:

https://www.unix.com/shell-programmin...#post302468464
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from awk script to shell?

Hi, Please need to print the Rej variable outsite the awk script which is given below...please advised how to achieve it. #!/bin/bash echo "Enter DMU Pipe delimited File name for the Feed to be validated" read DMU_File echo "Enter Pre-DMU File name for the Feed" read Predum_file ... (3 Replies)
Discussion started by: pelethangjam
3 Replies

2. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

3. Post Here to Contact Site Administrators and Moderators

Unable to pass shell script parameter value to awk command in side the same script

Variable I have in my shell script diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new I could see value of $id is not passing to the awk... (0 Replies)
Discussion started by: Ashunayak
0 Replies

4. Shell Programming and Scripting

pass shell parameters to awk does not work

Why does this work for myfile in `find . -name "R*VER" -mtime +1` do SHELLVAR=`grep ^err $myfile || echo "No error"` ECHO $SHELLVAR done and outputs No error err ->BIST Login Fail 3922 err No error err ->IR Remote Key 1 3310 err But... (2 Replies)
Discussion started by: alan
2 Replies

5. Shell Programming and Scripting

call another shell script and pass parameters to that shell script

Hi, I basically have 2 shell scripts. One is a shell script will get the variable value from the user. The variable is nothing but the IP of the remote system. Another shell script is a script that does the job of connecting to the remote system using ssh. This uses a expect utility in turn. ... (2 Replies)
Discussion started by: sunrexstar
2 Replies

6. Shell Programming and Scripting

need to pass parameters to working and tested awk script

I have a working and tested AWK script that removes duplicates from an input file and generates an output file without the duplicates. I had help from my other post to develop it: ... (3 Replies)
Discussion started by: script_op2a
3 Replies

7. Shell Programming and Scripting

pass variable from awk to shell script

Hello Experts, Actually I was searching for a solution here in this forum , but didn't get what exactly I want . Is this possible to do in awk ? I am trying to do some thing like below in ksh script . Upto my knowledge I can pass shell script to awk with "-v " option. But I... (3 Replies)
Discussion started by: user_prady
3 Replies

8. Shell Programming and Scripting

Is it possible to pass variable from awk to shell script

Hello experts, can I return a value from gawk to a shell script ? My script as follows, #Here I want the num value to shell script so that I can use later gawk ' { split($0,num,","); print num }' gawk -v no=$number '{print no}' file1 ... (3 Replies)
Discussion started by: user_prady
3 Replies

9. UNIX for Dummies Questions & Answers

How to pass two or more parameters to the main in shell script

Hey Guys from the below script what I understood is we are sending the the first parameter as input to the main (){} file main > $LOGFILE 2>&1 but can we send two or three parameter as input to this main file as main > $LOGFILE 2>&1 2>&2 like this Can any one plz help I need to writ a... (0 Replies)
Discussion started by: pinky
0 Replies

10. UNIX for Dummies Questions & Answers

How to pass Shell script variable to awk

Hi, I have a shell script with an ambedded awk script. i need to pass a script variable to the awk script. Please help. Thanks in advance Himani (3 Replies)
Discussion started by: HIMANI
3 Replies
Login or Register to Ask a Question