Parameter to AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parameter to AWK
# 8  
Old 07-23-2009
if you are using bash, just place your user_text inside of an array.
# 9  
Old 07-23-2009
Array in KSH

Quote:
Originally Posted by RiSk
if you are using bash, just place your user_text inside of an array.
RiSk,

I tried with arrays in KSH.

Below code works fine
Code:
$ set -A x a b c
$ echo ${x[1]}
b
$ echo ${x[0]}
a

But when i try my input text i get errors as below

Try 1
Code:
$ set -A x -parm a=b -parm c=d -parm e=f -parm g=h \
>
ksh: -parm: 0403-010 A specified flag is not valid for this command.

Try 2
Code:
$  set -A x -parm a=b -parm c=d -parm e=f -parm g=h \\
ksh: -parm: 0403-010 A specified flag is not valid for this command.

Try 3
Code:
$ set -A x '-parm a=b -parm c=d -parm e=f -parm g=h \'
ksh: -parm a=b -parm c=d -parm e=f -parm g=h \: 0403-010 A specified flag is not valid for this command.

Any other suggestions?
# 10  
Old 07-23-2009
This is the input file I used from your request:

Code:
-parm a=b -parm c=d -parm e=f -parm g=h \
-parm a=b -parm c=d \
-parm a=b -parm c=d -parm e=f \

This is the code I used to do stuff based on this input:

Code:
#!/bin/ksh
while read -r line
do
  set -- $line
  while [ "$1" != "" ]
  do
    case $1 in
      "a=b") echo "doing a=b stuff" ;;
      "c=d") echo "doing c=d stuff" ;;
      "e=f") echo "doing e=f stuff" ;;
      "g=h") echo "doing g=h stuff" ;;
    esac
    shift
  done
done < /input_file

# 11  
Old 07-23-2009
It's working !!!

peterro,
Thank you very much. It's working Smilie


Others,
Thanks for your suggestions and your valuable time. I appreciate it.
# 12  
Old 07-23-2009
Quote:
Originally Posted by rakeshawasthi
Coming back to original question, how to achieve this by passing variable to awk.
Code:
user_text="first second third fourth"
var=1
echo $user_text | awk -v awk_var="$var" '{print $awk_var}'

---------- Post updated at 09:40 AM ---------- Previous update was at 09:39 AM ----------


$1 will be overwritten...
to avoid that, first store the passed argument and then use set.
in that case:
Code:
 
set $1 first second third fourth
echo $2
   first
echo $5
   fourth

# 13  
Old 07-23-2009
Need your help again...

I used set as below which is working fine.

Code:
$ set -- a b c d
$ echo $1
a
$ echo $2
b
$

But instead of using
Code:
$ echo $3

Is there a way of using it as below?
Code:
$ z=3
$ echo $z

Expected output is 'c' instead of '3'

The reason why I want to do this is like I mentioned before the number of arguements may vary in my input and one of my requirement is to get the last - 1 parameter.
For example
Code:
$ set -- -parm a=b -parm c=d -parm e=f \\
$ echo $1
-parm
$ echo $4
c=d
$ echo $7
\

This works fine when $4 or $1 is an arguement to echo.

But I need the 6th parameter of set which is e=f and this position varies based on my input line. Sometimes it might be 7th position as shown in above example and sometimes it may be 3rd.

Last edited by sacguy08; 07-23-2009 at 06:43 PM..
# 14  
Old 07-24-2009
I think there are two answers here. First, you seem to be asking how to access a variable-variable name. In other words, I want to define a variable name with a variable since I don't know something about the code before it runs. Try this:

Code:
set a b c d
field=3
eval echo '$'$field

Second, you seem to want this so you can grab the last field from the original input.

Original input from before:

Code:
-parm a=b -parm c=d -parm e=f -parm g=h \
-parm a=b -parm c=d \
-parm a=b -parm c=d -parm e=f \

This can be done simply with awk:

Code:
awk '{print $(NF-1)}' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Does awk have parameter substitution?

Can I specify a default value to a variable in AWK like BASH in one statement using parameter substitution? BASH example: argument=${$1-"default if empty"} (BASH) I know I can do: argument=$1; sub ( "^$", "default if empty", argument) (AWK) Mike (13 Replies)
Discussion started by: Michael Stora
13 Replies

2. Shell Programming and Scripting

Passing parameter to awk command

Hi, I have a situation where I need to create a SQL statement using Unix script for each value in first field (ID). The file looks like this. Id,TARGET_FIELD_NAME,SOURCE_FIELD_NAME 1,Test_Rate,Field1 1,Test_Factor,Field2 1,Test_Size,Field3 2,Test_Rate,Field4 2,Test_Factor,Field5... (3 Replies)
Discussion started by: kiranredz
3 Replies

3. Shell Programming and Scripting

Parameter not accepting in awk

In below script parameterwhich i am passing to awk function is not working.Not sure why its not accepting parameter.Please tell me how to pass parameter to the awk function.Please check the below script. #!/bin/ksh dummy_file=/etlapps/dev/data/sub_servicer_data/ScriptLogs/emp.txt fields=5... (2 Replies)
Discussion started by: katakamvivek
2 Replies

4. Shell Programming and Scripting

awk: replace with script parameter

Hi var=0001 I want to replace 2nd field of file with variable var in file sample.txt Please suggest with awk. dont want to use awk -v option. pseudo code : something like this. var=0001 awk '{ 12193 /var } {print $0 }' sample.txt (2 Replies)
Discussion started by: theshashi
2 Replies

5. Shell Programming and Scripting

Passing a parameter to AWK

Hi All, I am trying to pass a parameter to AWK on my KSH shell prompt as below. var1=2 echo $var1 awk -v var2=${var1} '{print var2}' testfile.txt I am passing the input file (testfile) to awk to get some o/p. It is having 10 records. When I run AWK, it is throwing the following errors... (1 Reply)
Discussion started by: Raamc
1 Replies

6. Shell Programming and Scripting

awk pass $LOGDIR parameter

hi guys i need your help , i wrote one script which is as below #!/bin/ksh ########################################################### LOGDIR=/export/home/xyz/logs EMAILFile=$LOGDIR/xxs_email.log BOX=$(uname -a | awk '{print $2}') awk '{if ($4 >= 30) {print $1 " " $3 " HAS LAG of "... (1 Reply)
Discussion started by: tapia
1 Replies

7. Shell Programming and Scripting

using awk != parameter

Hi all, I am having the below file log.txt 1 aaa 111 @@@ 2 bbb 222 ### 14 ccc 333 $$$ using awk '$1!=1 || $1!=14 {print $1" " $2}' log.txt gets the below output 1 aaa 2 bbb 14 ccc (4 Replies)
Discussion started by: karthikn7974
4 Replies

8. Shell Programming and Scripting

awk parameter

How to pass a parameter in system function e.g system(script parameter) i want to run a shell script along with a parameter (5 Replies)
Discussion started by: alokmits
5 Replies

9. Shell Programming and Scripting

awk/input parameter

Hi, My script takes in one input parameter($1-email id) on the command line... The script contains something like this... awk '$1 == 400' abc.log >def.log mail -s subject $1 <def.log abc.log looks something like this... 300 222 330 123 445 400 098 890 727 663 How do i make the... (3 Replies)
Discussion started by: wannalearn
3 Replies

10. UNIX for Dummies Questions & Answers

PASS parameter to AWK

Hi, Can i pass a parameter(not a file name) as a parameter to a awk program? eg; $awk -f test 1 2 3 here test is the filename...and 1,2,3 are the i/p parameters? thank you:-) (2 Replies)
Discussion started by: unisam
2 Replies
Login or Register to Ask a Question