LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   shell script -- awk variable substitution (https://www.linuxquestions.org/questions/linux-general-1/shell-script-awk-variable-substitution-836802/)

r2d2#jedi 10-07-2010 02:09 PM

shell script -- awk variable substitution
 
Hi,

I have a script in which I'm reading a file line by line and I'm finding certain position value using awk index as
Code:

# 2 spaces before open parens
pos=`echo | awk -v Line="$Line" '{ print index("'"$Line"'","  (")}'`

When reading the file line by line and if the line is like this and when the script tries to executes the line:
Quote:

CALL ECHO ( BUILD ("person id: " , REQUEST -> PERSONID ))
Then the awk functionality is failing and giving me errors as
Code:

awk: { print index("CALL ECHO ( BUILD ("person id: " ,  REQUEST -> PERSONID ))","  (")}
awk:                                            ^ syntax error
awk: fatal: 0 is invalid as number of arguments for index

Please do let me know how to resolve this.

Thanks

berbae 10-07-2010 03:58 PM

Code:

Line1='CALL ECHO ( BUILD ("person id: " , REQUEST -> PERSONID ))'
Line2='CALL ECHO  ( BUILD ("person id: " , REQUEST -> PERSONID ))'
Line3='CALL ECHO ( BUILD  ("person id: " , REQUEST -> PERSONID ))'
echo "$Line1"|awk -v Substr="  (" '{ print index($0,Substr)}'
0
echo "$Line2"|awk -v Substr="  (" '{ print index($0,Substr)}'
10
echo "$Line3"|awk -v Substr="  (" '{ print index($0,Substr)}'
18

So it seems, if I understand correctly what you want to do, that your code should be :
Code:

# 2 spaces before open parens
pos=$(echo "$Line"|awk -v Substr="  (" '{ print index($0,Substr)}')

At least it seems to give what you expect.

ghostdog74 10-07-2010 09:04 PM

why are you using "-v Line=$Line" when you are not using it in your awk command ?
What is $Line?

Code:

awk -v Line="$Line" '{ print index(Line, ...) }'
and you should use $() with bash , not `..`


All times are GMT -5. The time now is 10:49 PM.