Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

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


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 | sort -nu

Instead I would like to do this inside the perl script and get the uniqe IDs with 'perl'. 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.

$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


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

# perl | 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 01:59 PM.
Old 11-15-2012, 02:57 PM   #2
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled

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

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

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
After putting your sample data into 'LQSojiAntony.dat'...
perl -e '@array=<>; open(SORT,"| sort -nu"); print SORT @array; close SORT;' LQSojiAntony.dat
results in
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 04:39 PM.
1 members found this post helpful.
Old 11-16-2012, 10:04 AM   #4
Soji Antony
Registered: Jul 2010
Posts: 54

Original Poster
Rep: Reputation: 1
Hi Thenbomr, Markush

Thank you so much for your kind responses.


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 05:52 PM
[SOLVED] perl script output cannot pipe to file hattori.hanzo Programming 3 12-15-2010 03:26 AM
Use of "Command line perl" in perl script using system command. aditya007 Linux - Newbie 4 11-29-2009 11:08 PM
linux command inside perl script von_vea Programming 5 09-08-2009 02:54 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 11:22 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:06 AM.

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