LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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: 66
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,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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