Professional Web Applications Themes

Perl equivalent to the unix 'cut' command - PERL Beginners

Howdy list, Subject speaks for itself.. Need to find a function or module to work like the 'cut' command. I have a file with multiple fields and i need to 'cut' out 2 of the fields. Looked thru CPAN for cut, but didnt find anything, started looking thru CPAN searching on 'File' but I think I'm just not thinking of the right keyword that perl uses. I'm still new to perl and I'm sorry for my ignorance if it should be right under my nose Thanks in advance, Dave Kettmann NetLogic...

  1. #1

    Default Perl equivalent to the unix 'cut' command

    Howdy list,

    Subject speaks for itself.. Need to find a function or module to work like the 'cut' command. I have a file with multiple fields and i need to 'cut' out 2 of the fields. Looked thru CPAN for cut, but didnt find anything, started looking thru CPAN searching on 'File' but I think I'm just not thinking of the right keyword that perl uses. I'm still new to perl and I'm sorry for my ignorance if it should be right under my nose

    Thanks in advance,

    Dave Kettmann
    NetLogic
    Dave Guest

  2. #2

    Default Re: Perl equivalent to the unix 'cut' command

    On Thu, 14 Oct 2004, Dave Kettmann wrote:
     

    Okay then.

    perldoc -f split

    Also speaks for itself :-)




    --
    Chris Devers
    Chris Guest

  3. #3

    Default Re: Perl equivalent to the unix 'cut' command

    On Thu, 14 Oct 2004, Chris Devers wrote:
     
    >
    > Okay then.
    >
    > perldoc -f split
    >
    > Also speaks for itself :-)[/ref]

    To be less snarky, you probably need to open up your file, iterate over
    it line by line, using split to break each line up into chunks, then
    write out a new array with the fields you want and the order you want
    them. This second array can then be written out to disc; if you want you
    could even read & write within the same loop.

    But the key point is that split is often the easiest way to break apart
    the fields in a file that is, for example, CSV formatted.

    Give that a try, write some code to attempt it, and let the list know if
    you have any problems in getting it to work.


    --
    Chris Devers
    Chris Guest

  4. #4

    Default RE: Perl equivalent to the unix 'cut' command


     
    > >
    > > Okay then.
    > >
    > > perldoc -f split
    > >
    > > Also speaks for itself :-)[/ref]
    >
    > To be less snarky, you probably need to open up your file,
    > iterate over
    > it line by line, using split to break each line up into chunks, then
    > write out a new array with the fields you want and the order you want
    > them. This second array can then be written out to disc; if
    > you want you
    > could even read & write within the same loop.
    >
    > But the key point is that split is often the easiest way to
    > break apart
    > the fields in a file that is, for example, CSV formatted.
    >
    > Give that a try, write some code to attempt it, and let the
    > list know if
    > you have any problems in getting it to work.
    >
    >
    > --
    > Chris Devers
    > [/ref]

    Chris,

    The reply was deserved :) Just another question before I go too far with this... The files I am parsing (just needing 2 tabbed fields out of them) are approximately 20,000 - 25,000 lines long a piece. Each of these files will be globbed into one file, but that is something completely different. I guess my question is, would I be better off calling exec(cut) with files of this size for ease of use? Guess I should have mentioned this in my previous email.

    Thanks again,

    Dave
    Dave Guest

  5. #5

    Default RE: Perl equivalent to the unix 'cut' command

    On Thu, 14 Oct 2004, Dave Kettmann wrote:
     

    Not necessarily.

    I seem to remember that as long as you're iterating over a small window
    of the file at any given time, you don't necessarily end up slurping the
    whole thing into memory at once.

    How long is each line? How large are the files, bytewise? And how much
    memory (etc) do you have to work with?

    This is going to be a situations where benchmarks are invaluable.




    --
    Chris Devers
    Chris Guest

  6. #6

    Default RE: Perl equivalent to the unix 'cut' command

     
    > fields out  
    > piece. Each of  
    >
    > Not necessarily.
    >
    > I seem to remember that as long as you're iterating over a
    > small window
    > of the file at any given time, you don't necessarily end up
    > slurping the
    > whole thing into memory at once.
    >
    > How long is each line? How large are the files, bytewise? And
    > how much
    > memory (etc) do you have to work with?
    >
    > This is going to be a situations where benchmarks are invaluable.
    >
    >
    >
    >
    > --
    > Chris Devers
    > [/ref]

    Each line is probably 80-100 characters in legnth, the files are about 300Kb each (6 files total) working with 1GB of memory. Looking at these numbers, dont know that these are really that big of a file, but seem like it when you look at them in vi ;)... I guess I will give slice a shot and see what I can do with it, I will keep you and the list updated :)

    Dave Kettmann
    Dave Guest

  7. #7

    Default RE: Perl equivalent to the unix 'cut' command

    On Thu, 14 Oct 2004, Dave Kettmann wrote:
     

    Oh, that's it? I'm sure you'll be find then

     

    Well, among other things, Vim handles big files more smoothly :-)




    --
    Chris Devers
    Chris Guest

  8. #8

    Default Re: Perl equivalent to the unix 'cut' command

    Dave Kettmann wrote: 

    Hello,
     

    Really? I found it right away.

    http://search.cpan.org/~cwest/ppt-0.14/src/cut/cut.hewgill
     

    Use 'cut'.
     


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  9. #9

    Default RE: Perl equivalent to the unix 'cut' command

    On Thu, 14 Oct 2004 17:40:28 -0400, Chris Devers wrote:
     
    >
    > Oh, that's it? I'm sure you'll be find then[/ref]

    Most definitely. I sometimes iterate over 1Gb files in a system with 512Mb
    RAM with no problems. It usually takes ~20 seconds to complete so speed
    isn't too much of an issue either.
     
    >
    > Well, among other things, Vim handles big files more smoothly :-)[/ref]

    Chris Guest

Similar Threads

  1. Equivalent of 'DO' command in Flash
    By Jeckyl in forum Macromedia Flash Actionscript
    Replies: 2
    Last Post: February 19th, 11:13 PM
  2. Wrapping Unix Command into Perl
    By Anthony J Segelhorst in forum PERL Beginners
    Replies: 6
    Last Post: December 12th, 10:01 PM
  3. Replies: 1
    Last Post: November 18th, 09:08 PM
  4. equivalent perl for sed command ..newbie question
    By walter in forum PERL Miscellaneous
    Replies: 4
    Last Post: November 6th, 04:03 PM
  5. Unix equivalent of Dos commands
    By justin in forum SCO
    Replies: 8
    Last Post: June 26th, 05:54 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139