Need to pass shell arguments into Nawk/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to pass shell arguments into Nawk/awk
# 1  
Old 04-05-2014
IBM 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 need t o encrypt. i have a nawk script which does that part, I will share the layout here.

Code:
nawk -F"<delimiter>"'{
if (FILENAME="Mapping_file")
{
map[$1]=$2
}
else of (FILENAME = "file1")
{
k=1
while (k <= "Need to pass second argument from shell")
{
<code to encrypt>
}
}
} Mapping_file file1

I am calling this script (a.ksh) from another main script and need to pass two arguemnts (file1 in above code --> file name should the first arguemnt) and second argument in while statement

My script execution should be like this

ksh a.ksh <filename> <length> > OUTPUTFILE

finally my code should like this

Code:
nawk -F"<delimiter>"'{
if (FILENAME="Mapping_file")
{
map[$1]=$2
}
else of (FILENAME = "<filename>")
{
k=1
while (k <= "<length>")
{
<code to encrypt>
}
}
} Mapping_file <filename>

Your help on early is much appreciated. Please help me as I m in desperate need of this solution.
# 2  
Old 04-05-2014
This is just an example to start code
Code:
#!/bin/ksh


nawk -v args="$*" '
               BEGIN { 

                      n=split(args,Array," ")

		      first_argument = Array[1]
		      last_argument  = Array[n]

                      # process your arguments here  
                     }
                     {

                      # put your remaing code here

                     }

                  ' $1 > ${@: -1}

---------- Post updated at 11:40 PM ---------- Previous update was at 11:34 PM ----------

Note : ${@: -1} is last argument, may not work with sh use ksh or bash
# 3  
Old 04-05-2014
You could also use the ARGC and ARGV builtin variables:
Code:
awk 'BEGIN {for (i=0; i<ARGC; i++) print ARGV[i]}' Mapping_File filename length
awk
Mapping_File
filename
length

# 4  
Old 04-05-2014
Assuming there's one 'Mapping_file'... Iterate through all the files to be processed with the below 'template' substituting 'file1'...
Something to start with....

Code:
nawk -F"<delimiter>" -v cnt="${myShellVarNameGoesHere}" '
FNR==NR { map[$1]=$2;next } 
{
   for(k=1;k<=cnt;k++) {
      <code to encrypt> 
   } 
}' Mapping_file file1


Last edited by vgersh99; 04-05-2014 at 02:25 PM..
# 5  
Old 04-05-2014
Linux Need to pass shell arguments into Nawk/awk

Thanks you all for quick resposne. sorry I think i m not clear with my initial query.

Scripts should run like this ": ksh encrypt.ksh <input file> <Length> > Outputfile

Inputfile = different files like file1, file2 etc..
length = varies from file 1 to file2 etc..

My current script:

Code:
nawk -F"<delimiter"'{
if (FILENAME=="MAPPINGS.TXT")
{
map{$1]=$2
}
if (FILENAME=="file1 or file2")
{
while (i <= $length(parameter from script) )
{
print i,
<using map, ecnrypt the values using substr funciton to a certain length>
}
i++;
print i; (remaining records in that line)
}' MAPPINGS.TXT file1 or file2 (1st shell argument)

MAPPINGS.TXT == a single file used for all files.
file1 or file2 = Shell script first parameter
length = Shell script second parameter

NOTE: My encrypt.ksh script only consists of nawk command, I need to pass shell arguments into that nawk script.

your help is much appreciated as I m in desperate need to complete this on priority.
# 6  
Old 04-05-2014
If so desperate, why don't you try the examples and hints given and adapt if needed? Like
Code:
                 if (FILENAME==ARGV[2])         {while (i <= ARGV[3] )

Unfortunately, the structures of your shell scripts and calls therein are not clear to me. Above will work if awk is called like awk '...' MAPFILE $1 $2 from within encrypt.ksh.
# 7  
Old 04-06-2014
Code:
ksh encrypt.ksh len=64 infile1 infile2 > Outputfile

Then the ksh script is
Code:
nawk '
...
' "$@"

And the nawk has the variable len defined.

Last edited by MadeInGermany; 04-06-2014 at 04:42 AM.. Reason: order changed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to pass arguments while running a Shell Script

Shell Script Gurus, I am writing a shell script which needs User ID's to pass as an Arguments in command line while executing. Can this be doable? I've never done this, If you give a sample script that would be helpful, Thanks. (1 Reply)
Discussion started by: shekar777
1 Replies

2. Shell Programming and Scripting

Pass Arguments to Command from Shell Script

Hi all, I am working on a project, in which I have to connect to Bluetooth low energy device. I am able to connect and do data transfer from command line. But I want to do from script Here is my script #!/bin/bash #sudo hcitool -i hci0 lescan sleep 1 sudo hcitool -i hci0 lecc --random... (8 Replies)
Discussion started by: nithin@embdes
8 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

How to pass nawk variable to shell within the same script?

Hi All, I tried googling but so far no luck, can someone tell me how pass the variable value used inside the nawk command to shell. In the below script i get the value of $c (without color: Total Executed: " c ") but the printf which is outside the nawk command doesn't print the value or it... (4 Replies)
Discussion started by: Optimus81
4 Replies

5. Shell Programming and Scripting

Pass parameter to nawk from shell script

I need to parse log files using nawk, but I'm not able to pass script input argument (date) to nawk, for example: ------------ #!/bin/ksh read date nawk -F, '{if($1==date) print $4" "$5}' ------------- Is there a way to pass an argument to nawk from shell script. Many thanks... (8 Replies)
Discussion started by: samer.odeh
8 Replies

6. Shell Programming and Scripting

[C SHELL] How to pass dynamic number of arguments

I have a task. The scenario is like this. I have a operation program (OPR1) , whose function is to simply double the (single)value it receives as input. I have to write a script to operate the OPR1 and save its output in a file. Later, I have to modify the script so as to be able to operate ... (0 Replies)
Discussion started by: animesharma
0 Replies

7. Shell Programming and Scripting

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: awk: txsrx.awk:82: (FILENAME=jcd.tx FNR=4161) fatal: cannot open file `40' for reading (No such file or directory) Somehow it... (2 Replies)
Discussion started by: kristinu
2 Replies

8. 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

9. Shell Programming and Scripting

Need help to pass arguments to shell script

Hi, I have a shell script called ftp.sh which is running continously in background. I tried passing arguments to this script but it did not worked out. Below is ftp.sh script. Please help me case $param in start) sleep_func "300" echo "!ksh $scr_ddir/ftp.sh... (1 Reply)
Discussion started by: bhargav20
1 Replies

10. Shell Programming and Scripting

How to pass arguments to a function in a shell script?

Hi, I have two shell variables $t1 and $t2 which I need to pass to a function in a shell script. The function will do some computation with those two variables and echo the resultant. But I do not know how to pass teh arguments. The function written is f1() {...... ........ } What should... (3 Replies)
Discussion started by: preetikate
3 Replies
Login or Register to Ask a Question