Using xargs perl to join lines

Discussion in 'Perl Misc' started by charissaf@gmail.com, Jul 10, 2007.

  1. Guest

    Hello,
    FreeBSD 4.10
    #!/bin/ksh

    I'm looking for some guidance on how to use a combination of grep and
    xargs to find lines in multpile files beginning with > and append them
    to the previous line.

    For example, ideally I would like to use:

    grep -r -l '^>' . | xargs perl -pi -e :a -e '$!N;s/\n>/>/;ta' -e 'P;D'

    I am basing this one liner on the following sed example:

    # if a line begins with an equal sign, append it to the previous line
    # and replace the "=" with a single space
    # sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'

    The error I receive is: N: Event not found.

    A sample file would have a line similar to
    <htm
    >


    The ideal result is to bring the closing > up to the preceeding line:
    <htm>

    Please look at my syntax or let me know if there is another way of
    doing this operation using xargs perl.

    Thank you!
    C
    , Jul 10, 2007
    #1
    1. Advertising

  2. Guest

    On Jul 10, 9:06 am, wrote:
    > Hello,
    > FreeBSD 4.10
    > #!/bin/ksh
    >
    > I'm looking for some guidance on how to use a combination of grep and
    > xargs to find lines in multpile files beginning with > and append them
    > to the previous line.
    >
    > For example, ideally I would like to use:
    >
    > grep -r -l '^>' . | xargs perl -pi -e :a -e '$!N;s/\n>/>/;ta' -e 'P;D'
    >
    > I am basing this one liner on the following sed example:
    >
    > # if a line begins with an equal sign, append it to the previous line
    > # and replace the "=" with a single space
    > # sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'
    >
    > The error I receive is: N: Event not found.
    >
    > A sample file would have a line similar to
    > <htm
    >
    >
    >
    > The ideal result is to bring the closing > up to the preceeding line:
    > <htm>
    >
    > Please look at my syntax or let me know if there is another way of
    > doing this operation using xargs perl.
    >
    > Thank you!
    > C


    More info:
    sample file has
    <htm
    >


    Also, I turned on the perl -w option and have the following info:
    Bareword found where operator expected at -e line 2, near "$!N"
    (Missing operator before N?)
    Can't call method "a" without a package or object reference at -e line
    1, <> chunk 1.
    , Jul 10, 2007
    #2
    1. Advertising

  3. wrote:
    > Hello,
    > FreeBSD 4.10
    > #!/bin/ksh
    >
    > I'm looking for some guidance on how to use a combination of grep and
    > xargs to find lines in multpile files beginning with > and append them
    > to the previous line.
    >
    > For example, ideally I would like to use:
    >
    > grep -r -l '^>' . | xargs perl -pi -e :a -e '$!N;s/\n>/>/;ta' -e 'P;D'
    >
    > I am basing this one liner on the following sed example:
    >
    > # if a line begins with an equal sign, append it to the previous line
    > # and replace the "=" with a single space
    > # sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'
    >
    > The error I receive is: N: Event not found.


    Of course Perl cannot run sed code just as sed cannot run Python code.


    > A sample file would have a line similar to
    > <htm
    >
    > The ideal result is to bring the closing > up to the preceeding line:
    > <htm>
    >
    > Please look at my syntax or let me know if there is another way of
    > doing this operation using xargs perl.


    $ echo "
    <html


    >

    " | perl -076pe's/\s+>$/>/'

    <html>




    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Jul 10, 2007
    #3
  4. Guest

    On Jul 10, 12:05 pm, "John W. Krahn" <> wrote:
    > wrote:
    > > Hello,
    > > FreeBSD 4.10
    > > #!/bin/ksh

    >
    > > I'm looking for some guidance on how to use a combination of grep and
    > > xargs to find lines in multpile files beginning with > and append them
    > > to thepreviousline.

    >
    > > For example, ideally I would like to use:

    >
    > > grep -r -l '^>' . | xargs perl -pi -e :a -e '$!N;s/\n>/>/;ta' -e 'P;D'

    >
    > > I am basing this one liner on the followingsedexample:

    >
    > > # if alinebegins with an equal sign, append it to thepreviousline
    > > # and replace the "=" with a single space
    > > #sed-e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'

    >
    > > The error I receive is: N: Event not found.

    >
    > Of course Perl cannot runsedcode just assedcannot run Python code.
    >
    > > A sample file would have alinesimilar to
    > > <htm

    >
    > > The ideal result is to bring the closing > up to the preceedingline:
    > > <htm>

    >
    > > Please look at my syntax or let me know if there is another way of
    > > doing this operation using xargs perl.

    >
    > $ echo "
    > <html
    >
    > >

    > " | perl -076pe's/\s+>$/>/'
    >
    > <html>
    >
    > John
    > --
    > Perl isn't a toolbox, but a small machine shop where you
    > can special-order certain sorts of tools at low cost and
    > in short order. -- Larry Wall- Hide quoted text -
    >
    > - Show quoted text -


    Wow, thank you for your help!
    , Jul 10, 2007
    #4
  5. Joe Smith wrote:
    > wrote:
    >
    >> # sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'
    >>
    >> The error I receive is: N: Event not found.

    >
    > Are you aware that some shells require ! to be quoted as \! even
    > inside ''? -Joe


    My bash shell doesn't treat ! as special when in single quotes.
    Otherwise, it acts as a histoy shortcut:

    $ echo 12345
    12345
    $ echo 123
    123
    $ echo ABC
    ABC
    $ history | tail -n 4 | head -n 3
    25767 echo 12345
    25768 echo 123
    25769 echo ABC
    $ perl -e 'print "[", shift, "]\n";' "!25767"
    perl -e 'print "[", shift, "]\n";' "echo 12345"
    [echo 12345]
    $ perl -e 'print "[", shift, "]\n";' !25767
    perl -e 'print "[", shift, "]\n";' echo 12345
    [echo]
    $ perl -e 'print "[", shift, "]\n";' '!25767'
    [!25767]
    $ perl -e 'print "[", shift, "]\n";' \!25767
    [!25767]
    $ echo $SHELL
    /bin/bash
    $ bash --version
    GNU bash, version 2.05a.0(1)-release (i686-pc-linux-gnu) ...


    Howver, I checked with tcsh, and it doesn't seem to care if it's single
    quoted or not (kinda odd?)

    $ echo 123
    123
    $ echo 456
    456
    $ echo 789
    789
    $ history | tail -n 4 | head -n 3
    2 9:27 echo 123
    3 9:27 echo 456
    4 9:27 echo 789
    $ perl -e 'print "[", shift, "]\n";' "!3"
    perl -e 'print "[", shift, "]\n";' "echo 456"
    [echo 456]
    $ perl -e 'print "[", shift, "]\n";' '!3'
    perl -e 'print "[", shift, "]\n";' 'echo 456'
    [echo 456]
    $ perl -e 'print "[", shift, "]\n";' '\!3'
    [!3]
    $ echo $version
    tcsh 6.10.00 (Astron) 2000-11-19 (i386-intel-linux) ...


    I thought all shells are not supposed regard single quoted characters as
    special?


    > linux% date
    > Thu Jul 12 01:43:44 PDT 2007
    > linux% echo "a!N."
    > N.: Event not found.
    > linux% echo !da
    > echo date
    > date
    > linux% echo $version
    > tcsh 6.14.00 (Astron) 2005-03-25 (i386-intel-linux) ...
    > linux% echo "a\!N."
    > a!N.




    --
    CL
    Clenna Lumina, Jul 12, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    880
    Alan Silver
    Jun 5, 2006
  2. googleboy
    Replies:
    1
    Views:
    920
    Benji York
    Oct 1, 2005
  3. Markus Dehmann
    Replies:
    1
    Views:
    135
    Tad McClellan
    Sep 26, 2006
  4. nickli

    Using Perl to join lines in a file

    nickli, Nov 7, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    86
    John Bokma
    Nov 8, 2006
  5. julia

    join lines - perl command

    julia, Sep 7, 2009, in forum: Perl Misc
    Replies:
    2
    Views:
    104
Loading...

Share This Page