The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get following output in perl email-lalit Shell Programming and Scripting 1 08-08-2008 01:20 PM
Set a variable from awk output Cranie UNIX for Dummies Questions & Answers 3 10-11-2007 09:39 AM
To store the output in a variable Sudhakar333 Shell Programming and Scripting 2 07-10-2007 08:45 AM
how to output awk to a variable bashirpopal UNIX for Dummies Questions & Answers 4 04-02-2003 11:02 AM
Saving Perl scrpits in a UNIX Shell hagrid UNIX for Dummies Questions & Answers 3 06-21-2001 12:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-07-2008
userix userix is offline
Registered User
  
 

Join Date: May 2008
Posts: 21
Saving output from awk into a perl variable

How would I pass awk output to a perl variable?

For example, I want to save the value in the 4th column into the variable called test. My best guess is something as follow, but I am sure this isn't correct.

Code:
$test = system("awk '/NUMBER/{print \$4}' $_");
  #2 (permalink)  
Old 10-08-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
Perl supports the "backtick" operator, just as in the shell. the only difference is that Perl doesn't convert linefeeds to spaces nor squeeze extra spaces. So:
Code:
$test=`awk .... $_`;
is probably what you want.
  #3 (permalink)  
Old 10-08-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,953
Quote:
Originally Posted by userix View Post
How would I pass awk output to a perl variable?

For example, I want to save the value in the 4th column into the variable called test. My best guess is something as follow, but I am sure this isn't correct.

Code:
$test = system("awk '/NUMBER/{print \$4}' $_");
Why don't you use the feature provided in perl itself ?

To store the 4th column value in perl,

Code:
my @arr = split(/\|/, $var);
print $var[3];
Is that what you meant ?
  #4 (permalink)  
Old 10-09-2008
userix userix is offline
Registered User
  
 

Join Date: May 2008
Posts: 21
I want to search for a number found in the 4th column on a specific line in a file and store that value into a variable. This file contains over 7000 lines of data. The specific line I am looking for contains the letters 'OXT' This is why I was thinking of using awk to find the line that contains 'OXT' and then grab the number from the 4th column of that line only. Would that work with split?
  #5 (permalink)  
Old 10-09-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
He's processing a file stored in $_. For that, the awk one-liner looks nicer. But it would normally be replaced with:

Code:
my @test;
if (open(FOO,$_)) { 
 while ($_=<FOO>) { 
   push @test,(split)[4];
 }
}
$test=join(" ",@test);
  #6 (permalink)  
Old 10-09-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
Absolutely. Are you looking for exactly one line, or more? Are the columns separated by whitespace? Assuming yes to all the answers above:
Code:
my $target;
if (open(FOO,$_)) { 
 while ($_=<FOO>) { 
   next unless /OXT/;  # find OXT anywhere in line.
   $target=(split)[4];
   last;
 }
 close(FOO); # forgot this last time, though not really necessary.
}
# 4th field in $target
  #7 (permalink)  
Old 10-09-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
That will only pick out the first occurrence, though. The original awk script would return all matching lines. Not hard to work around if you know any Perl, just mentioning in case you're new to this.

Last edited by era; 10-09-2008 at 04:44 AM.. Reason: first, not last occurrence (missed that -- thanks otheus)
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0