Looking for advice to do some simple txt (log) filtering

hardware_failure

[H]ard|Gawd
Joined
Mar 21, 2008
Messages
1,617
First off, I cant code for s***. Maybe some general concepts. Running Windows. I fooled around with basic as a kid and took a pascal class in hs but dont remember jack 🤷

What I want to do is:

import txt file
If line contains: "blah1" or "blah2" or "blah3"
then keep line
else delete line
save

Im not necessiartly asking for someone to do work for me, (tho I certainly wouldnt refuse it lol) maybe advice of how to educate my self how to do this. Im looking for simple. I currently have no installations or knowlege of modern coding/scripting platforms.

EDIT: basically got this software at work thats poorly programmed with no filter options on logs and its a PITA to sift thu it. Can make it less painful with excel but thats still a bit of manual work.

Thanks in advance for helping a noob.
 
Last edited:
A quick C# console app would do it.

C#:
static void Main(string[] args)
        {
            //1.0  Split all the lines into an array from the file name you are reading from.
            string[] temp = File.ReadAllLines("test.txt");


            //2.0 write out values.
            using (StreamWriter sw = new StreamWriter("output.txt"))
            {
                //2.1 Go through each item in the array (aka each line)
                foreach (var item in temp)
                {
                    //2.2 if the line matches your conditions
                    if ((item.Contains("blah1") || item.Contains("blah2") || item.Contains("blah3")))
                    {
                        //2.3 Write to the new file.
                        sw.WriteLine(item);
                    }
                }
            }
        }

Test.txt

foo
bar
fooblah1
testtest

blah3
barblah2
blah4
aadfasdfasdf

output.txt

fooblah1
blah3
barblah2
 
that really something a chatgpt would tell you step by step in great details, just copy paste this very message.


Simply using notepad++ regular expression instead of making a program could work here I think, if you do not need to do it a lot:

  • Open the TXT file in Notepad++.
  • Open the Find dialog by pressing Ctrl + F.
  • Go to the "Mark" tab in the Find dialog.
  • In the "Find what" field, enter the regular expression that matches the lines containing your keywords (e.g., "blah1", "blah2", or "blah3"). For example:
  • Check "Bookmark line" in the options below.
    (blah1|blah2|blah3)
  • Click on "Mark All" — this will mark all lines containing "blah1", "blah2", or "blah3".
  • Now, close the Find dialog.
  • Go to Search > Bookmark > Remove Unmarked Lines. This will delete all lines that don’t contain "blah1", "blah2", or "blah3".
  • Save the file by pressing Ctrl + S.
Otherwise a python or powershell script could do it and would not need to be compiled
 
Last edited:
A quick C# console app would do it.

C#:
static void Main(string[] args)
        {
            //1.0  Split all the lines into an array from the file name you are reading from.
            string[] temp = File.ReadAllLines("test.txt");


            //2.0 write out values.
            using (StreamWriter sw = new StreamWriter("output.txt"))
            {
                //2.1 Go through each item in the array (aka each line)
                foreach (var item in temp)
                {
                    //2.2 if the line matches your conditions
                    if ((item.Contains("blah1") || item.Contains("blah2") || item.Contains("blah3")))
                    {
                        //2.3 Write to the new file.
                        sw.WriteLine(item);
                    }
                }
            }
        }

Test.txt



output.txt
I guess the one thing on that that I am not seeing (and yes, go easy on me, I dont code) is that I need to retain the whole lines, not just keywords. Or maybe what you shared already does that. Playing with what you shared. Thanks!
 
Also the problem with a C# console is Id like to make this available for people even more ignorant than me. I have a duct tape excel solution but its still a PITA.
 
Also the problem with a C# console is Id like to make this available for people even more ignorant than me. I have a duct tape excel solution but its still a PITA.
Would they be at ease to type in terminal

converter.exe "nameofthefileToconvert" "resultfilename.txt" ?

you could do an excel plugin or something that accept to draganddrop a file and ask for an output destination
 
Would they be at ease to type in terminal

converter.exe "nameofthefileToconvert" "resultfilename.txt" ?
Indeed that C# example could easily read in args. Assuming the conditions are static. ha!
 
Indeed that C# example could easily read in args. Assuming the conditions are static. ha!
Yeah, first brainstorm for this would be reading a static file but it is always recording, realtime would be super badass but "one step at a time grasshopper"
 
Would they be at ease to type in terminal

converter.exe "nameofthefileToconvert" "resultfilename.txt" ?

you could do an excel plugin or something that accept to draganddrop a file and ask for an output destination
Yes, both would be a step up from my orginal goal. As I have implied I have my IT professions, coding is not one of them lol.
 
grep -E '(blah1|blah2|blah3)' logfile > newfile
Someone else suggested this to me on reddit. Assuming this is a windows environment, what is the best way to use this? (and eventually, looking to a way to automate it)

Thanks.
 
I was hopeing to not full on sideload linux and make it easy to distribute to whoever, but thanks, I will look into it further. Plus would need to function on a windows os as far back as xp.
On windows in the powershell you can do:

Code:
Select-String -Path "myLogFileName" -Pattern "blah1", "blah2", "blah3" | Out-File -FilePath "newfileName"

that seem to work

or:
Code:
Select-String -Path "myLogFileName" -Pattern "blah1", "blah2", "blah3" | ForEach-Object { $_.Line } | Out-File -FilePath "newfileName"

if you do not want to have the line numbers in your output file.
 
On windows in the powershell you can do:

Code:
Select-String -Path "myLogFileName" -Pattern "blah1", "blah2", "blah3" | Out-File -FilePath "newfileName"

that seem to work

or:
Code:
Select-String -Path "myLogFileName" -Pattern "blah1", "blah2", "blah3" | ForEach-Object { $_.Line } | Out-File -FilePath "newfileName"

if you do not want to have the line numbers in your output file.
Until I find a more slick solition...

This actually works!

Thanks :)
 
I was hopeing to not full on sideload linux and make it easy to distribute to whoever, but thanks, I will look into it further. Plus would need to function on a windows os as far back as xp.

If you can install git bash, it comes with grep. It's not really a whole linux, but it's kind of a separate ecosystem in your existing filesystem.
 
that really something a chatgpt would tell you step by step in great details, just copy paste this very message.


Simply using notepad++ regular expression instead of making a program could work here I think, if you do not need to do it a lot:

  • Open the TXT file in Notepad++.
  • Open the Find dialog by pressing Ctrl + F.
  • Go to the "Mark" tab in the Find dialog.
  • In the "Find what" field, enter the regular expression that matches the lines containing your keywords (e.g., "blah1", "blah2", or "blah3"). For example:
  • Check "Bookmark line" in the options below.
    (blah1|blah2|blah3)
  • Click on "Mark All" — this will mark all lines containing "blah1", "blah2", or "blah3".
  • Now, close the Find dialog.
  • Go to Search > Bookmark > Remove Unmarked Lines. This will delete all lines that don’t contain "blah1", "blah2", or "blah3".
  • Save the file by pressing Ctrl + S.
Otherwise a python or powershell script could do it and would not need to be compiled
Holy crap, I knew ChatGPT was amazing but not as amazing as thought it was.

Anyone ever play a super old old txt/telnet based game called MajorMud?

So far my "program" looks like this, compiled and all:

1726201384458.png

The "coloring" is because thats what said messages look like in the game, and carries over to the HTML file.

But Im adding stuff as fast as ChatGPT can help me do it (in python)

Compiling with pyinstaller. I could share the code if anyone cared. Chances are it will be even better with more functionality by the time anyone looks at it

But wow. Yeah ChatGPT does alot of the work for me but I still have to piece it together and make edits for what I want. My goodness Im actually learning python from this, crazy.
 

Attachments

  • source.zip
    2.5 KB · Views: 0
I was hopeing to not full on sideload linux and make it easy to distribute to whoever, but thanks, I will look into it further. Plus would need to function on a windows os as far back as xp.
If you want this to run on basically anything the best option is probably java script. I am not a web dev so I asked co-pilot for a quick solution and it seems to work.
Feel free to scan this..its just 3 text files. The script.js does the work and the html makes a simple webpage and there is a test text file you...you can easily adjust this to whatever you need. If you get stuck, copy paste the code into co-pilot and ask it to do whatever....

These other solutions feel a bit crazy to me, no offense....haha
 

Attachments

  • solution.zip
    1.7 KB · Views: 0
Back
Top