Pass Parameters to awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass Parameters to awk command
# 1  
Old 01-07-2013
Pass Parameters to awk command

I need to pass values at runtime for the below awk command where

l is the length and partial.txt is the file name.

Code:
 
awk -v l=285 '{s="%-"l"s\n";printf(s,$0);}' partial.txt > temp1.txt;

# 2  
Old 01-07-2013
Code:
FILE="partial.txt"
LEN=285

echo | awk -v l="$LEN" -v file="$FILE" '{s="%-"l"s\n"; while(( getline line < file  ) > 0 ) { print s,line; } } '

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-07-2013
The parameters i need to pass are

FILE = <filepath>/partial.txt

When i replace the filepath it gives error as "cannot open the file for reading"
# 4  
Old 01-07-2013
It should work! Make sure you do not have any blank space between assignment operator = and wrap the string variable value in double quotes " to avoid any unexpected behavior.
# 5  
Old 01-07-2013
Code:
 
#!/bin/ksh
FILE=$1
LEN=$2
echo | awk -v l="$LEN" -v file="$FILE" '{s="%-"l"s\n";printf(s,$0);}' file > temp1.txt;

I execte the script using the below command

Code:
 
sh -x Script.sh /home/dhandal/partial.txt 285

Let me know whether i am executing right.
# 6  
Old 01-07-2013
Rather try
Code:
awk -v l="$LEN"  '{s="%-"l"s\n";printf(s,$0);}' $FILE > temp1.txt;

or
Code:
awk -v l="$2"  '{s="%-"l"s\n";printf(s,$0);}' $1 > temp1.txt;


Last edited by RudiC; 01-07-2013 at 02:40 AM.. Reason: Typo
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Cannot pass rsh and awk command into a variable

Hello, I'm having some issues getting a home dir from a remote server passed to a variable. Here is what I have so far: rsh server "(ls -ld /home*/user | awk '{print \$9}')" /home3/userThat works fine and brings back what I need. But when I try to add it to a variable it goes all... (3 Replies)
Discussion started by: elcounto
3 Replies

3. Shell Programming and Scripting

Bash- Command run from script does not pass full parameters with spaces inside

There's a JavaScript file that I call from command line (there's a framework) like so: ./RunDiag.js param1:'string one here' param2:'string two here' I have a shell script where I invoke the above command. I can run it in a script as simple as this #!/bin/bash stuff="./RunDiag.js... (4 Replies)
Discussion started by: AcerAspirant
4 Replies

4. Emergency UNIX and Linux Support

Pass two parameters

Hi I have a batch file aaa.exe which needs two input parameters: Usually the command's format likes aaa 555 10000 But I want to use parameters to do it. aaa $1 $2 These two parameters come from a text file list.txt 41800497 41801375 41814783 41816135 41814930 41816135 41819987 41820843... (4 Replies)
Discussion started by: zhshqzyc
4 Replies

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

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

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... (4 Replies)
Discussion started by: script_op2a
4 Replies

8. Shell Programming and Scripting

Pass parameters to function

Hi, for example I have this function: function get_param () { test=echo "some string" test2=echo "someother string" } I want to call this function and get test or test2 result, how do I do that ? Thank you (2 Replies)
Discussion started by: ktm
2 Replies

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

10. Shell Programming and Scripting

How to pass parameters to an awk file?

I have an awk file where I need to pass a filename and a value as a parameter from a sh script. I need to know how to pass those values in the sh script and how to use the same in the awk file. Thanks in advance!!! Geetha (3 Replies)
Discussion started by: iamgeethuj
3 Replies
Login or Register to Ask a Question