How to store values into variable in perl


 
Thread Tools Search this Thread
Operating Systems Linux How to store values into variable in perl
# 1  
Old 05-04-2008
Bug How to store values into variable in perl

Hi,

Can you please help me of how to store the values into variables.

Here is the output in LINUX for the below command.

$free

output :
total used free
Mem: 3079276 3059328 19948
Swap: 1023992 6324 1017668

while i am trying the below commands i got the ouput
free |grep "Mem:" |awk ' {print $2}'
output for the above cmd is printing == 3079276

similar to the swap as well..

But i want to store the ouput value in a variable

I also decalred the variable like this

$Memory=`free |grep "Mem:" |awk ' {print $2}'`;
print "$Memory ";
It is printing like this
Mem: 3079276 3059328 19948

Can anyone please help me regarding this how to store the values into variables....Smilie
# 2  
Old 05-04-2008
Try this:

Code:
Memory=$(free | awk '/Mem:/{print $2}')
print $Memory

Regards
# 3  
Old 05-05-2008
$2 in the awk script probably gets substituted with something unexpected when you evaluate it in backticks in Perl. $2 in Perl is the result of the second set of parentheses in the latest regular expression match; presumably it's empty, so the awk script is just "print".

Quote the variable properly, and it should work.

Note, though, that you have a Useless Use of Grep when you pipe to awk -- awk can grep just fine. And by the same logic, I guess it's a Useless Use of Awk to call awk from Perl, because Perl can do everything awk can, only better.

Code:
perl -e '$mem = `free`; $mem =~ s/.*Mem:\s+(\d+)\s.*/$1/s'

The /s option causes .* to match across newlines, so you can handle the multiple lines of output from free as one expression.
# 4  
Old 05-08-2008
Thank a lot guys.

It is working fine now. I appreciate your help.Once again thanks a million.SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PERL - Variable values getting mixed up!

Hi, I only dip my toe into PERL programming on the odd ocassion so I was wondering if anyone had any ideas as to why the below is happening: When I run my PERL script the variable values seem to get mixed up. my $fileName = basename($maxFile,".TXT"); my $currentSeqNum =... (2 Replies)
Discussion started by: chris01010
2 Replies

2. Shell Programming and Scripting

Extract values from Perl variable

Hi Guys, I am stuck in a problem. I have a variable in Perl script which has value for example X=a-b-c; Now, I want to extract a b c separately into different 3 variables. I know this can be done in shell using awk but Perl behaves a bit different. Can anybody help me on this please?... (3 Replies)
Discussion started by: prashant2507198
3 Replies

3. Shell Programming and Scripting

How to read values and store in array?

I am reading a value from a file and want to store the value in a dynamic array as i don't know the number of occurrences of the value in that file. How can i do that and then later fetch that value from array (25 Replies)
Discussion started by: Prachi Gupta
25 Replies

4. Shell Programming and Scripting

Store the output values in array

Hi, How to store the values in array from output result, EG: I have the result like this, ps, google, 1.txt, 1 sam, google, 2.txt, 2 These are the four values followed by comma in two sets. I need to store these values set by set. One set contains four values followed by comma. ... (2 Replies)
Discussion started by: KarthikPS
2 Replies

5. Shell Programming and Scripting

Store values from a file into an array variable in Shell

Dear All, I have been trying to do a simple task of extracting 2 fields from the file (3 rows) and store it in an array variable. I tried with: #! /bin/bash ch=`cut -f10 tmp.txt` counter=0 for p in $pid do c=${ch} echo "$c ..$counter" counter=$((counter+1))... (2 Replies)
Discussion started by: ezhil01
2 Replies

6. Shell Programming and Scripting

substitute variable for values in perl

hi all, how do i assign values passed in from command line to and sql statement in perl ?? e.g i want to assign :name1 and :Name2 to be whatever is passed into the perl script command line my $sqlStr = "select * from test_table where column1 = upper(nvl(:name1, name1 )) and column2... (1 Reply)
Discussion started by: cesarNZ
1 Replies

7. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

8. Shell Programming and Scripting

Store values in an Array

Hi all. Well, I have the next code: I need to make an array with the values I have in the bucle, but just don't get it... Question is, how can I store in an array that values, and how can I display them with echo? (8 Replies)
Discussion started by: crcbad
8 Replies

9. UNIX for Dummies Questions & Answers

How to store the values in a file into an array

Hi, I do have a file and the contents are as follws: 10 20 30 40 50 Now I want to store those values into an array. How can be done this ?? (3 Replies)
Discussion started by: risshanth
3 Replies

10. UNIX for Advanced & Expert Users

how do I store the values in array using shell

Hi, Is is possible to get the value using shell script? x=1 y1 = 10 y2 = 15 y3 = 7 echo $y$x is giving y1 (variable name) but I need the value of y1 (i.e. 10 dynamically) Is there any solution? if so, please mail me at kkodava@maxis.com.my ... (2 Replies)
Discussion started by: krishna
2 Replies
Login or Register to Ask a Question