Saving output from awk into a perl variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Saving output from awk into a perl variable
# 1  
Old 10-08-2008
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  
Old 10-08-2008
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  
Old 10-08-2008
Quote:
Originally Posted by userix
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  
Old 10-09-2008
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);

# 5  
Old 10-09-2008
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?
# 6  
Old 10-09-2008
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  
Old 10-09-2008
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 05:44 AM.. Reason: first, not last occurrence (missed that -- thanks otheus)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using variable output in awk

Hi, I am trying to use variable output in awk to append a string to a word in a line. But that is not happening. Could you please help me on this. YouTube Video Tutorial: How to Use Code Tags and Format Posts @UNIX.com The below is the code #!/bin/ksh set -x src=/users/oracle/Temp... (2 Replies)
Discussion started by: pvmanikandan
2 Replies

2. Shell Programming and Scripting

awk - saving results of external script to variable.

So, I've been playing with speeding up some analysis we do by using multiple threads of awk (actually, mawk, but code-compatible as far as I use it) on multiple CPU cores. So, I have a big data file and I have several copies of exactly the same processor script, written in mawk. I also have a... (8 Replies)
Discussion started by: treesloth
8 Replies

3. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

4. Shell Programming and Scripting

Perl help - how to assign output of perl to variable

Hi, guys, i have a script i inherited from a coworker but i'm not perl savy. The script works but i would like it to work better. I want to run this command ./ciscomgrtest.pl -r "show version" -h hosts.router and have the script goto each router in the hosts.router file and run the command... (2 Replies)
Discussion started by: whipuras
2 Replies

5. Shell Programming and Scripting

help on awk---- need to assign the output of awk to a variable

hi i want to find the size of a folder and assign it to a variable and then compare if it is greater than 1 gb. i am doin this script, but it is throwing error.... #!/bin/ksh cd . | du -s | size = awk '{print $1}' if size >= 112000 then echo size high fi ERROR : (4 Replies)
Discussion started by: Nithz
4 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

Assign perl output to ksh shell variable

Hello, I am writing a ksh script on an AIX system. I need to get the date and time from a file into a variable. I found the following perl script from another post on this site and modified it slightly to output the format I need: perl -e '@d=localtime ((stat(shift)));... (4 Replies)
Discussion started by: swimp
4 Replies

8. Shell Programming and Scripting

Piping and assigning output to a variable in Perl

Hi All, I am trying to convert the below Csh code into Perl. But i have the following error. Can any expert help ? Error: ls: *tac: No such file or directory Csh set $ST_file = `ls -rt *$testid*st*|tail -1`; Perl my $ST_file = `ls -rt *$testid*st*|tail -1`; (10 Replies)
Discussion started by: Raynon
10 Replies

9. UNIX for Dummies Questions & Answers

saving command output to a variable

Hello, I have a shell script containing a command string in the following format: command1 | command2 | cut -c9-16 The output from this is a record number (using characters 9-16 of the original output string) e.g. ORD-1234 I wish to save this value to a variable for use in later commands... (4 Replies)
Discussion started by: philjo
4 Replies

10. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies
Login or Register to Ask a Question