Wonderful Uses of Windows PowerShell

       I came across PowerShell by chance recently, and it gave me a deep impression, so I hope to share it with you. Let's talk about what happened first, why I came into contact with PowerShell. A week ago, my project went live for a month. After going online, something went wrong. When the project manager and I were solving the problem in the operation room of the production computer room, we encountered such a problem. Our system fetched data from other systems through the data warehouse and used some tables. Every day, when the data of each table is imported, it will be generated The corresponding log file records the number of imports. When I want to check the number of corresponding table imports in all log files, the approach I take is to open files one by one. At this time, the project manager mentioned that if there is a Linux environment, we can put this batch of files on the server and quickly filter the content we want through the grep command. I said yes, but we can't do anything without this environment now. At this point the project manager told me that maybe we could try to use the PowerShell feature of the Windows system to help us. When I first heard the word, I could roughly guess that it was something about the command line. The project manager said that it can implement the function of the Linux grep command under Windows, and then he helped me solve the problem after Baidu the specific usage. It has to be said that the project manager is a command line expert.

       Let's talk about our scene. At that time, I had dozens of log files in hand, and each log file had the same keyword - "Rows are loaded." It was used to display the amount of data that was imported for each table. I want to count the amount of imported data for all tables. At first, the method I used was to open one file one by one to find out the amount of imported data and record it in another file. Obviously, doing this is time-consuming, inefficient, and error-prone. Now let's talk about how I used PowerShell to achieve this efficiently. Because there is no batch of log files at that time, the ok file (also a text file) is used as an example. Now my desktop has a data folder, which contains sub-files named after many dates, and there are a batch of .data and .ok files under the sub-files. The .dat file is a data file, and the .ok file is a record file corresponding to the data file, which stores data volume information. As shown below:


       The content format of the ok file is as follows: pathname|number of records


       Now I want to extract the number of records in all the ok files in the folder and output it to a file. The following methods can be used.

       There is a PowerShell command line under Windows, which is directly built into the system without installation. We can enter PowerShell through cmd, as shown below:


        Then we can use the cd command to enter the corresponding file directory:


       Next is the crux of the problem. We know that there is a grep command under Linux, but there is also a similar command Select-String under PowerShell. The simplest usage is Select-String "matched content" *. file suffix > custom file Name. Custom file suffix, if we need to extract the number of records in the content of each ok file (of course, there is only one line, and in many scenarios, the content is more than one line), you can use: Select-String ".dat" *. ok > ok.log, ">" is the same as in the Linux system, it means redirection, here is to output all the extracted content to an ok.log file, and we will create the file for us here.

Next we enter the command in PowerShell and press Enter:


       Although there is no prompt, we can go to the file directory to take a look and find that an ok.log file is generated, open the file, and check the contents as follows:

       As you can see, the function I want is easily achieved, and the key content in all files is extracted into one file, instead of opening files one by one and copying them to a unified place.

       We often encounter this kind of problem. The format of a bunch of files is the same, but the content inside is different. When we need to extract the content, we don't need to open all the files in turn to find out some of the content inside. Using PowerShell can easily solve this problem, is it not a huge increase in productivity? Of course, there are many convenient functions under PowerShell, which need to be learned and explored by yourself.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326680637&siteId=291194637