PowerShell implementation retrieves keyword functionality in multiple files _powershell

Source: Internet
Author: User

These days in see PowerShell, feel very strong, a small task, is to use PowerShell to implement multiple files to retrieve thousands of lines of records in the script. Just beginning to use get-content combined with where-object to achieve, found that the results of the search is too slow, and the retrieved entries are not known from which file, although it is easy to achieve, but after the exploration of a variety of methods, I found out that a powerful command could achieve all of these needs, and that it was super fast.

Generally used for IIS log analysis, because the main introduction of this PS command, IIS log is crawled from the server side, here is only half the test data.

First go to the directory and view the files in the current directory, where four log files are used for testing. As follows:

Then use the following command to achieve that powerful feature:

For example, to search for keyword write, then enter the write return, the results of the search is as follows (only the interception section, here due to privacy, the log file is made by me, but the amount of data is relatively large, the content is purely fictitious ~ ~):

You can assign this statement to a temporary variable to see how much data is detected at this instant:

In fact, there are not many, 4 log files found in the Instant 500 data, in fact, this can not be seen, because I made the amount of data is not big enough ...

The following is a simple explanation for this powerful command:

The whole is actually used select-string This command, the specific use of reference:

Http://technet.microsoft.com/zh-cn/library/dd315403.aspx

Get-childitem is to get all the items in the current face and then through the pipe | Input to? (is Where-object alias) to filter, and filter the current directory log files, $_ represents the current object, that is, the results of the previous detection,-like use pattern matching, $ (read-host) is read from the console, similar to C # ReadLine () (the corresponding standard output is write-host), that is, when the user enters a keyword, the command executes from the back forward.

Because it is just getting started, if there is a mistake, the great God to teach a lot.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.