I want to display only lines beginning by something of my text file, is it possible ?
-
I want to display only lines beginning by 'fam ’ or other words of my text file, is it possible ?
-
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:
- Open the file with the words you’re searching for (hereafter
WORD_FILE) - Open the PythonScript console
- Paste in the command
words = [x + ' ' for x in editor.getText().splitlines()]then hit Enter. - Open the file that you want to search in (hereafter
TARGET_FILE) - Paste in the command
lines = [line for line in editor.getText().splitlines() if any(line.startswith(word) for word in words)]then hit Enter - Open a new file.
- Paste in the command
editor.setText('\r\n'.join(lines))and hit Enter - Your new file will contain only the lines in
TARGET_FILEthat started with one of the words inWORD_FILEfollowed 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:
- Use
Search->Find...from the Notepad++ main menu (Ctrl+F with default keybindings) to open the Find form. - Set
Search ModetoRegular expression. - 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 withFAMfor example) - The
^means that we only search at the start of a line - The
(?:fam|blah)matchesfamorblah - The
\x20is 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)
- This regular expression has these basic parts:
- Hit the
Find All in Current Documentbutton. A form will pop up at the bottom of the Notepad++ window showing all the lines that matched.
- Open the file with the words you’re searching for (hereafter