1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to make line numbers fixed width in FFIND

Discussion in 'Support' started by dcantor, Jun 10, 2015.

  1. dcantor

    Joined:
    May 29, 2008
    Messages:
    507
    Likes Received:
    3
    When using FFIND /L , line numbers are rendered as a [n] where n has no leading zeroes. Is there any way to cause the line numbers to be formatted with leading zeroes, like [01] or [001]?
     
  2. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,804
    Likes Received:
    82
  3. Christian Albaret

    Joined:
    Jul 1, 2008
    Messages:
    154
    Likes Received:
    1
    It's not too hard with Perl:
    Code:
    ffind /L … | perl -p -e "s/\[(\d+)\]/sprintf(q([%%%%03i]),$1)/e;"
    
    Here the %03i (plus escaping percents-%) formats to 3 digits with leading zeros.
     
    #3 Christian Albaret, Jun 11, 2015
    Last edited: Jun 11, 2015
  4. dcantor

    Joined:
    May 29, 2008
    Messages:
    507
    Likes Received:
    3
    Thanks for the perl of wisdom. I'll try that.
    ...

    I did try it. I downloaded and installed Strawberry Perl, and it worked after I modified the string of four consecutive percent signs down to only two of them.
     
    #4 dcantor, Jun 11, 2015
    Last edited: Jun 11, 2015
  5. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,287
    Likes Received:
    39
    Boooo......
     
  6. dcantor

    Joined:
    May 29, 2008
    Messages:
    507
    Likes Received:
    3
    I can't resist. Have pun, will babble.
     
  7. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,188
    Likes Received:
    11
    This method doesn't depend on Perl.
    Code:
    do l in /p ffind /l /v /t"Take" readme.txt (echo.%@rereplace["\[(\d+)\](.+)",[%@format[03,\1]]\2,%l])
    
    However, there appears to be a bug in @format. I imagine there's a way to do it using the built-in Lua interpreter too, or tpipe.
     
  8. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,188
    Likes Received:
    11
    Here's one that uses the ridiculously complicated TPIPE
    Code:
    tpipe /input=readme.txt /line=1,1,0,0,-7.5d /grep=3,0,0,0,0,0,0,0,Take
    
     
  9. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,804
    Likes Received:
    82
    Ridiculously complicated? It's half the size (and a lot simpler) than your previous solutions!
     
  10. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,188
    Likes Received:
    11
    It's kind of like programming in APL. You have no idea what the code does and it's usually easier just to rewrite it than trying to figure it out. It took me 20 minutes of futzing with the switches and various options before I figured it out.
     
  11. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,883
    Likes Received:
    29
    Nice one, Scott. It only took an hour to figure this out, but you can get the []s in there too.
    Code:
    g:\tc18> tpipe /input=readme.txt /line=1,1,0,0,-7.5d /grep=3,0,0,0,0,0,0,0,Take /replace=4,0,0,0,0,0,0,0,0,"^^(\d+?)",[$1]
    [00001]  Take Command v18.0
    [00012]  Greetings, and thanks for trying Take Command!
    <snip>
     
  12. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,188
    Likes Received:
    11
    I was going to suggest a regex that specified the 5 digits at the start of the line. But you got there.
     

Share This Page