Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing shell string to awk 1

Status
Not open for further replies.

tinoSWE

Technical User
Apr 9, 2010
2
DE
Hi all,
any help would be very appreciated, cause I got stuck.

In my (bash) shell script, the string named "energy" has the value "double energy = 100;" (which is printed out correctly on the screen). I am using a shell "for" loop, so "100" is one of the possible values of the "i" variable.

I would like to pass the string "en" to the awk command below.
---
en=${base}${i}${end}
echo $en
awk 'NR==21{$0="en" }1' routInt.h > routInt.temp
---
in routInt.temp at line 21, it writes the letters "en" cause I don't know how to pass to awk the value of the string "en"... Can you help me?
Thanks in advance,

Martino
 
Hi

We have dozens of thread and one FAQ about passing shell variables in [tt]awk[/tt] script. Next time please search the existing threads and FAQs first.
Code:
awk "NR==21{$0=\"$en\"}1" routInt.h

[gray]# or[/gray]

awk 'NR==21{$0="'"$en"'"}1' routInt.h

[gray]# or[/gray]

awk -v en="$en" 'NR==21{$0=en}1' routInt.h

[gray]# or[/gray]

awk 'NR==21{$0=en}1' en="$en" routInt.h

[gray]# or[/gray]

export en
awk 'NR==21{$0=ENVIRON["en"]}1' routInt.h

Feherke.
 
feherke, I'd replace this:
awk "NR==21{$0=\"$en\"}1" routInt.h
with this:
awk "NR==21{[!]\[/!]$0=\"$en\"}1" routInt.h
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top