Parameter to AWK

Hi,

I need help with AWK. Here is the issue that i'm facing.

I have a variable in KSH script set as below.
user_text="first second third fourth"

My Requirement is to print 'first', 'second', 'third' or 'fourth' based on setting a variable

Example 1
user_text="first second third fourth"
var=1
echo $user_text | awk '{print $var}'

Ouput expected:
first

Example 2
user_text="first second third fourth"
var=3
echo $user_text | awk '{print $var}'

Ouput expected:
third

I'm new to AWK. I would be grateful if you could reply with correct AWK syntax. Your help is highly appreciated.

Thanks for your time.

Hello!

Per forum rules, and the benefit of all users, please search the forums before posting a question.

There're plenty of threads on parameter+awk subject.

You can easily search the forums using our internal Google search engine or our advanced internal search engine. You can also search our huge UNIX and Linux database by user generated tags or search the database for unanswered questions and provide an answer.

Thank you.

The UNIX and Linux Forums

you need array not awk, please refer to your shell array assignment

-bash-3.2$ x=(first second third fourth)
-bash-3.2$ echo ${x[0]}
first
-bash-3.2$ echo ${x[1]}
second
-bash-3.2$ echo ${x[2]}
third
-bash-3.2$ echo ${x[3]}
fourth
-bash-3.2$
 
set first second third fourth
echo $1 $4
Output==> first fourth

wow i didnt know that.. what happens if i use this to a script?
what happens to $1 if i have argumented script?

Coming back to original question, how to achieve this by passing variable to awk.

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.

Hello Everyone,

Thanks for your replies. I tried set and array solutions but it didn't work.

I'm using ksh in AIX 5.3

Here is my input string
-parm a=b -parm c=d -parm e=f -parm g=h \

Note: At the end of each line in my input file there is a Back slash charater.

My input string can be of variable length. For example it can be
-parm a=b -parm c=d \
or
-parm a=b -parm c=d -parm e=f \

My requirement is to check for values for each -parm and do a particular action.

For example if my input string is
-parm a=b -parm c=d -parm e=f -parm g=h \

If the last -parm value is g=h then I need to set a variable X to 1.
If the 2nd -parm value is a=b then I need to set a variable Y to 1.
That is I need to read the value one at a time from my input string and perform a particular action.

I thought it would be possible in awk and hence my original question.

Your different approachs to this problem or solutions to this problem is highly appreciated.

Thanks for your time.

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

$ 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

$ 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

$  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

$ 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?

This is the input file I used from your request:

-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:

#!/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

peterro,
Thank you very much. It's working :slight_smile:

Others,
Thanks for your suggestions and your valuable time. I appreciate it.

in that case:

 
set $1 first second third fourth
echo $2
   first
echo $5
   fourth

I used set as below which is working fine.

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

But instead of using

$ echo $3

Is there a way of using it as below?

$ 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

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

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:

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:

-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:

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

peterro,

Thanks in advance since I won't be able to have access to a UNIX machine until Monday. I'll try your solutions on Monday and let you know.

Thanks once again.

Try this:

set a b c d e f g h i
count=$#
lastone=`echo ${*:$count}`

echo $lastone
Output => i

---------- Post updated at 11:43 PM ---------- Previous update was at 10:17 PM ----------

user_text="first second third fourth"
echo ${user_text##* }

This is more simple

Did not realize you needed the last but one. Try this:

set aa bb cc dd ee ff gg hh ii
echo $* | sed 's/.* \([^ ]*\) [^ ]*$/\1/'

Thanks peterro and edidataguy.

It's working except for the below code given by edidataguy.

$ set a b c d e f g h i
$ count=$#
$ lastone=`echo ${*:$count}`
ksh: ${*:$count}: 0403-011 The specified substitution is not valid for this command.

Anyways my issues are solved. Thanks to everyone. I appreciate your help.

I think there is a version issue. It works fine for me.
Please post your final code for every one to see.