nawk and space in the parameter

Hi,

Could you please tell me how nawk command works when there is a asterisk <*> or space with asterisk <[space] > or <[space] > in the parameter.

I am just trying to read line by line and fetch fourth parameter separated by delimiter (|).
But if there is a * or <[space] > or <[space] > in the fourth parameter it gets all the files in that directory. How to address this issue..

PARAM4=`echo $LINE | nawk -F"|" '{print $4}'
echo $PARAM4

output for echo comes like that 123 file1.csv file2.txt

It's not awk, it's the shell ...
You need to quote the parameters in order to avoid shell filed splitting and filename generation (globbing)!

PARAM4=`echo "$LINE" | nawk -F"|" '{print $4}'
echo "$PARAM4"

Or better:

PARAM4=$(
  printf '%s\n' "$LINE" | 
    nawk -F"|" '{ print $4 }'
  )

printf '%s\n' "$PARAM4"
1 Like

second option works good.. Thank you..
if i want to make some kind of string manipulation on PARAM4, how should i do.. like using sed or etc., when i use PARAM4 with sed, i getting stuck with same problem again.

We'll need to see a sample of your input and an example of the desired/expected output.

1 Like

Thanks,

Here is the script <test.ksh> i use

#!/bin/ksh
LINE="nramak|BFS|121 * 789 * 85#2[123]|7539|"
PARAM4=$(printf '%s\n' "$LINE" | nawk -F"|" '{ print $3 }')
printf '%s\n' "$PARAM4"

PARAM5=`echo "$PARAM4" |  sed 's/\[/ /g'  | sed 's/\]/ /g' | sed 's/#/ /g'`

echo $PARAM5

when i ran it, i got below:

121 * 789 * 85#2[123]
121 test.ksh 789 test.ksh 85 2 123

seems like i have trouble in line#5 while using sed command on PARAM4.

Any idea how to get this done.
Thanks in advance!

Try this:

line='nramak|BFS|121 * 789 * 85#2[123]|7539|'

param4=$(
  printf '%s\n' "$line" |
    cut -d\| -f3 |
          tr ']#[' '   '
  )

printf '%s\n' "$param4"
1 Like

It works. Thank you very much
Which command gives better performance sed or tr

I don't think that it makes any difference in this case.