Discussion:
Perl equivalent to the unix 'cut' command
Dave Kettmann
2004-10-14 20:47:34 UTC
Permalink
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
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Chris Devers
2004-10-14 21:11:45 UTC
Permalink
Post by Dave Kettmann
Subject speaks for itself..
Okay then.

perldoc -f split

Also speaks for itself :-)
--
Chris Devers
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Chris Devers
2004-10-14 21:15:36 UTC
Permalink
Post by Chris Devers
Post by Dave Kettmann
Subject speaks for itself..
Okay then.
perldoc -f split
Also speaks for itself :-)
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
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Dave Kettmann
2004-10-14 21:19:10 UTC
Permalink
-----Original Message-----
Sent: Thursday, October 14, 2004 4:16 PM
To: Perl List (E-mail)
Cc: Dave Kettmann
Subject: Re: Perl equivalent to the unix 'cut' command
Post by Chris Devers
Post by Dave Kettmann
Subject speaks for itself..
Okay then.
perldoc -f split
Also speaks for itself :-)
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,

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
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Chris Devers
2004-10-14 21:31:16 UTC
Permalink
Post by Dave Kettmann
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.
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
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Dave Kettmann
2004-10-14 21:35:35 UTC
Permalink
Post by Dave Kettmann
Post by Dave Kettmann
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
Post by Dave Kettmann
of them) are approximately 20,000 - 25,000 lines long a
piece. Each of
Post by Dave Kettmann
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.
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
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
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Chris Devers
2004-10-14 21:40:28 UTC
Permalink
Post by Dave Kettmann
Each line is probably 80-100 characters in legnth, the files are about
300Kb each (6 files total) working with 1GB of memory.
Oh, that's it? I'm sure you'll be find then
Post by Dave Kettmann
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 ;)...
Well, among other things, Vim handles big files more smoothly :-)
--
Chris Devers
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
John W. Krahn
2004-10-14 22:16:18 UTC
Permalink
Post by Dave Kettmann
Howdy list,
Hello,
Post by Dave Kettmann
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,
Really? I found it right away.

http://search.cpan.org/~cwest/ppt-0.14/src/cut/cut.hewgill
Post by Dave Kettmann
started looking thru CPAN searching on 'File' but I think
I'm just not thinking of the right keyword that perl uses.
Use 'cut'.
Post by Dave Kettmann
I'm still
new to perl and I'm sorry for my ignorance if it should be right under
my nose
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Continue reading on narkive:
Loading...