Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows下的“unixshell”类脚本_Windows_Unix_Shell_Scripting - Fatal编程技术网

Windows下的“unixshell”类脚本

Windows下的“unixshell”类脚本,windows,unix,shell,scripting,Windows,Unix,Shell,Scripting,我需要shell脚本大师的帮助 我有一个.txt文件日志,它在几行上跟踪客户机的IP地址,格式与此类似: Line1 - Client IP [192.168.0.1] Other data Line2 - Client IP [192.168.0.2] Other data Line3 - Client IP [192.168.0.3] Other data Line4 - Client IP [192.168.0.2] Other data Line5 - Client IP [192.16

我需要shell脚本大师的帮助

我有一个.txt文件日志,它在几行上跟踪客户机的IP地址,格式与此类似:

Line1 - Client IP [192.168.0.1] Other data
Line2 - Client IP [192.168.0.2] Other data
Line3 - Client IP [192.168.0.3] Other data
Line4 - Client IP [192.168.0.2] Other data
Line5 - Client IP [192.168.0.1] Other data
...
我需要创建以下脚本:

从该文件中提取IP地址 将IP地址分组相同的IP地址只报告一次 输出带有结果IP地址的文件 对于上一个示例,生成的文件将是:

192.168.0.1
192.168.0.2
192.168.0.3
我使用的是Windows操作系统,但我可以使用或之类的工具,在Windows下提供类似Unix的命令,如grep、sort等

一个没有脚本的解决方案也可能很好

提前感谢你的帮助

 cat yourfile.txt | sed 's/*\[//g' | sed 's/\]*//g' | sort | uniq > newfile.txt
支架可能不需要逃逸。我不记得了。这些工具都应该在Cygwin上可用


支架可能不需要逃逸。我不记得了。这些工具都应该在Cygwin上可用。

下面是一个简短的sed脚本,它提取方括号中的部分,然后排序-u删除重复项:

sed -e 's/^.*\[\(.*\)\].*$/\1/g' < inputfile | sort -u

下面是一个简短的sed脚本,它提取方括号之间的部分,然后sort-u删除重复项:

sed -e 's/^.*\[\(.*\)\].*$/\1/g' < inputfile | sort -u
在PowerShell中:

冗长的方式-

$regex = '(?<IPAddress>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
get-content log.txt | where-object {if ($_ -match $regex){$matches.ipaddress}} | group-object -noelement
在PowerShell中:

冗长的方式-

$regex = '(?<IPAddress>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
get-content log.txt | where-object {if ($_ -match $regex){$matches.ipaddress}} | group-object -noelement

要在简洁性方面击败那些sed脚本是相当困难的。好吧,可读性是个问题

您可以在VBScript中使用Scripting.FileSystemObject进行文件访问,使用VBScript的正则表达式和Dictionary对象,创建更详细、更可读的版本,如下所示

Option Explicit

Dim oFSO
Dim oRgx
Dim oMatch
Dim oMatches
Dim oStream
Dim sLine
Dim oDict
Dim sIP
Dim aKeys
Dim sKey

Set oFSO     = CreateObject( "Scripting.FileSystemObject" )
Set oDict    = CreateObject( "Scripting.Dictionary" )

Set oStream  = oFSO.OpenTextFile( "log.txt", 1, False )

Set oRgx     = new regexp
oRgx.Pattern = "\[(.+?)\]"
oRgx.Global  = True

Do While Not oStream.AtEndOfStream
  sLine        = oStream.ReadLine
  Set oMatches = oRgx.Execute(sLine)

  For Each omatch in omatches
    sIP         = oMatch.SubMatches(0)

    If Not oDict.Exists( sIP ) Then
      oDict.Add sIp,1
    End If

  Next

Loop

aKeys = oDict.Keys

For Each sKey in aKeys
  wscript.echo sKey
Next

要在简洁性方面击败那些sed脚本是相当困难的。好吧,可读性是个问题

您可以在VBScript中使用Scripting.FileSystemObject进行文件访问,使用VBScript的正则表达式和Dictionary对象,创建更详细、更可读的版本,如下所示

Option Explicit

Dim oFSO
Dim oRgx
Dim oMatch
Dim oMatches
Dim oStream
Dim sLine
Dim oDict
Dim sIP
Dim aKeys
Dim sKey

Set oFSO     = CreateObject( "Scripting.FileSystemObject" )
Set oDict    = CreateObject( "Scripting.Dictionary" )

Set oStream  = oFSO.OpenTextFile( "log.txt", 1, False )

Set oRgx     = new regexp
oRgx.Pattern = "\[(.+?)\]"
oRgx.Global  = True

Do While Not oStream.AtEndOfStream
  sLine        = oStream.ReadLine
  Set oMatches = oRgx.Execute(sLine)

  For Each omatch in omatches
    sIP         = oMatch.SubMatches(0)

    If Not oDict.Exists( sIP ) Then
      oDict.Add sIp,1
    End If

  Next

Loop

aKeys = oDict.Keys

For Each sKey in aKeys
  wscript.echo sKey
Next

如果您可以使用Cygwin,则无需担心Windows脚本解决方案。

如果您可以使用Cygwin,则无需担心Windows脚本解决方案。

如果您必须在Windows平台上编写大量脚本,则应查看PowerShell。它很容易学习,在Windows平台上几乎没有什么做不到的。如果您必须在Windows平台上编写大量脚本,您应该查看PowerShell。它很容易学习,在Windows平台上使用它几乎没有什么做不到的。