sorting after 'grepping' 
Author Message
 sorting after 'grepping'

Hi,

        I've been assigned to convert our shell programs to perl and one
        construct I'm having difficulty changing to Perl is the following
        fragment:

        grep "pattern" /my/file | sort

        or generally

        grep "pattern" /my/file | [some command].

        I tried doing the following and it works but there's no sorting.

        open (MYFILE,"/my/file");
        while (<MYFILE>) {
                if (/pattern/) {
                        print "$_";
                }
        }

        I think it's something to do with 'piping.' Any solutions how to
        'pipe' the output of the above into Perl's built-in 'sort'
        function? Pls. email or post. Thanks!

--
- J -



Sat, 19 Jul 1997 10:09:35 GMT  
 sorting after 'grepping'

Quote:

>    I've been assigned to convert our shell programs to perl and one
>    construct I'm having difficulty changing to Perl is the following
>    fragment:
[snip]
>    I tried doing the following and it works but there's no sorting.
>    open (MYFILE,"/my/file");
>    while (<MYFILE>) {
>            if (/pattern/) {
>                    print "$_";
>            }
>    }

Try this:

        open (FILE, "/blah/blah/blah");
        while (<FILE>) {

        }

--




Sat, 19 Jul 1997 11:24:34 GMT  
 sorting after 'grepping'

Daryn> Try this:

Daryn>       open (FILE, "/blah/blah/blah");
Daryn>       while (<FILE>) {

Daryn>       }

print sort grep /pattern/,<FILE>;

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>



Thu, 24 Jul 1997 03:14:43 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Large number of pattern 'grepping'

2. How to dup 'sort'

3. anonymous code refs and 'sort'

4. 'sort'ing umlauts

5. 'Middle of String' sort revisited

6. What is pseudorandommy @chars=('a'..'z','A'..'Z','0'..'9','_');

7. HELP: 'grepping' large files taking TOO LONG

8. 'if' returning false if given 'sort @ary'

9. Sorting user's mail directories

10. While we're sorting...

11. Beginner's question: sorting large arrays

12. Q: sort's key and cmp parameters

 

 
Powered by phpBB® Forum Software