batch-filelinecuttxtfindstr

How to delete SQL error lines from TXT file with batch script


I have automatically generated report.txt from sql server. But sometimes server get time out and add such info to my txt file.

My report.txt:

Here is good text 
Here also good text with some data
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : SSL Provider: An existing connection was forcibly closed by the remote host. 
. 
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Client unable to establish connection.
Corp. "Big data" and etc.
And here is continue good text
Good text....

I am tried to use findstr function:

findstr /v /i /b "Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server" _allcustomers_srT.txt > SrT_fixed.txt

find /V ". " SrT_fixed.txt > Allcustomers_SrT_fixed.txt
IF EXIST SrT_fixed.txt del /Q SrT_fixed.txt

this script can remove line where is text "SQL", but in middle exist line with dot "." I cannot remove only dot ".' because in my generated data exist companies names, where also is using dot. like in example "Corp. "Big data". and then script remove it also. Maybe is command which could remove such three line right away?


Solution

  • I'd omitted the space after the .

    findstr /v /i /b /L /c:"Sqlcmd: Error:" SrT_fixed.txt |findstr /v /x /L /c:". "> Allcustomers_SrT_fixed.txt
    

    Your original would find and exclude any of Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL or Server. The /c: tells findstr to find "whatever is quoted including spaces" - I arbitrarily shortened that to "Sqlcmd: Error:" since each line to be excluded would seem to begin precisely Sqlcmd: Error:. The /b and /i you know. /L forces literal rather than ""regex".

    Output of the first findstr is fed to the second. That one excludes (/v) lines that exactly match (/x) the (/L) literal string.