How to insert a field in between variable data in notepad++

2014-05-29
2014-05-30
  • Shalabh Agarwal

    Shalabh Agarwal - 2014-05-29

    I have a pipe-delimited file with 19 fields in each row as below:

    40|ESIS|016066|$179,517.43|$15,334.73|$0.00|$0.00|$0.00|$0.00|$0.00|$-1,318.55|-$0.00|-$0.00|-$2,293.87|$0.00|-$42.57|-$43.00|$0.00|$191,154.17

    I want to add one field (with -$0.00 value) at 15th position to have 20 fields in each row.

    40|ESIS|016066|$179,517.43|$15,334.73|$0.00|$0.00|$0.00|$0.00|$0.00|$-1,318.55|-$0.00|-$0.00|-$2,293.87|-$0.00|$0.00|-$42.57|-$43.00|$0.00|$191,154.17

    Please help.

     
  • CFrank

    CFrank - 2014-05-30

    Hi Shalabh Agarwal,

    do you know that npp has the so called block select feature?
    By pressing and holding the alt key the mouser cursor can select a block.
    If you select just the space between two chars you can paste whatever you
    want between those lines. Of course this is only useful if the file isn't to big.
    Otherwise you could use find and replace dialog and use regular expression to insert
    the additional data.

    Cheers
    Claudia

     
    • Shalabh Agarwal

      Shalabh Agarwal - 2014-05-30

      Thanks Claudia. My file has 45,000 such rows. So, I am looking replace all using regex.

      My first 2 fields are constant values (40|ESIS|) and rest all fields have variable values.

       
      • CFrank

        CFrank - 2014-05-30

        OK - so search for
        (.+?\|){14}
        and replace with
        $0-\$0.00|

        Cheers
        Claudia

         
  • THEVENOT Guy

    THEVENOT Guy - 2014-05-30

    Hello Shalabh Agarwal and Claudia,

    No problem : everything is possible with N++ :-) We'll use a Search/Replacement with regular expressions :-)

    So :

    • Open the Replace dialog ( CTRL + H )

    • Check the Regular expression radio button

    • Check the Wrap around option, if necessary

    • Type in the Find what and Replace with zones, the regexes below :

    SEARCH : (.+?\|){14}

    REPLACE : $0-\$0.00|

    • Click on the Replace All button

    Et voilà !

    To sum up :

    • We are searching for, exactly, 14 non-empty blocks, made of any standard characters ( except the pipe character ) till a pipe character (.+?\|)

    • And, then, we must replace the matched string by itself $0, followed by your text -\$0.00|

    Just note three points :

    • The pipe character, in the search part, must be escaped as it's the symbol of an alternation

    • The symbole $, in the replacement part, must be escaped, as the string $0 represents, usually, the entire matched string

    • If a field, between two pipe characters, may be empty, just change the search regex into (.*?\|){14}

    Cheers,

    guy038

    P.S. :

    You'll find good documentation, about the new Boost C++ Regex library ( similar to the PERL Regular Common Expressions ) used by N++, since the 6.0 version, at the TWO addresses below :

    http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html

    http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html

    The FIRST link explains the syntax, of regular expressions, in the SEARCH part

    The SECOND link explains the syntax, of regular expressions, in the REPLACEMENT part

     
    Last edit: THEVENOT Guy 2014-05-30
    • Shalabh Agarwal

      Shalabh Agarwal - 2014-05-30

      Excellent. This works. Thanks heaps. One more point though. I forgot to mention that I also have other rows starting with 10|ESIS| or 20|ESIS| which also have around 20 fields and (.+?\|){14} is searching those rows too. But I only want to modify the rows starting with 40|ESIS|. Could you please advise?

      Thanks for those links, it's very good doco.

       
  • CFrank

    CFrank - 2014-05-30

    oopppsss - sorry THEVENOT Guy , I need to refresh the page more often.
    @Shalabh Agarwal - check THEVENOT Guys solution and explanation - it is wonderful.

    Cheers
    Claudia

     
    • THEVENOT Guy

      THEVENOT Guy - 2014-05-30

      Hi Claudia,

      We just found out the same regexes, at the same time :-) Amazing !

      Really, with Notepad++, we have the choice between Build-in commands, Bookmarks, Macros, Regular expressions and Plugins, particularly the Python plugin :-)

      In the same way, a medieval poet said, before, I would say :

      I appreciate Notepad++, today, more than yesterday and still less than tomorrow !

      Cheers,

      guy038

       
      Last edit: THEVENOT Guy 2014-05-30
      • CFrank

        CFrank - 2014-05-30

        Hi "guy" ;-)

        to be honest, this wasn't mine idea - I used grouping, which worked but wasn't very elegant. A collegue told me to change this - hmm now I'm curious where he did get
        this info from? Nightshift begins in 7 hours - let's see.

        I love npp too - for me it's a time saver every day. :-)

        Cheers
        Claudia

         
    • Shalabh Agarwal

      Shalabh Agarwal - 2014-05-30

      Thanks Claudia. I also just noticed you gave same solution as THEVENOT Guy. I need to refresh page more often too.

      Cheers
      Shalabh

       
  • THEVENOT Guy

    THEVENOT Guy - 2014-05-30

    Hi Shalabh Agarwal,

    This shouldn't be very difficult to achieve. Just force the first field to be 40, in the search regex :

    SEARCH : 40\|(.+?\|){13}

    Again, after the number 40, the pipe character must be escaped

    Best Regards,

    guy038

    P.S. :

    Sorry, Shalabh and Claudia, but now, in France, it's about 4.00 a.m. and I'd better go to bed and stop reading N++ forums !!

    Today, it was a public holiday, but, unfortunately, I must work tomorrow :-(

    See you later,

    Cheers

     
    Last edit: THEVENOT Guy 2014-05-30
  • Shalabh Agarwal

    Shalabh Agarwal - 2014-05-30

    Thanks again Guy. Really appreciate it.

     
  • THEVENOT Guy

    THEVENOT Guy - 2014-05-30

    Hi Shalabh Agarwal,

    Just in case, one of the five first columns ( of a line, beginning with the unwanted number 10 or 20 ) would end with the two digits 40, we should anchor the second search to the beginning of each line ! So the right search regex should be :

    SEARCH : ^40\|(.+?\|){13}

    Cheers,

    guy038

     
    Last edit: THEVENOT Guy 2014-05-30

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks