![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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}' $_");
|
|
||||
|
Quote:
To store the 4th column value in perl, Code:
my @arr = split(/\|/, $var); print $var[3]; |
|
||||
|
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?
|
|
|||||
|
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
|
|
||||
|
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) |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|