splunk:从ip地址获取前三个数字

时间:2016-08-04 03:55:37

标签: regex ip substr splunk

我正在尝试获取此格式的前三组IP地址:10.10.10.10

期望值为10.10.10

7 个答案:

答案 0 :(得分:1)

试试这个正则表达式:phpmyadmin

DEMO

从下次开始,请发布您尝试过的内容以及您计划如何实现解决方案。

答案 1 :(得分:0)

正则表达式匹配正确的IP4地址:

<body id="body" onload="window.print();">
    <table>
        <tbody>
            <tr>
                <td class="table-header" colspan="8">
                    <div style="width: 100%">
                        <div class="lleft">
                            @* <img id="imgLogo" src="~/Images/myLogo.png" />*@
                        </div>
                        <div class="baslik">Approval Packing List Form</div>
                        <div style="float: right;">@DateTime.Now.ToString("MM.dd.yyyy")</div>
                    </div>

                </td>
            </tr>
            <tr>
                <td colspan="6"></td>
            </tr>
            <tr>
                <td></td>
            </tr>
            <tr>
                <td></td>
            </tr>
            <tr>
                <td class="line-header">@Html.DisplayNameFor(x => x.ID)</td>
                <td>: @Html.HiddenFor(x => x.ID)@Html.DisplayFor(x => x.ID)</td>

                <td class="line-header" style="width: 165px;">@Html.DisplayNameFor(x => x.Voyage.StartDate)</td>
                <td>: @Html.DisplayFor(x => x.Voyage.StartDate)</td>

                <td class="line-header">@Html.DisplayNameFor(x => x.Voyage.VesselID)</td>
                <td>: @Html.DisplayFor(x => x.Voyage.VesselText)
                </td>
            </tr>
            <tr>
                <td class="line-header">@Html.DisplayNameFor(x => x.Voyage.Id)</td>
                <td>: @Html.DisplayFor(x => x.Voyage.Id)</td>

                <td class="line-header" style="width: 165px;">@Html.DisplayNameFor(x => x.Voyage.EndDate)</td>
                <td>: @Html.DisplayFor(x => x.Voyage.EndDate)</td>
                <td></td>
                <td></td>
            </tr>

            <tr>
                <td colspan="6">
                    <hr />
                </td>
            </tr>

        </tbody>

    </table>
    <table>
        <tr>
            <td class="line-header" style="width: 160px;">Approve Personel</td>
            <td style="border: solid 1px; width: 180px;"></td>
            <td class="line-header">Discharge Port</td>
            <td style="border: solid 1px; width: 180px;"></td>
        </tr>
        <tr>
            <td class="line-header">Approve Date</td>
            <td style="border: solid 1px;"></td>
            <td class="line-header">Terminal</td>
            <td style="border: solid 1px;"></td>
        </tr>
        <tr>
            <td class="line-header">Signiture</td>
            <td style="border: solid 1px;"></td>
            <td></td>
            <td></td>
        </tr>
    </table>

    @if (Request.QueryString["type"] == "HRC" && Model.HrcListPrint != null)
    {
        <table>
            <tr>
                <td colspan="10" style="height: 20px;">
                    <hr />
                </td>
            </tr>
            <tr>
                <td style="text-align: center; width: 210mm; font-weight: bold;" colspan="11">HRC LIST
                </td>
            </tr>

        </table>
        <table class="display dataTable no-footer">
            <thead>
                <tr>
                    <th>Customer Name</th>
                    <th>Customer PO No</th>
                    <th>Ord. ITem No</th>
                    <th>CM No</th>
                    <th>Product</th>
                    <th>Size (T x W inch)</th>
                    <th>Thickness Tolerance</th>
                    <th>Qty (tons)</th>
                    <th>Coil Weight (Lbs)</th>
                    <th>Destination Port</th>
                    <th>Barcode</th>
                    <th>Heat No</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                @foreach (MedTrade.Apollo.Shared.Models.Shipment.PackingListDetailModel item in Model.HrcListPrint)
                {
                    <tr>
                        <td>@item.CustomerName</td>
                        <td>@item.CustomerPurchaseOrderNumber</td>
                        <td>@String.Format("'{0}'", item.OrderItemText)</td>
                        <td>@item.CMNO</td>
                        <td>@item.ProductStandartName</td>
                        <td>@item.ProductProperty</td>
                        <td>@item.ThicknessToleranceType</td>
                        <td>@((item.Quantity / 1000).ToString("N3"))</td>
                        <td>@item.CoilWeight.ToString("N0")</td>
                        <td>@item.DischargePortTanim</td>
                        <td>@item.BarcodeNo</td>
                        <td>@item.HeatNo</td>
                        <td>@item.StatusText</td>
                    </tr>    
                }

            </tbody>
        </table>
    }

    @if (Request.QueryString["type"] == "Rebar" && Model.RebarListPrint != null)
    {
        <table>
            <tr>
                <td colspan="10" style="height: 20px;">
                    <hr />
                </td>
            </tr>
            <tr>
                <td style="text-align: center; width: 210mm; font-weight: bold;" colspan="10">REBAR LIST
                </td>
            </tr>

        </table>

        <table class="display dataTable no-footer">
            <thead>
                <tr>
                    <th>Customer Name</th>
                    <th>Customer PO No</th>
                    <th>Ord. ITem No</th>
                    <th>CM No</th>
                    <th>Product</th>
                    <th>Size (D x L inch)</th>
                    @if (Model.SearchCriteria.ViewType == ViewType.Group)
                    {
                        <th>Qty (tons) / # of bundles</th>
                    }
                    else
                    {
                        <th>Quantity (Tons)</th>
                    }
                    <th>Bundle Weight (Lbs)</th>
                    <th>Destination Port</th>
                    @if (Model.SearchCriteria.ViewType == ViewType.Detail)
                    {
                        <th>Barcode</th>
                    }
                    <th>Heat No</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                @foreach (MedTrade.Apollo.Shared.Models.Shipment.PackingListDetailModel item in Model.RebarListPrint)
                {
                    <tr>
                        <td>@item.CustomerName</td>
                        <td>@item.CustomerPurchaseOrderNumber</td>
                        <td>@String.Format("'{0}'", item.OrderItemText)</td>
                        <td>@item.CMNO</td>
                        <td>@item.ProductStandartName</td>
                        <td>@item.ProductProperty</td>
                        @if (Model.SearchCriteria.ViewType == ViewType.Group)
                        {
                            <td>@((item.Quantity / 1000).ToString("N3")) / @item.Count</td>
                        }
                        else
                        {
                            <td>@((item.Quantity / 1000).ToString("N3"))</td>
                        }
                        <td>@item.BundleWeight.ToString("N0")</td>
                        <td>@item.DischargePortTanim</td>
                        @if (Model.SearchCriteria.ViewType == ViewType.Detail)
                        {
                            <td>@item.BarcodeNo</td>
                        }
                        <td>@item.HeatNo</td>
                        <td>@item.StatusText</td>
                    </tr>    
                }

            </tbody>
        </table>        
    }

</body>

Regex101

正则表达式匹配正确的IP4Address的前三个块:

/^(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])$/

Regex101

或者当它与第三个块之后的点匹配时仍然可以:

/^(([01]?\d?\d|2[0-4]\d|25[0-5])\.){2}([01]?\d?\d|2[0-4]\d|25[0-5])$/

Regex101

答案 2 :(得分:0)

能够以这种方式得到它:

rex field=IP "(?<first_three>\d+\.\d+\.\d+)\.\d+"

答案 3 :(得分:0)

另一种方法。

..| rex field=ip_addr "(?<split_ip>.+)\.[0-9]+"

在哪里

ip_addr-字段名称

split_ip-用于存储拆分IP地址的变量

示例:

Splunk查询:

| stats count | eval ip = "115.124.35.123" | rex field=ip "(?<split_ip>.+)\.[0-9]+" | table split_ip

输出: 115.124.35

答案 4 :(得分:0)

以下为我工作。     rex field=_raw "(?<ip_address>^\d+\.\d+\.\d+\.\d+)"|timechart count by ip_address

答案 5 :(得分:0)

在正则表达式下使用:

^(?P<result>.+(?=\.\d+))

[链接] https://regex101.com/r/bO4tY5/3

答案 6 :(得分:0)

https://regex101.com/是用于此类内容的超级有用的工具。它使您可以编写正则表达式,并针对不同的字符串进行实时测试。

一旦您有需要,请使用rex命令将其粘贴到Splunk搜索查询中。

要回答您的确切问题:

  1. 正则表达式代码,其中 MY_FIELD_NAME_HERE 是提取字段的名称:

(?<MY_FIELD_NAME_HERE>\d+\.\d+\.\d+)\.\d+

  1. 带有regex101示例的正则表达式:

https://regex101.com/r/qTTf4e/2

  1. Splunk查询语言所需的命令,其中 ORIGNAL_FIELD 是您的原始字段,保留10.10.10.10,而 MY_FIELD_NAME_HERE 是提取的字段:

... | rex field="ORIGNAL_FIELD" "(?<MY_FIELD_NAME_HERE>\d+\.\d+\.\d+)\.\d+"