Come on Perl, rescue me one more time!

L

laredotornado

Hi,

I'm using Perl 5.8.8 on Mac 10.5.6. I have a text file with the
following content pattern ...

(newline)
line 1
line 2
line 3
(newline)
line 1
line 2
line 3
line 4
line 5
(new line)
line 1
line 2

What I would like to do is remove the first line immediately after any
new line (carriage return) and keep all the other lines. How can I do
this with perl?

Thanks, - Dave
 
J

Jürgen Exner

laredotornado said:
I'm using Perl 5.8.8 on Mac 10.5.6. I have a text file with the
following content pattern ...

(newline)
line 1
line 2
line 3
(newline)
line 1
line 2
line 3
line 4
line 5
(new line)
line 1
line 2

What I would like to do is remove the first line immediately after any
new line (carriage return) and keep all the other lines. How can I do
this with perl?

Where are you stuck? General algorithmic idea? A specific function that
you don't know how to use? What code do you have so far that doesn't
work?

It's about a 5 line program at most. You may find
perldoc -f open
perldoc -f print
perldoc -f next
perldoc -q "change, delete"
as well as if() and while() to be helpful in your task.

jue
 
R

Randal L. Schwartz

laredotornado> What I would like to do is remove the first line immediately after any
laredotornado> new line (carriage return) and keep all the other lines. How can I do
laredotornado> this with perl?

perl -ne 'print unless (/^$/.../^/) =~ /E/'
 
J

jl_post

I have a text file with the following content pattern ...

(newline)
line 1
line 2
line 3
(newline)
line 1
line 2
line 3
line 4
line 5
(new line)
line 1
line 2

What I would like to do is remove the first line immediately after any
new line (carriage return) and keep all the other lines.  How can I do
this with perl?


Dear Dave,

I assume by "new line" you really mean a blank line. If I'm right,
then you can remove the line after every blank line with this simple
one-line Perl script:

perl -lpe "length or <>" input.txt > output.txt

(Warning: This is untested.)

I hope this helps, Dave.

-- Jean-Luc
 
C

C.DeRykus

...

What I would like to do is remove the first line immediately after any
new line (carriage return) and keep all the other lines.  How can I do
this with perl?

perl -pe '<> if /^$/' input
 
J

Josef

Randal said:
laredotornado> What I would like to do is remove the first line immediately after any
laredotornado> new line (carriage return) and keep all the other lines. How can I do
laredotornado> this with perl?

perl -ne 'print unless (/^$/.../^/) =~ /E/'

I prefer
perl -ne '$.&&print;$.=-length'
because it is shorter, and less obfuscated¹.
But OTOH your solution seems more straightforward. ;-)



yes 'i can'²|perl -lpe '$_="Just Another Perl Hacker"'³,
Josef

ad 1) Under the premise that a misuse of a known feature is easier
to understand as the use of a feature which is unknown for the
most of us.
ad 2) …'t get no satisfaction!
ad 3) use warnings; use Acme::Your 'Keyboard'; your $key="\cC";
PS: The (non·)solutions from JeanLuc & DeRykus looks nice, but …
Maybe
perl -pe 'print,<>,chop if /^$/'
perl -pe "length>1 or print,<>,chop"
is near enough.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top