4

I have various logfiles which I want to filter with a grep-like functionality.

The way I did it before is:

  • Mark the word / string
  • Search/Mark
    • enable option "Bookmark line"
    • mark all
    • close
  • Search/Bookmark
    • "Remove Unmarked lines"

For this procedure I recorded a macro and stored it in shortcuts.xml:

<Macros>
        <Macro name="Trim Trailing and save" Ctrl="no" Alt="yes" Shift="yes" Key="83">
            <Action type="2" message="0" wParam="42024" lParam="0" sParam="" />
            <Action type="2" message="0" wParam="41006" lParam="0" sParam="" />
        </Macro>
        <Macro name="grep01" Ctrl="no" Alt="no" Shift="no" Key="0">
            <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
            <Action type="3" message="1601" wParam="0" lParam="0" sParam="IM1 001" />
            <Action type="3" message="1625" wParam="0" lParam="0" sParam="" />
            <Action type="3" message="1702" wParam="0" lParam="784" sParam="" />
            <Action type="3" message="1701" wParam="0" lParam="1615" sParam="" />
            <Action type="2" message="0" wParam="43051" lParam="0" sParam="" />
        </Macro>

Is it possible to replace the string "IM1 001" with a wildcard, or the actual marked text and then just press a combination to do the job?

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • I don't think Notepad++'s macros support variables. There might be a plugin that could help (maybe NPPExec), but if you need to do this task regularly then it may be worth putting together a PowerShell script for it instead. – trapper_hag Feb 19 '19 at 15:10
  • Thank you, i will ask this question at the Notepad++ community, maybe they can help – Sammy Bresel Feb 20 '19 at 08:25
  • Powershell script is not what i need. I often have outputs of logfiles which are copy-pasted into notepad++. Then i want to filter lines by specific words or terms. – Sammy Bresel Feb 20 '19 at 08:36

4 Answers4

0

It is not possible:

Searches recorded as macros always have all of their parameters hard-wired.

see here on notepad++ community

0

EmEditor has a bookmark functionality that can do that. In the Find dialog, you enter your string, then instead of Find click Bookmark. Then Edit > Bookmarks > This document and you can either delete or extract to new file or even Invert bookmarked lines and then proceed to delete or extract, etc.

greektranslator
  • 499
  • 1
  • 6
  • 19
0

Was curious to know how do-able something like this might be with PowerShell (which I know next to nothing about) and came up with the following. This overwrites the original file and I've only given it a very quick test, so obviously don't use it with anything that isn't backed up :)

The PowerShell script (removelines.ps1):

# https://stackoverflow.com/a/30534991/957246
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = 'Search'
$msg   = "The string to search for (lines that do not contain this string will be removed)."
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
$c = Get-Content $args[0] | where { $_ -match $text }
$c | Set-Content $args[0]

Command to save in the 'Run' box in Notepad++:

powershell.exe -ExecutionPolicy Unrestricted -NoLogo -File "<Path to script goes here>\removelines.ps1" "$(FULL_CURRENT_PATH)"

When you run it (with the file you want to modify open/on the current tab), you'll be prompted for the search string. After running it, Notepad++ will prompt to reload the file - which should now be missing any lines that don't contain the search string.

trapper_hag
  • 780
  • 9
  • 14
0

I'm not sure if you ever got a satisfactory answer to this question but from the sounds of it you could do this entirely within notepad++ with a regex find and replace.

For example given the source:

Line 1: lorem ipsum
Line 2: test test test
Line 3: test IM1 001 test test
Line 4: IM1 001
Line 5: nothing here
Line 6: aaaaaaaaaaaaaaaaaIM1 001aaaaaaaaaa
Line 7: active
Line 8: inactive
Line 9: value
Line 10: blah blah
Line 11: …..
Line 12: …..
Line 13: this contains the IM1 001

Replace: ^(?:(?!IM1 001).)*?$\R* (where IM1 001 is the text/expression you're looking for)
With: nothing

And all that will be left is the lines with the search phrase you specify:

Line 3: test IM1 001 test test
Line 4: IM1 001
Line 6: aaaaaaaaaaaaaaaaaIM1 001aaaaaaaaaa
Line 13: this contains the IM1 001

If you want to preserve the empty lines you can simply remove the \R* from the end of the expression:



Line 3: test IM1 001 test test
Line 4: IM1 001

Line 6: aaaaaaaaaaaaaaaaaIM1 001aaaaaaaaaa






Line 13: this contains the IM1 001


JonM
  • 1,314
  • 10
  • 14
  • Okay , this is weird. Hey JonM, I bookmarked a question of yours in puzzle exchange while waiting the correct answer but there is none so I'm very curios of the answer. It's the "what's the word..." question. I tried to find you in chat but no luck... Please give me the answer through spoiler tag or similar, so that I can redeem myself. This comment is super spammish, so I will delete it in a couple of hours. Sorry for the inconvenience – atakanyenel Mar 21 '19 at 22:32