Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Come on Perl, rescue me one more time!

Reply
Thread Tools

Come on Perl, rescue me one more time!

 
 
laredotornado
Guest
Posts: n/a
 
      12-12-2009
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      12-12-2009
laredotornado <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
 
 
 
Randal L. Schwartz
Guest
Posts: n/a
 
      12-13-2009
>>>>> "laredotornado" == laredotornado <(E-Mail Removed)> writes:

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/'

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
 
Reply With Quote
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      12-13-2009
On Dec 12, 2:16*pm, laredotornado <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      12-14-2009
On Dec 12, 1:16*pm, laredotornado <(E-Mail Removed)> wrote:
> ...
>
> 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

--
Charles DeRykus

 
Reply With Quote
 
Josef
Guest
Posts: n/a
 
      12-15-2009
Randal L. Schwartz schrieb:
>>>>>> "laredotornado" == laredotornado <(E-Mail Removed)> writes:

>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
DVD Verdict reviews: GRANDMA'S BOY, SNOOPY, COME HOME, and more! DVD Verdict DVD Video 0 05-18-2006 08:20 AM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
Come One, Come All Jess Guim Digital Photography 0 12-02-2003 04:43 PM



Advertisments