LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-25-2010, 12:25 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Not able to get external variables in awk


Dear All,

In the following lines I am trying to replace Puppy Linux 5.0 Released with the content of external variable Var.

Following lines are in a file called news

Code:
<A title="Puppy Linux 5.0 Released" href="http://lwn.net/Articles/388754/" rel=bookmark><FONT color=#ffffff size=2><STRONG>Puppy Linux 5.0 Released
</STRONG></A><STRONG> &nbsp;|&nbsp; </STRONG>
I have tried following.

Code:
awk -v var=NewNews '/title="[^"]*/{n+=1}{if (n==1){sub(/title="[^"]*/,"title=\"$var",$0)};print }' news
But it is replacing Puppy Linux 5.0 Released as $var, not with the contents of var.

can anybody help me with this..

Thanks
 
Old 05-25-2010, 12:55 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Sure. -v var=NewNews defines an awk variable called var but "title=\"$var" does not use it (it is the string 'title="$var'). Fix by "title=\"" var
 
Old 05-25-2010, 01:07 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by catkin View Post
Sure. -v var=NewNews defines an awk variable called var but "title=\"$var" does not use it (it is the string 'title="$var'). Fix by "title=\"" var
Thanks catkin, It worked.

Pretty amazing, How the contents of var is coming under double quotes, as it resides outside double quotes in "title=\"" var ?

Last edited by vinaytp; 05-25-2010 at 01:09 AM.
 
Old 05-25-2010, 01:18 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,473

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
Please mark as SOLVED once you have a solution

Quote:
Pretty amazing, How the contents of var is coming under double quotes, as it resides outside double quotes in "title=\"" var ?
Not really because if you look at your replacement you said to leave the last pair of quotes untouched [^"]* so you then replaced with
title=\" this escapes the first quotes so they are included and the "var is the end of the quotes for the replacement along with the data in your variable so final product would be:

title="NewNews"

Last edited by grail; 05-25-2010 at 01:22 AM.
 
Old 05-25-2010, 01:41 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by vinaytp View Post
Pretty amazing, How the contents of var is coming under double quotes, as it resides outside double quotes in "title=\"" var ?
Grail's answered it already but I'd like to add detail.

In bash "title=\"$var" would do what you want but awk does not have the same syntax as bash.

In awk:
  • "title=\"$var" means a string of 'title="$var', that is exactly what is between the double quotes with the escaped " included.
  • "title=\"" var means a string of 'title="' concatenated with the value of variable var, thus giving 'title="NewNews' as intended.
  • Variables are referenced by name and do not have a leading $, unlike bash.
  • Adjacent strings, separated by whitespace, are concatenated, unlike bash which concatenates them with a single space between.
 
  


Reply


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using variables with awk dots Linux - Newbie 4 07-07-2009 03:00 PM
using variables in awk vgr12386 Programming 14 06-24-2009 04:19 AM
Does awk depend on the environment variables ? first_linux Linux - Newbie 6 05-08-2009 03:17 AM
Assigning variables by awk(?) bioinformatics_guy Linux - Newbie 2 02-19-2009 12:01 PM
How to make 2 variables from one variable value in awk intikhabalam Linux - General 1 07-30-2008 04:32 AM


All times are GMT -5. The time now is 11:03 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration