LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-09-2011, 04:32 AM   #1
meridionaljet
LQ Newbie
 
Registered: May 2011
Posts: 18

Rep: Reputation: 0
Help with awk and accessing variable values within the syntax


Hello,

I'm writing a bash script, and I am having trouble with this line:

awk '{print "url = \"http://www.example.com/directory/$type/"$1"\""}' input.file > output.file

Within the URL being printed, I wish the value of the variable "$type" to be printed. However, the quotation marks associated with the awk command do not allow the value of the variable to be called (syntax highlighting doesn't recognize $type as a variable, but rather as a literal part of the URL). Thus, the URL that ends up being printed literally has "$type" printed as a part of it. I would like to know how to change this behavior so that the actual value of the variable $type is printed within the URL instead. Any help would be appreciated.

Last edited by meridionaljet; 07-09-2011 at 04:34 AM.
 
Old 07-09-2011, 04:59 AM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello meridionaljet,

I can't help you with your question. But your question has much more to do with "Programming" than with "Software". I'd recommend that you use the "Report"-button at your post and ask a Moderator to move your thread to the Programming-forum of LQ, http://www.linuxquestions.org/questions/programming-9/

You will find more people there who can help you with this question.

Markus
 
Old 07-09-2011, 05:16 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
awk '{print "url = \"http://www.example.com/directory/'"$type"'/"$1"\""}' input.file > output.file
 
Old 07-09-2011, 07:28 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Or use awk's ability to create variables at calling:
Code:
awk -vtype=$type '{print "url = \"http://www.example.com/directory/"type"/"$1"\""}' input.file > output.file
 
Old 07-09-2011, 01:08 PM   #5
meridionaljet
LQ Newbie
 
Registered: May 2011
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Or use awk's ability to create variables at calling:
Code:
awk -vtype=$type '{print "url = \"http://www.example.com/directory/"type"/"$1"\""}' input.file > output.file
Thank you very much. That worked. I apologize for putting this in the wrong forum section.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
passing a shell variable into awk - syntax for correct interpretation? johnpaulodonnell Programming 3 06-19-2007 12:13 PM
How to share variable values between shell and awk? realos Programming 1 12-16-2006 10:15 PM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

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