Community
    • Login

    I want to display only lines beginning by something of my text file, is it possible ?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 56 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.
    • PATRICK MULOTP
      PATRICK MULOT
      last edited by

      I want to display only lines beginning by 'fam ’ or other words of my text file, is it possible ?

      Mark OlsonM 1 Reply Last reply Reply Quote 0
      • Mark OlsonM
        Mark Olson @PATRICK MULOT
        last edited by Mark Olson

        @PATRICK-MULOT

        This is probably a job for regular expressions, but it depends to some extent on the specifics of your task.

        If you have a text file where every line is a word that you need to search for, you need to do a scripting solution. This kind of question has been asked many times before on the forum and I’m too lazy to look up which posts it was answered in, but a PythonScript script that would accomplish this would probably be as follows:

        1. Open the file with the words you’re searching for (hereafter WORD_FILE)
        2. Open the PythonScript console
        3. Paste in the command words = [x + ' ' for x in editor.getText().splitlines()] then hit Enter.
        4. Open the file that you want to search in (hereafter TARGET_FILE)
        5. Paste in the command lines = [line for line in editor.getText().splitlines() if any(line.startswith(word) for word in words)] then hit Enter
        6. Open a new file.
        7. Paste in the command editor.setText('\r\n'.join(lines)) and hit Enter
        8. Your new file will contain only the lines in TARGET_FILE that started with one of the words in WORD_FILE followed by a literal space character

        If you want to find lines that start with "fam " or "blah " (as in “fam” or “blah” followed by a literal space character), you can do that by:

        1. Use Search->Find... from the Notepad++ main menu (Ctrl+F with default keybindings) to open the Find form.
        2. Set Search Mode to Regular expression.
        3. Set Find what: to (?-i)^(?:fam|blah)\x20.*
          • This regular expression has these basic parts: (?-i), ^, (?:fam|blah), \x20, and .*
          • The (?-i) says that this is a case-sensitive search (so it would exclude lines starting with FAM for example)
          • The ^ means that we only search at the start of a line
          • The (?:fam|blah) matches fam or blah
          • The \x20 is a different representation of a normal space character, which I sometimes use in regular expressions to make it more readable
          • The .* matches the rest of the line (. is any non-newline character and * means match 0 or more of the preceding pattern)
        4. Hit the Find All in Current Document button. A form will pop up at the bottom of the Notepad++ window showing all the lines that matched.
        1 Reply Last reply Reply Quote 4
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors