English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a log file which is constantly storing text in lines. I want to find a way to remove any line that says "[PM] This is a test line" (I will change that with the desired text), once an hour. Like, if I have a file that says:

[S] Some text here
[PM] This is a test line
[S] Some other text here

I want to make a batch file which will remove all lines that say "[PM] This is a test line", leaving just:

[S] Some text here
[S] Some other text here

Any ideas?

2007-09-24 11:33:05 · 3 answers · asked by Random and Anonymous 1 in Computers & Internet Programming & Design

3 answers

Instead of a batch file (that being .bat) you should implement a visual basic script that reads the file, writes it to string array, deletes the text in the file, then rewrites the text file with the strings in the array, following the logic that it ignores the [PM] 's. This is really simple and shouldn't take more than an hour.

2007-09-24 17:18:51 · answer #1 · answered by freakin_smart 1 · 0 5

No problem. Here's one command line using the "for" and "find" functions to do that for you.

Basically, I'm reading each line of a file (in this case, it's test.txt), trying to "find /v" any lines that do not contain the string "[pm]", and when they are found "echo" each of those lines to another file called "output.log"

More details,

"for /f" ... "%i in" - using "for /f" to read in the value of what's coming in the parantheses

"tokens=*" - by default, the "for" command will use space or tab as a delimiter, so it would chop up a line of text and only return the first delimited section. I'm telling it to treat the whole line as one chunk.

('find /v /i "[pm]" test.txt') - each line of output from this command will be piped into what's coming next. The single quotes that bracket the whole command are important. Without them, the "for" command assumes that anything in the parantheses is a filename. The "/v" switch displays all lines NOT containing the specified string. The "/i" switch ignores case. It's only necessary to test for a unique item that will appear on each line you want to ignore which is why I'm just testing for "[pm]".

"do @echo %i >>output.log" - this is the easy part. echo every line - none of which will contain the [pm] line - into a log file.

If you want your log file name to remain the same, then you'll have to do a little extra work to delete the current log file and rename the output file of this command to the log file name. Unfortunately, I don't know of a way to strip out the line in realtime on the actual log file ... that's really just data that's already written to disk.

Final note, if you use this in a batch file instead of from the command line, then the % variable delimiter has to be doubled up ..

for /f "tokens=*" %%i in ('find /v /i "[pm]" test.txt') do @echo %%i >>output.log

2007-09-25 23:52:26 · answer #2 · answered by Kevin 7 · 4 0

Ratchetr is right: echo Hello > test.txt "Hello" is the content of the file "test.txt" is the location of the file use a single > to create your file or two >> to add contents to your file echo World >> test.txt

2016-04-05 23:34:25 · answer #3 · answered by ? 4 · 0 0

fedest.com, questions and answers