简体   繁体   English

Powershell选择字符串列

[英]powershell select-string columns

I have a bunch of IIS logs and powershell 2.0. 我有一堆IIS日志和powershell 2.0。

Currently i'm using the following command to find some info out about them (where 'crossdomain' occurs in them): 目前,我正在使用以下命令来查找有关它们的一些信息(其中出现“跨域”):

dir -r -i *.log | select-string "crossdomain" | Select-Object | export-csv test.csv

This then gives me some data like so: 然后,这给了我一些数据,如下所示:

TRUE    1132740 2011-06-09 11:13:49 W3SVC322653822 myserver-WEB1 1.1.1.1 GET /crossdomain.xml - 80 - 1.1.1.1 HTTP/1.1 Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+6.0;+Trident/4.0;+GTB6.5;+SLCC1;+.NET+CLR+2.0.50727;+Media+Center+PC+5.0;+.NET+CLR+3.5.30729;+.NET+CLR+3.0.30618) WT_FPC=id=82.40.25.58-3980883472.30062468:lv=1307614413232:ss=1307614405277;+__utma=151217894.932228880.1307618019.1307618019.1307618019.1;+__utmz=151217894.1307618019.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);+myserverVISITOR=eyJzaVZpc2l0VHJhY2tpbmcxIjpbeyJWSVNJVERBVEUiOiJNYXksIDEzIDIwMTEgMDY6NTc6NTAiLCJTVEFOREFSRElURU1JRCI6NjQ0MTkzLjB9LHsiVklTSVREQVRFIjoiTWF5LCAxMyAyMDExIDE1OjU0OjMyIiwiU1RBTkRBUkRJVEVNSUQiOjYwMzc4OC4wfSx7IlZJU0lUREFURSI6Ik1heSwgMTUgMjAxMSAxMzo0MDoxNCIsIlNUQU5EQVJESVRFTUlEIjo2NDQxOTUuMH0seyJWSVNJVERBVEUiOiJNYXksIDE1IDIwMTEgMTQ6MDE6NDEiLCJTVEFOREFSRElURU1JRCI6NjQ0MTkyLjB9LHsiVklTSVREQVRFIjoiTWF5LCAxNSAyMDExIDE0OjAzOjIyIiwiU1RBTkRBUkRJVEVNSUQiOjY0NDIxMC4wfSx7IlZJU0lUREFURSI6Ik1heSwgMTUgMjAxMSAxNDoyMTozMiIsIlNUQU5EQVJESVRFTUlEIjo2NDQ2MjAuMH0seyJWSVNJVERBVEUiOiJNYXksIDIyIDIwMTEgMDk6MTM6NTIiLCJTVEFOREFSRElURU1JRCI6NjI5NzYyLjB9LHsiVklTSVREQVRFIjoiSnVuZSwgMDcgMjAxMSAxMTo1MjoxMiIsIlNUQU5EQVJESVRFTUlEIjo2NDUxMjMuMH1dLCJ2aXNpdG9ySWQiOiI1QkFGNzg4NjNERDBENjQ3MUU4NkZENTYwQzU4NTFEMCJ9;+myserverGFSurvey=1;+ebNewBandWidth_.myserver.co.uk=251%3A1303383702897;+__utmb=151217894.1.10.1307618019;+__utmc=151217894 - myserver.co.uk 200 0 0 601 1506 0   W3SVC322653822_ex110609.log.log E:\w3\W3SVC322653822_ex110609.log.log   crossdomain     System.Text.RegularExpressions.Match[]

which is fine and dandy, but not dandy enough. 很好,很漂亮,但是不够漂亮。

What I really want to do is get an export of the 7th column from the end where crossdomain occurs in the file. 我真正想做的是从文件中出现跨域的末尾导出第7列。 So this part in here: 所以这部分在这里:

**myserver.co.uk** 200 0 0 601 1506 0

(the myserver.co.uk) (myserver.co.uk)

any tips on this? 关于这个的任何提示?

Cheers 干杯

Similar to Mjolinors' answer, but I'd try to keep the regexp as simple as possible. 与Mjolinors的答案类似,但我会尝试使regexp尽可能简单。 And since you've already selected lines with the word "crossdomain" you don't have to search for just that: 而且,由于您已经选择了带有“跨域”一词的行,因此您不必仅搜索以下内容:

Get-Content test.csv  | Foreach-Object
{
   if ($_ -match '(\w+\.\w+\.\w+ \d+ \d+ \d+ \d+ \d+ \d+)')
   {
       $matches[1]
   }
}

You won't get any 'Unexpected token' error, but you might have to tweak the regexp to get the result you want (I'm presuming you're looking for a three-dot domain and six numbers after it). 您不会收到任何“意外令牌”错误,但是您可能需要调整正则表达式以获得所需的结果(我假设您正在寻找一个三点域名和其后的六个数字)。

And always use '' for strings when you don't need variable extrapolation. 当不需要变量外推时,请始终对字符串使用''。 it's much safer. 更安全。

get-content  test.csv |
 foreach -object {
 $_ -match ".+\s([a-z\.]+)\s[\s\d]+\S+\s\S+\s+crossdomain\s+\S+$" > $nul
 $matches[1]
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM