LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-15-2012, 12:55 PM   #1
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Rep: Reputation: 1
PERL, How to pipe out put of a perl script for processing with linux command.


Hi

I have an array which stores some rules ID's which I extracted from a log file. What I would like to do is feed this array content to linux 'sort -nu' command and find out the unique rule IDs. But I don't want to redirect perl out to this command like

perl id.pl | sort -nu

Instead I would like to do this inside the perl script and get the uniqe IDs with 'perl id.pl'. Is it possible to do this.
ie: Feed the data contained within an array of a perl script to a linux command for processing. I know it is very easy to catch the out put of a linux command in perl script like

$x = `cat /etc/passwd`;

Similrly, is the reverse thing possible?. Feed the conents of an array to a linux command for further processing.

Code:
$x = `cat log.txt`;
@ar = split /(\[id "\d+"\])/, $x;
foreach (@ar)
{
if (/id "\d+"/)
{
@ar1 = split '"', $_;
push (@ar2, @ar1);
}
}

foreach (@ar2)
{
if (/\d+/)
{
print "$_\n";
}
}
This is the code which I am using, which will give some rule IDs like

380025
380025
380025
380025
380025
340155
340155

I would like to find the unique rules by feeding the ID's stored in @ar2 to 'sort -nu' command. It can be done like this

Code:
# perl id.pl | sort -nu
But, I dont want to use this long command and want to process the above command inside the script. is it possible?.

I am newbie in PERL programming. I am trying to learn how to mix perl and linux commands to obtain a desired result.

Last edited by Soji Antony; 11-15-2012 at 12:59 PM.
 
Old 11-15-2012, 01:57 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,970

Rep: Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848
Hi,

sort numerical with Perl
Code:
sort {$a<=>$b} @ar ;
uniq can simply be done with a hash
Code:
my %uniq;
foreach (@ar) {
   $uniq{$_}=1;
}
foreach (keys %uniq) {
    print $_ ;
}
Markus

Last edited by markush; 11-15-2012 at 02:00 PM. Reason: typo
 
Old 11-15-2012, 03:35 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
After putting your sample data into 'LQSojiAntony.dat'...
Code:
perl -e '@array=<>; open(SORT,"| sort -nu"); print SORT @array; close SORT;' LQSojiAntony.dat
results in
Code:
340155
380025
This is the classic method for piping data to arbitrary processes. Great for automating applications that read instructions or data from standard input. Try it with something like gnuplot.
--- rod.

Last edited by theNbomr; 11-15-2012 at 03:39 PM.
 
1 members found this post helpful.
Old 11-16-2012, 09:04 AM   #4
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Original Poster
Rep: Reputation: 1
Hi Thenbomr, Markush

Thank you so much for your kind responses.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl experts: I need to run shell command from perl using pipe idreesedu Programming 14 01-11-2011 04:52 PM
[SOLVED] perl script output cannot pipe to file hattori.hanzo Programming 3 12-15-2010 02:26 AM
Use of "Command line perl" in perl script using system command. aditya007 Linux - Newbie 4 11-29-2009 10:08 PM
linux command inside perl script von_vea Programming 5 09-08-2009 01:54 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM


All times are GMT -5. The time now is 07:43 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration