Perl Cut Command

B

Buck Turgidson

I have a file of data that is 60 bytes long. I want to cut (remove) bytes
21-40, and just print 1-20 and 21 thru 60 on a line.

The Unix cut command does the opposite of what I want. "cut --bytes=21-40"
returns those bytes, but I want to remove them.

Is there a simple perl one-liner that will remove them?

I appreciate it.
 
B

Buck Turgidson

21-40, and just print 1-20 and 21 thru 60 on a line.
That should read "just print 1-20 and 41 thru 60 on a line"
 
D

David K. Wall

Buck Turgidson said:
That should read "just print 1-20 and 41 thru 60 on a line"

[untested]

perl -ne "print substr($_,0,20), substr($_,40,20)" filename
 
C

Charles DeRykus

I have a file of data that is 60 bytes long. I want to cut (remove) bytes
21-40, and just print 1-20 and 21 thru 60 on a line.

The Unix cut command does the opposite of what I want. "cut --bytes=21-40"
returns those bytes, but I want to remove them.

Is there a simple perl one-liner that will remove them?

perl -nle 'print /(.{20}).{40}(.*)/' <file>
 
B

Ben Morrow

Buck Turgidson said:
That should read "just print 1-20 and 41 thru 60 on a line"

perl -ne'BEGIN { $/ = \1 } print if $. < 21 or $. > 40'

perl -ne'print ((unpack "a"x60, $_)[0..19,40..59])'

perl -ne'print unpack "a20x20a20", $_'

perl -pe'pos = 21; s/\G.{20}//'

Ben
 
M

Michael Zawrotny

Tintin said:
$ perldoc -f cut
No documentation for perl function 'cut' found

The "cut" in question was the *nix utility. The OP was having trouble
getting those columns from a file with cut, and went looking for a
perl solution when it wasn't working, so I showed how cut could do the
job. See "man cut" for details.


Mike
 
T

Tintin

Michael Zawrotny said:
The "cut" in question was the *nix utility. The OP was having trouble
getting those columns from a file with cut, and went looking for a
perl solution when it wasn't working, so I showed how cut could do the
job. See "man cut" for details.

I should have added my sarcasm tags. This is a newsgroup for discussing
Perl.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top