Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 10-07-2010, 03:09 PM   #1
LQ Newbie
Registered: Jul 2008
Posts: 5

Rep: Reputation: 0
shell script -- awk variable substitution


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
# 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:
CALL ECHO ( BUILD ("person id: " , REQUEST -> PERSONID ))
Then the awk functionality is failing and giving me errors as
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.


Last edited by r2d2#jedi; 10-07-2010 at 03:30 PM.
Old 10-07-2010, 04:58 PM   #2
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
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)}'
echo "$Line2"|awk -v Substr="  (" '{ print index($0,Substr)}'
echo "$Line3"|awk -v Substr="  (" '{ print index($0,Substr)}'
So it seems, if I understand correctly what you want to do, that your code should be :
# 2 spaces before open parens
pos=$(echo "$Line"|awk -v Substr="  (" '{ print index($0,Substr)}')
At least it seems to give what you expect.
Old 10-07-2010, 10:04 PM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell variable in awk script rohini_sharma Programming 3 02-22-2010 09:32 AM
Trouble storing a variable in a shell script after awk output uncle-c Linux - Newbie 3 02-08-2010 08:13 AM
[SOLVED] variable substitution in awk statement emmalg Linux - Software 12 07-02-2009 09:39 AM
passing awk output to $variable for shell script? NewnanNOC Programming 3 10-23-2008 03:08 PM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 08:47 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:47 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration