Filtering text using Grep's PowerShell
There is a log file, about 4M in size, asking to find a record that takes more than 100s. First think of the powerful grep, then get up.
First find information on the Internet, this article, there are several ways:
The first type:
Get-content somefile.txt|findstr "Someregexp"
Get-content can be replaced by Cat,powershell already gave them an alias, but really considerate sheller.
This approach is a mix of commandline and PowerShell, because Findstr is a command-line tool and not a PowerShell cmdlet.
The second type:
Cat Somefile.txt | where {$ -match "Some_regexp"}
Purebred PowerShell is implemented using the where filter
The third type:
Select-string "Some_regexp" Somefile.txt
Directly with the implementation of select-string.
After testing, the last PowerShell command written is as follows:
Cat " \d{3,}\.\d{2,}s "} >>result.log
Use where this, this can use regular, findstr command not. Inside the regular match string "\d{3,}.\d{2,}s" is also very simple , "3 numbers. 2 digits above S" means.
Finally: Filter out the results into Result.log
17:05:14,884 [email protected] HTTP-0.0.0.0-8888-PROCESSOR7 DEBUG strategyactionhelper:-Getstrategyinvoicemap Finished ... Consumed time:191.028s
17:05:14,889 [email protected] Http-0.0.0.0-8888-processor4 DEBUG strategyactionhelper:-Getstrategyinvoicemap Finished ... Consumed time:191.04s
17:07:19,112 [email protected] HTTP-0.0.0.0-8888-PROCESSOR7 DEBUG strategyactionhelper:-setliststrategyattributes Finished ... Consumed time:379.082s
17:07:20,106 [email protected] Http-0.0.0.0-8888-processor4 DEBUG strategyactionhelper:-setliststrategyattributes Finished ... Consumed time:381.021s
17:07:37,449 [email protected] Http-0.0.0.0-8888-processor4 DEBUG strategysearchaction:-setliststrategyattributes Finished ... Consumed time:398.364s
17:25:26,773 [email protected] Http-0.0.0.0-8888-processor4 DEBUG CL:-Build table data in Getclientcontractelement Finis Hed ... Consumed time:1064.296s
17:25:27,328 [email protected] Http-0.0.0.0-8888-processor4 DEBUG CL:-Getclientcontractelement finished ... Consumed time:1064.858s
17:25:27,328 [email protected] Http-0.0.0.0-8888-processor4 DEBUG CL:-Buildgtreport finished ... Consumed time:1064.87s free memory:176198
Note: The code in PowerShell is GB K, not UTF8, if you want to filter Chinese characters, the simple way is to first convert the UTF8 encoded file to ANSI encoding.
Filtering text using Grep's PowerShell