how to add printf(); after each line.



  • input:

    kamal
    kamal
    kamal
    kamal
    kamal
    kamal
    kamal
    kamal

    OUTPUT:

    kamal
    printf(“kamal log 1 x=%d\n”,x);
    kamal
    printf(“kamal log 2 x=%d\n”,x);
    kamal
    printf(“kamal log 3 x=%d\n”,x);
    kamal
    printf(“kamal log 4 x=%d\n”,x);
    kamal
    printf(“kamal log 5 x=%d\n”,x);
    kamal
    printf(“kamal log 6 x=%d\n”,x);
    kamal
    printf(“kamal log 7 x=%d\n”,x);
    kamal
    printf(“kamal log 8 x=%d\n”,x);
    kamal
    printf(“kamal log 9 x=%d\n”,x);
    kamal
    printf(“kamal log 10 x=%d\n”,x);



  • @Kamal-lochan-0 ,

    First, where do the extra two lines come from? You had 8 “kamal” lines, but 10 pairs of output lines. I am going to assume that you don’t want to magically create extra lines during the search/replace.

    Second, are you really going to have N lines that are all identical to start with? Or are each of those lines going to really have different text? Because I doubt it’s really the same every time (otherwise, you would just copy/paste the same pair of lines 10x), I am going to assume it can be different each line.

    Third, in your printf, you seem to replicate the text from the found line in the printf string. But since you (and the @kamal-lochan from the december questions) seem to use your name as a dummy string in your example text, I cannot tell for sure. I am going to assume that if the text were kamal then peter then forum, the text in the printf would change.

    Fourth, you have the auto-increment in your printf numbers. As @kamal-lochan was told in December, the generic search/replace built into Notepad++ cannot do auto-increment. So, you’re going to have two options: either a combination of the regex and column-editor, as @Terry-R suggested in December, or use the PythonScript solution very similar to the one that @Alan-Kilborn suggested in December

    Quite honestly, if you are coding in a language that uses printf, you should really just make a for-loop and make the auto-increment in the printf by printing the loop variable. Doing repetative coding by regex just shows that you aren’t thinking about loops and subroutines/functions, which is really bad coding style. But this isn’t a coding forum, so moving on…

    pure-Notepad++ solution

    1. FIND = (?-s)^.+$
      REPLACE = $0\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20printf\("$0 log ## x=%d\\n",x\);
      MODE = regular expression
      REPLACE ALL
      =>
      kamal                    printf("kamal log ## x=%d\n",x);
      peter                    printf("peter log ## x=%d\n",x);
      forum                    printf("forum log ## x=%d\n",x);
      kamal                    printf("kamal log ## x=%d\n",x);
      peter                    printf("peter log ## x=%d\n",x);
      forum                    printf("forum log ## x=%d\n",x);
      kamal                    printf("kamal log ## x=%d\n",x);
      peter                    printf("peter log ## x=%d\n",x);
      forum                    printf("forum log ## x=%d\n",x);
      kamal                    printf("kamal log ## x=%d\n",x);
      peter                    printf("peter log ## x=%d\n",x);
      forum                    printf("forum log ## x=%d\n",x);
      
    2. Column-select the ##, Edit > Column Editor, then ☑ Number to Insert , Initial Number: 1, Increase by: 1
      =>
      kamal                    printf("kamal log 1  x=%d\n",x);
      peter                    printf("peter log 2  x=%d\n",x);
      forum                    printf("forum log 3  x=%d\n",x);
      kamal                    printf("kamal log 4  x=%d\n",x);
      peter                    printf("peter log 5  x=%d\n",x);
      forum                    printf("forum log 6  x=%d\n",x);
      kamal                    printf("kamal log 7  x=%d\n",x);
      peter                    printf("peter log 8  x=%d\n",x);
      forum                    printf("forum log 9  x=%d\n",x);
      kamal                    printf("kamal log 10 x=%d\n",x);
      peter                    printf("peter log 11 x=%d\n",x);
      forum                    printf("forum log 12 x=%d\n",x);
      
    3. split the lines
      FIND = \s*printf
      REPLACE = \r\nprintf
      REPLACE ALL

    I did leave out an important idea: if kamal, peter, and forum were all 5 characters, they will work fine; but if you have text of different length, the column editor won’t line up. It’s possible to add another regex or two between step 1 and step 2 to align everything, to make column editor happy… but if you’ve got text that is different length, and if you (like @kamal-lochan) have already installed PythonScript, I think a PythonScript solution will be more robust. If you really do have multiple lengths of text, I would highly recommend the PythonScript solution, so that’s all I will provide

    PythonScript solution

    from Npp import *
    
    count = 0
    
    def add_1(m):
        global count
        count += 1
        kamal = m.group().rstrip()
        return kamal + "\r\n" + 'printf("' + kamal + ' log ' + str(count) + ' x=%d\\n", x);'
    
    editor.rereplace(r'(?-s)^.+$', add_1);
    

    which converts

    kamal
    peter
    longer text with spaces
    kamal
    peter
    longer text with spaces
    kamal
    peter
    longer text with spaces
    kamal
    peter
    longer text with spaces
    

    to

    kamal
    printf("kamal log 1 x=%d\n", x);
    peter
    printf("peter log 2 x=%d\n", x);
    longer text with spaces
    printf("longer text with spaces log 3 x=%d\n", x);
    kamal
    printf("kamal log 4 x=%d\n", x);
    peter
    printf("peter log 5 x=%d\n", x);
    longer text with spaces
    printf("longer text with spaces log 6 x=%d\n", x);
    kamal
    printf("kamal log 7 x=%d\n", x);
    peter
    printf("peter log 8 x=%d\n", x);
    longer text with spaces
    printf("longer text with spaces log 9 x=%d\n", x);
    kamal
    printf("kamal log 10 x=%d\n", x);
    peter
    printf("peter log 11 x=%d\n", x);
    longer text with spaces
    printf("longer text with spaces log 12 x=%d\n", x);
    

    Finally, while this ended up being a mixed solution, you are still generally asking a search/replace question, so the following advice should be heeded:

    ----

    Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the </> toolbar button or manual Markdown syntax. To make regex in red (and so they keep their special characters like *), use backticks, like `^.*?blah.*?\z`. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.



  • i do not understand how to install the script is given in

    https://community.notepad-plus-plus.org/post/54655

    ![0_1617515446372_Capture.PNG](Uploading 100%)

    kamal.py file :

    from Npp import *

    count = 0

    def add_1(m):
    global count
    count += 1
    kamal = m.group().rstrip()
    return kamal + “\r\n” + ‘printf("’ + kamal + ’ log ’ + str(count) + ’ x=%d\n", x);’

    editor.rereplace(r’(?-s)^.+$’, add_1);

    My Doubt is how can i run and use this script ?

    can u please provide the screen capture s of your steps ?



  • @Kamal-lochan-0 ,

    how can i run and use this script ?

    By following the clear, step-by-step instructions that you just linked to.

    please provide the screen capture

    That post you linked was pretty explicit about the steps necessary. But if screen captures are needed, I will intersperse them between the quoted version of the plain text instructions from that post:

    • Use Plugins admin to install the PythonScript plugin

    a3c12d0c-4c77-4ca5-996e-5d3373ba19e6-image.png

    • Menu to Plugins > Python Script > New Script

    512db8aa-3d1f-4e05-81b7-b0f4dbde84f1-image.png

    • Provide a (file) name for the script; you don’t need to specify an extension – .py will be added automatically
    • Press the Save button to close out the Save As dialog box.

    98d60736-7684-4a9c-b8ee-980da87ac037-image.png

    • At this point your script file will be showing and is empty; copying and pasting one of the scripts from above seems like a fine idea :-)

    after pasting, it will look like:

    eaa461d0-85d0-40e3-b54c-3f30ad93e493-image.png

    Please note: your post lost the leading spaces when you quoted the script. In Python, indentation carries meaning, so make sure you actually copy/paste the text from my post, not from yours that has lost the spaces. If you disregard this advice, it will not work. It should look like the screenshot above.

    • Save the file

    cff43f73-a456-4086-9372-c7d495feb887-image.png

    or

    8ea24b13-e7a4-472f-9ffc-6bbcf41ffba8-image.png

    • To execute your script, menu to Plugins > Python Script >Scripts > (pick the name of your script)

    563543d8-e217-4dc1-a504-7c7e069c391f-image.png

    And after running that, you will have a file that looks like:
    c78137e3-8a3f-4a77-b936-d8b0bafb0b3a-image.png

    Those screenshots are just a direct translation of the text of Alan’s instructions into the application.


Log in to reply