69

This probably isn't a VS Code-specific question but it's my tool of choice.

I have a log file with a lot of lines containing the following:

Company.Environment.Security.RightsBased.Policies.RightsUserAuthorizationPolicy

Those are debug-level log records that clutter the file I'm trying to process. I'm looking to remove the lines with that content.

I've looked into Regex but, unlike removing a blank line where you have the whole content in the search criteria (making find/replace easy), here I need to match from line break to line break on some criteria between the two, I think...

What are your thoughts on how criteria like that would work?

3 Answers 3

167

If the criteria is a particular string and you don't want to have to remember regexes, there is a few handy keyboard shortcuts that can help you out. I'm going to assume you're on a Mac.

  1. Cmd-F to open find.
  2. Paste your string.
  3. Opt-Enter to select all of the instances of the string on the page.
  4. Cmd-L to broaden the selection to the entire line of each instance on the page.
  5. Delete/Backspace to remove those lines.
6
  • 4
    On Windows, replace "Cmd" with "Ctrl" and this works perfectly.
    – OLP
    Jun 1, 2020 at 14:20
  • 1
    I also prefer this solution over the regex solution. Matching text with special characters is easier/faster with this solution since you don't have to manually escape each special character.
    – MKANET
    Jul 14, 2020 at 15:56
  • Hi @James, can you please my this question? stackoverflow.com/questions/64365300/… Oct 20, 2020 at 3:59
  • Another variant is to highlight one occurrence in the file, right-click, select Change All Occurances then do Ctrl+L and backspace. I guess it's a combination of this answer and the one from Sanket Patel
    – trademark
    Feb 22, 2021 at 18:10
  • 1
    Thanks!! This worked great on a huge kafka dump file. I use Regex normally but using regex crashed VSCode. btw 3 should be Opt-Enter instead of Alt-Enter
    – Siraj K
    Jan 5 at 4:21
80

I think you should be able to just search for ^.*CONTENT.*$\n, where the content is the text you showed us. That is, search on the following pattern:

^.*Company\.Environment\.Security\.RightsBased\.Policies\.RightsUserAuthorizationPolicy.*$\n

And then just replace with empty string.

3
  • 2
    Don't forget to escape the periods in the middle \.
    – aaldilai
    Jul 31, 2018 at 16:44
  • 1
    @aaldilai Thanks for the correction. The pattern still would have worked, but for all the wrong reasons. Jul 31, 2018 at 16:45
  • 2
    Maybe worth to note: In the Studio Code 'Regular expression' search mode should be enabled to be able to search with the RegExp.
    – szhuravel
    Sep 26, 2019 at 10:05
6

I have already up-voted answer of @james. But.. still I found one more easy and many feature available extension in VS Code. Here it is

It have much easy options to apply filters.

To match specific case mentioned in question. I am attaching screenshot which display how to use for it. I am posting this for others who come here in search for same issue. (Like I came) enter image description here

1
  • useless for larger files, keep showing error when use function "This function requires an active editor"
    – Mr.Devops
    Aug 4, 2021 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.