The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > Linux
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to store the values in a file into an array risshanth UNIX for Dummies Questions & Answers 3 01-22-2008 06:34 AM
To store the output in a variable Sudhakar333 Shell Programming and Scripting 2 07-10-2007 05:45 AM
Need to get pid of a process and have to store the pid in a variable samudha UNIX for Dummies Questions & Answers 5 06-11-2007 03:27 AM
store awk results in a variable forever_49ers Shell Programming and Scripting 1 09-19-2006 07:51 PM
how do I store the values in array using shell krishna UNIX for Advanced & Expert Users 2 08-17-2001 06:49 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-04-2008
Registered User
 

Join Date: Nov 2007
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Smile 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....
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-04-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,275
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Try this:

Code:
Memory=$(free | awk '/Mem:/{print $2}')
print $Memory
Regards
Reply With Quote
  #3 (permalink)  
Old 05-05-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
$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.
Reply With Quote
  #4 (permalink)  
Old 05-07-2008
Registered User
 

Join Date: Nov 2007
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Thank a lot guys.

It is working fine now. I appreciate your help.Once again thanks a million.
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 02:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102