• Login
Community
  • Login

Adding a character and a number (as part of a sequence) at the end of each line

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 3 Posters 4.3k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V
    Viktoria Ontapado
    last edited by Nov 25, 2017, 5:13 AM

    Hello All,

    I have a long list of words:

    apple
    apricot
    avocado
    banana
    blackberry
    blackcurrant
    .
    .
    .

    and I’d like to achieve the following format:

    apple/1
    apple/2
    apple/3
    apple/4
    apple/5
    apple/6
    apricot/1
    apricot/2
    apricot/3
    apricot/4
    apricot/5
    apricot/6
    avocado/1
    avocado/2
    avocado/3
    avocado/4
    avocado/5
    avocado/6
    banana/1
    banana/2
    banana/3
    banana/4
    banana/5
    banana/6
    blackberry/1
    blackberry/2
    blackberry/3
    blackberry/4
    blackberry/5
    blackberry/6
    blackcurrant/1
    blackcurrant/2
    blackcurrant/3
    blackcurrant/4
    blackcurrant/5
    blackcurrant/6
    .
    .
    .

    Every fruit needs exactly six instances.
    What way can I achieve this?

    Thank you and have a nice day,
    Viktoria

    1 Reply Last reply Reply Quote 0
    • M
      Meta Chuh moderator
      last edited by Nov 25, 2017, 9:10 PM

      @Viktoria-Ontapado

      in your given example you can do this by doing a search/replace using regular expressions
      open the search replace window and select regular expression in the lower left corner

      then use following search/replace strings:
      find what: ^(.*?)$
      replace with: $1/1\r\n$1/2\r\n$1/3\r\n$1/4\r\n$1/5\r\n$1/6

      and hit replace all

      so if your text was:
      apple
      apricot
      avocado
      banana
      blackberry
      blackcurrant

      you will get:
      apple/1
      apple/2
      apple/3
      apple/4
      apple/5
      apple/6
      apricot/1
      apricot/2
      apricot/3
      apricot/4
      apricot/5
      apricot/6
      avocado/1
      avocado/2
      avocado/3
      avocado/4
      avocado/5
      avocado/6
      banana/1
      banana/2
      banana/3
      banana/4
      banana/5
      banana/6
      blackberry/1
      blackberry/2
      blackberry/3
      blackberry/4
      blackberry/5
      blackberry/6
      blackcurrant/1
      blackcurrant/2
      blackcurrant/3
      blackcurrant/4
      blackcurrant/5
      blackcurrant/6

      1 Reply Last reply Reply Quote 0
      • V
        Viktoria Ontapado
        last edited by Nov 26, 2017, 5:51 AM

        Dear @Meta Chuh,

        Thank you so much for your help, this is exactly what I wanted.

        Just theoretically, if the number of instances would be much higher and every fruit needs for example 100 lines, so the final text displayed as:

        apple/1
        apple/2
        apple/3
        .
        .
        .
        apple/100
        apricot/1
        apricot/2
        apricot/3
        .
        .
        .
        apricot/100
        .
        .
        .

        is there any shorter formula that can do that so no need to type your expression individually to <...$1/99\r\n$1/100>

        I tried to edit your replace field you provided but I’m not at this level of understanding and had no success.

        Thank you,
        Victoria

        1 Reply Last reply Reply Quote 0
        • G
          guy038
          last edited by Nov 26, 2017, 5:53 PM

          Hello, @viktoria-ontapado, and All,

          Here is my solution, for any number of repetitions !

          Assuming, for instance, your initial block of text, with the 6 lines, as below :

          apple
          apricot
          avocado
          banana
          blackberry
          blackcurrant
          
          • First, paste this block in a new tab, with a line break after the sixth line ( IMPORTANT )

          • Display the Line Numbers column with the menu option Settings > Preferences… > Editing > Display line number

          Now, if you want to get, let’s say, 50 values of each fruit , that means that your total text will contain 6 x 50 = 300 lines

          • So, select the entire block ( Ctrl + A )

          • Hold down the Ctrl + D shortcut, while having a look, in the bottom status bar, at the part lines: xxx

          • And release the keys, as soon as you get the number 301

          • Now, move back at beginning, if necessary ( Ctrl + Home )

          • Open the column editor ( Alt + C )

            • Choose the Number to Insert feature

            • Fill in the number 1 in the Initial number and Increase by boxes

            • Type in the number 6 in the Repeat zone ( your number of fruits )

            • Check the Leading zeros box ( IMPORTANT )

            • Select, if necessary, the dec format

            • Click on the OK button

          • Open the Replace dialog ( Ctrl + H )

            • In the Find what: box, fill in the regex (?-s)(\d+)(.+)

            • In the Replace with: box, fill in the regex \2/\1

            • Check the Wrap around and Regular expression options

            • Click on the Replace All button

          => In each line, the numbers and the fruit’s names are swapped, separated by the / symbol !

          • Finally, perform a classic alphabetic sort, using the option Edit > Line Operations > Sort Lines Lexicographically Ascending

          Et voilà !

          Optionally, you may get rid of the leading zeros, in numbers, with the simple regex S/R :

          SEARCH /0+

          REPLACE /

          Best Regards,

          guy038

          V 1 Reply Last reply Nov 27, 2017, 6:56 AM Reply Quote 0
          • V
            Viktoria Ontapado @guy038
            last edited by Nov 27, 2017, 6:56 AM

            Hello @guy038,

            I was hoping you could check this topic.

            Thank you very much for your creative solution, I really appreciate it.
            I’ve already tested it with my other lists and it’s working like a charm.

            Have a nice week,
            Viktória

            1 Reply Last reply Reply Quote 0
            1 out of 5
            • First post
              1/5
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors