LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-25-2007, 02:04 AM   #1
sumin
LQ Newbie
 
Registered: Apr 2007
Posts: 2

Rep: Reputation: 0
How to acess Variable defined in perl script inside an awk call


In a perl script i am defining a variable called ColumnNum and assigning a value 5.

$ColumnNum=5;

I want to use this $ColumnNum to be used in the following awk call



$getCPUinfo = 'sar 1 1 |awk \'{print $($columnNum)}\'';


I am not able to use the variable since it is defined outside the awk .


Please help me with this..
 
Old 04-25-2007, 03:20 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
You have to use awk's "-v var=val" option.

Yves.
 
Old 04-25-2007, 03:22 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by sumin
In a perl script i am defining a variable called ColumnNum and assigning a value 5.

$ColumnNum=5;

I want to use this $ColumnNum to be used in the following awk call



$getCPUinfo = 'sar 1 1 |awk \'{print $($columnNum)}\'';


I am not able to use the variable since it is defined outside the awk .


Please help me with this..
you can try this:
Code:
$getCPUinfo = 'sar 1 1 |awk -v colnum=$columnNum \'{print colnum}\'';
 
Old 04-26-2007, 05:19 AM   #4
sumin
LQ Newbie
 
Registered: Apr 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74
you can try this:
Code:
$getCPUinfo = 'sar 1 1 |awk -v colnum=$columnNum \'{print colnum}\'';
Thank you for ur help.........
 
  


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 global variable in awk (bash script) kopeda Programming 2 04-24-2007 01:47 AM
Variable expansion inside of a bash script! A.S.Q. Linux - Newbie 4 09-29-2006 09:09 AM
perl: send output of system() call to variable drkstr Programming 4 07-23-2006 10:47 PM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM


All times are GMT -5. The time now is 11:40 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