One-Liner Mania

Discussion in 'Perl Misc' started by Lars Eighner, Feb 20, 2010.

  1. Lars Eighner

    Lars Eighner Guest

    Can I capture a group of digits, perform integer arithmetic on them,
    and use them in the replacement?

    Say I have a group of files named Fife_Ch01.html, Fife_Ch02.html
    and so forth. The files all have a line with their own name. I want
    to replace the file name with the next number and do not care about
    leading zeros.

    So,

    Fife_Ch01.html is replaced with 2
    Fife_Ch02.html is replaced with 3
    and so forth.

    Can I do this with a one-liner?

    it would look something like this:

    #perl -pi -e 's/Fife_Ch([0-9]*)\.html{a miracle happens}/\1/' Fife_Ch*.html




    --
    Lars Eighner <http://larseighner.com/> Warbama's Afghaninam day: 80
    1941.0 hours since Warbama declared Viet Nam II.
    Warbama: An LBJ for the Twenty-First century. No hope. No change.
    Lars Eighner, Feb 20, 2010
    #1
    1. Advertising

  2. Lars Eighner

    Marc Girod Guest

    On Feb 20, 10:10 pm, Lars Eighner <> wrote:

    > Can I do this with a one-liner?


    perl -pi -e '($b)=$ARGV=~/^Fife_Ch(\d+)\.html$/;close ARGV if s/$ARGV/
    $b/' Fife_Ch*.html

    On my home box (cygwin), this leaves me with .bak files which I didn't
    expect.

    Marc
    Marc Girod, Feb 20, 2010
    #2
    1. Advertising

  3. Lars Eighner

    Lars Eighner Guest

    In our last episode, <>, the lovely
    and talented Ben Morrow broadcast on comp.lang.perl.misc:

    > Quoth Lars Eighner <>:
    >> Can I capture a group of digits, perform integer arithmetic on them,
    >> and use them in the replacement?


    > You want the /e switch.


    Oh cool. This seems fraught with possibilities.

    >> Say I have a group of files named Fife_Ch01.html, Fife_Ch02.html
    >> and so forth. The files all have a line with their own name. I want
    >> to replace the file name with the next number and do not care about
    >> leading zeros.
    >>
    >> So,
    >>
    >> Fife_Ch01.html is replaced with 2
    >> Fife_Ch02.html is replaced with 3
    >> and so forth.
    >>
    >> Can I do this with a one-liner?
    >>
    >> it would look something like this:
    >>
    >> #perl -pi -e 's/Fife_Ch([0-9]*)\.html{a miracle happens}/\1/' Fife_Ch*.html


    > Don't use \1 on the replacement side of s///. It was a sop to sed users


    But ... but I *am* a sed user! I want my sop!

    Actually I mostly use one-liners in shell scripts. I double-quote lots of
    stuff and just hate escaping $s.

    > put into perl 1 that has never been removed.


    > perl -pi -e's/Fife_Ch(\d*)\.html/$1 + 1/e' Fife_Ch*.html


    --
    Lars Eighner <http://larseighner.com/> Warbama's Afghaninam day: 81
    1949.4 hours since Warbama declared Viet Nam II.
    Warbama: An LBJ for the Twenty-First century. No hope. No change.
    Lars Eighner, Feb 21, 2010
    #3
  4. Lars Eighner

    Marc Girod Guest

    On Feb 20, 11:52 pm, Ben Morrow <> wrote:

    > You want the /e switch.


    I can see that I obviously missed the question,
    focusing as I did on making sure that the single
    string in the file I changed would be the one
    matching the file name, and not even noticing
    the issue of incrementing the number.

    So, thanks for the /e switch which I didn't know.
    Thanks also for the explanation about the .bak
    files on cygwin.

    Now, if the files also happen to contain the
    names of other files in the list, these other
    lines would be modified as well.
    We were not told what else the files contain...
    In fact, I assumed that the first (or an early)
    line would contain the file name, but that there
    could be lots of data below, which is why I
    attempted to close the file after the
    replacement, which with -pi, wasn't a good idea.

    a> cat Fife_Ch3.html
    Fife_Ch0.html
    Fife_Ch1.html
    Fife_Ch2.html
    Fife_Ch3.html
    a> perl -pi -e 's/^Fife_Ch(\d+)\.html$/$1 + 1/e if /^${ARGV}$/'
    Fife_Ch*.html
    a> rm *.bak
    a> ls -l
    total 12
    -rw-r--r--+ 1 marc None 44 2010-02-21 10:40 Fife_Ch0.html
    -rw-r--r--+ 1 marc None 44 2010-02-21 10:40 Fife_Ch1.html
    -rw-r--r--+ 1 marc None 44 2010-02-21 10:40 Fife_Ch2.html
    -rw-r--r--+ 1 marc None 44 2010-02-21 10:40 Fife_Ch3.html
    a> cat Fife_Ch3.html
    Fife_Ch0.html
    Fife_Ch1.html
    Fife_Ch2.html
    4

    Marc
    Marc Girod, Feb 21, 2010
    #4
    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. Mantorok Redgormor

    bitwise increment one liner

    Mantorok Redgormor, Dec 17, 2003, in forum: C Programming
    Replies:
    8
    Views:
    809
    Peter Shaggy Haywood
    Dec 19, 2003
  2. =?ISO-8859-1?Q?Morris_Carr=E9?=

    Python passes the Turing test with a one-liner !

    =?ISO-8859-1?Q?Morris_Carr=E9?=, Apr 1, 2004, in forum: Python
    Replies:
    1
    Views:
    338
    Jon Perez
    Apr 2, 2004
  3. Paul Watson

    Using for in one-liner

    Paul Watson, Aug 15, 2005, in forum: Python
    Replies:
    8
    Views:
    588
    Reinhold Birkenfeld
    Aug 17, 2005
  4. Xah Lee
    Replies:
    0
    Views:
    342
    Xah Lee
    Oct 20, 2005
  5. Larry
    Replies:
    1
    Views:
    99
    Martien Verbruggen
    Feb 3, 2005
Loading...

Share This Page