Substitute across lines in "one liner" 
Author Message
 Substitute across lines in "one liner"

I am trying to make a "one liner" that substitute two lines with one.
The lines have only one "$" in them. I tryed:

perl -p -e"s/\$\n\$/\s/"

but it don't work.

HELP =:-(
_________________________________________________________
Example:

Some text
Some text
Some text
$
$
Some text
Some text
Some text

Should change to:

Some text
Some text
Some text
$
Some text
Some text
Some text



Wed, 28 Jul 2004 23:41:59 GMT  
 Substitute across lines in "one liner"

Quote:

> I am trying to make a "one liner" that substitute two lines with one.
> The lines have only one "$" in them.
> Some text
> Some text
> Some text
> $
> $
> Some text
> Some text
> Some text

> Should change to:

> Some text
> Some text
> Some text
> $
> Some text
> Some text
> Some text

Substitute might be tricky or impossible. Try a print after testing if it
is "$" and has already been printed the last time, too:

perl -ne 'print unless($last eq "\$\n" && $last eq $_);$last=$_;' test.txt

Andi.



Thu, 29 Jul 2004 01:18:54 GMT  
 Substitute across lines in "one liner"

Quote:

> I am trying to make a "one liner" that substitute two lines
> with one.  The lines have only one "$" in them. I tryed:

> perl -p -e"s/\$\n\$/\s/"

> but it don't work.

You're processing one line at a time.

Either slurp the file:

  $ perl -0777 -pe 's{(\$\n)+}{$1}g'

Or remember the last line.

  $ left as an exercise ;)

Steve
--



Thu, 29 Jul 2004 02:02:35 GMT  
 Substitute across lines in "one liner"

Quote:

>I am trying to make a "one liner" that substitute two lines with one.
>The lines have only one "$" in them. I tryed:

>perl -p -e"s/\$\n\$/\s/"

>but it don't work.

This Perl FAQ addresses the problem you are having:

   "I'm having trouble matching over more than one line.  What's wrong?"

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Thu, 29 Jul 2004 06:02:41 GMT  
 Substitute across lines in "one liner"

Quote:

>I am trying to make a "one liner" that substitute two lines with one.
>The lines have only one "$" in them. I tryed:

>perl -p -e"s/\$\n\$/\s/"

>but it don't work.

It doesn't work because the code after your "-e" is executed once
for each line of input.

     -p   causes Perl to assume the following loop around your
          program, which makes it iterate over filename arguments
          somewhat like sed:

            LINE:
              while (<>) {
                  ...             # your program goes here
              } continue {
                  print or die "-p destination: $!\n";
              }

So each time your code "s/\$\n\$/\s/" is run, $_ only contains a single
line from the input.

  - Logan
--
"I'll tell you something.  Luxury disgusts me."  Giorgio Armani, Jan 17, 2002
( http://dailynews.yahoo.com/h/nm/20020117/re/life_fashion_armani_dc_1.... )



Thu, 29 Jul 2004 07:24:42 GMT  
 Substitute across lines in "one liner"

Quote:


> > I am trying to make a "one liner" that substitute two lines
> > with one.  The lines have only one "$" in them. I tryed:

> > perl -p -e"s/\$\n\$/\s/"

> > but it don't work.

> You're processing one line at a time.

> Either slurp the file:

>   $ perl -0777 -pe 's{(\$\n)+}{$1}g'

> Or remember the last line.

>   $ left as an exercise ;)

Yet another alternative is to read the input in multi-line chunks
("paragraphs") and print only the non-empty chunks:

  $ perl -ne 'BEGIN { $/ = "\n\$" } chomp; print "$_$/" if length'

This is more scaleable than the file slurping way.

Notice you must restore the just-chomped record separator to the end of
the record when you print it. This script breaks if spaces occur on the
same line as the dollar signs.

I set $/ to "\n\$" and not "\$\n" because it seems to me that the
significant characteristic of the dollar sign record separator/terminator
is that it occurs at the beginning of the line. Consider, for example, a
line in the file consisting of the string "Some text $\n".

--
Jim Monty

Tempe, Arizona USA



Thu, 29 Jul 2004 07:54:43 GMT  
 Substitute across lines in "one liner"

Quote:

> I am trying to make a "one liner" that substitute two lines with one.
> The lines have only one "$" in them. I tryed:

> perl -p -e"s/\$\n\$/\s/"

> but it don't work.

> HELP =:-(
> _________________________________________________________
> Example:

> Some text
> Some text
> Some text
> $
> $
> Some text
> Some text
> Some text

> Should change to:

> Some text
> Some text
> Some text
> $
> Some text
> Some text
> Some text

perl -p0777 -e "s/\$\n\$/\$/g"
[untested]

--
From the libwww-perl changelog, describing an error fixed in 5.41:
 o  The local/http.t test actually did try to unlink(".").  This was
    very confusing on systems where it succeed.



Thu, 29 Jul 2004 08:17:00 GMT  
 Substitute across lines in "one liner"

Quote:

> Yet another alternative is to read the input in multi-line chunks
> ("paragraphs") and print only the non-empty chunks:

>   $ perl -ne 'BEGIN { $/ = "\n\$" } chomp; print "$_$/" if length'

> This is more scaleable than the file slurping way.

> Notice you must restore the just-chomped record separator to the end of
> the record when you print it.

Gosh, doesn't that seem silly and needless? So we just modify the test
for empty chunks:

  $ perl -ne 'BEGIN { $/ = "\n\$" } print unless $_ eq "\n\$"'

By the way, is there a way to set the value of $/ to the string "\n\$"
on the command line?

--
Jim Monty

Tempe, Arizona USA



Fri, 30 Jul 2004 05:00:21 GMT  
 Substitute across lines in "one liner"

Quote:


>  $ perl -ne 'BEGIN { $/ = "\n\$" } print unless $_ eq "\n\$"'

>By the way, is there a way to set the value of $/ to the string "\n\$"
>on the command line?

Eh? The code shown does what you ask on the command line.

Did you mean to ask something else?

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Fri, 30 Jul 2004 18:40:54 GMT  
 Substitute across lines in "one liner"

Quote:


> >   $ perl -ne 'BEGIN { $/ = "\n\$" } print unless $_ eq "\n\$"'

> > By the way, is there a way to set the value of $/ to the string "\n\$"
> > on the command line?

> Eh? The code shown does what you ask on the command line.

> Did you mean to ask something else?

Yes.

I meant to ask if there is a way to set the initial value of the
variable $/ to the string "\n\$" using a perl command-line option
and argument other than the script option -e and its argument, a
Perl script?

--
Jim Monty

Tempe, Arizona USA



Fri, 30 Jul 2004 21:13:38 GMT  
 Substitute across lines in "one liner"
perl -p0777 -e "s/\$\n\$/\$/g"

works fine but wat does "0777" stand for?

Thanks to all of you

Marteinn Sverrisson, system administrator
Netskil hf.
www.netskil.is



Sat, 31 Jul 2004 22:52:25 GMT  
 Substitute across lines in "one liner"

Quote:

>perl -p0777 -e "s/\$\n\$/\$/g"

>works fine but wat does "0777" stand for?

How to run perl, including its command line switches, is
documented in:

   perldoc perlrun

------------------------
=item -0[digits]

...

The special value 00 will cause Perl to slurp files in paragraph mode.
The value 0777 will cause Perl to slurp files whole because there is no
legal character with that value.
------------------------

So "0777" stands for "slurp the entire file into memory".

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sat, 31 Jul 2004 23:55:53 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. gave a2p, s2p a one-liner, want one-liner back

2. Dyn-IP "wanna be" 1-liner

3. how to safely substitute a "(", ")" or "["

4. "Static" hash across recurisive function

5. substituting "@" with regexp

6. substituting with /g and simulating "lookbehind"

7. split line like "a","b","c"

8. "character class ""bug""

9. Command line One liner challenge

10. windows one liner to output unix line feed

11. one liner in command line

12. problems with one-liners on the command line

 

 
Powered by phpBB® Forum Software