【问题标题】:Print out only last 4 digits of mac addresses from 2nd column using awk in linux在linux中使用awk仅打印第二列的最后4位mac地址
【发布时间】:2017-11-10 08:10:35
【问题描述】:

我已经制作了一个shell脚本,用于使用awkarp-scan命令获取mac地址列表。我想将 mac 地址剥离为仅最后 4 位数字,即(我只想打印字母 yy

ac:1e:04:0e:yy:yy   
ax:8d:5c:27:yy:yy   
ax:ee:fb:55:yy:yy   
dx:37:42:c9:yy:yy   
cx:bf:9c:a4:yy:yy

【问题讨论】:

  • 可以通过多种方式完成。你有没有尝试过?
  • 我尝试了很多东西。删除了所有不必要的行和行。但我无法缩短mac地址。我把它存储在 2 美元中
  • 也试试这个方法awk -F":" '{print $NF":"$(NF-1)}' fileName
  • 请用您迄今为止尝试过的内容更新您的问题。
  • @YashBhagwatkar,在代码标签中向我们展示您的预期输出,以便我们尝试帮助您(尽管我提供了解决方案,但由于未给出预期输出,因此仅基于一些假设)。

标签: linux bash shell awk


【解决方案1】:

试试cut -d: -f5-

(选项含义:分隔符:和字段 5 及以上。)

编辑:或者在 awk 中,根据您的要求: awk -F: '{ print $5 ":" $6 }'

【讨论】:

    【解决方案2】:

    这里有几个

    line=cx:bf:9c:a4:yy:yy
    echo ${line:(-5)}
    
    line=cx:bf:9c:a4:yy:yy
    echo $line | cut -d":" -f5-
    

    【讨论】:

      【解决方案3】:

      我想你想去掉尾随空格,但不清楚你是想要yy:yy 还是yyyy

      无论如何,有多种方法,但您已经在运行 AWK 并且在 $2 中拥有 MAC。

      第一种情况是:

      awk '{match($2,/([^:]{2}:[^:]{2}) *$/,m); print m[0]}'
      yy:yy
      

      在第二个(没有冒号:):

      awk 'match($2,/([^:]{2}):([^:]{2}) *$/,m); print m[1]m[2]}'
      yyyy
      

      如果您的AWK 中没有可用的match,则需要求助于gensub

      awk '{print gensub(/.*([^:]{2}:[^:]{2}) *$/,"\\1","g",$2)}'
      yy:yy
      

      或:

      awk '{print gensub(/.*([^:]{2}):([^:]{2}) *$/,"\\1\\2","g",$0)}'
      yyyy
      

      编辑:

      我现在意识到尾随空格是由 anubhava 在他的编辑中添加的;他们没有出现在原始问题中!然后您可以简单地保留最后 n 个字符:

      awk '{print substr($2,13,5)}'
      yy:yy
      

      或:

      awk '{print substr($2,13,2)substr($2,16,2)}'
      yyyy
      

      【讨论】:

        【解决方案4】:

        考虑到mac address 始终是 6 个八位字节,您可能只需执行以下操作来获取最后 2 个八位字节:

        awk '{print substr($0,13)}' input.txt
        

        在使用 arp -an 进行动态测试时,我注意到在某些情况下输出并不总是打印 mac 地址,而是返回如下内容:

        (169.254.113.54) at (incomplete) on en4 [ethernet]
        

        因此可能更好地过滤输入以保证mac地址,这可以通过应用regex来完成:

        ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$
        

        在 awk 中应用正则表达式并仅打印最后 2 个八进制数:

        arp -an | awk '{if ($4 ~ /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/) print substr($4,13)}'
        

        这将过滤列 $4 并验证这是一个有效的 MAC 地址,然后它使用 substr 只返回最后一个“字母”

        您也可以按: 拆分并以多种方式打印输出,例如:

        awk '{if ($4 ~ /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/) split($4,a,":");  print a[5] ":" a[6]}
        

        注意exp ~ /regexp/

        This is true if the expression exp (taken as a string) is matched by regexp. 
        The following example matches, or selects, all input records with the upper-case letter `J' somewhere in the first field:
        $ awk '$1 ~ /J/' inventory-shipped
        -| Jan  13  25  15 115
        -| Jun  31  42  75 492
        -| Jul  24  34  67 436
        -| Jan  21  36  64 620
        So does this:
        awk '{ if ($1 ~ /J/) print }' inventory-shipped
        

        【讨论】:

          猜你喜欢
          • 2014-03-24
          • 2015-11-04
          • 2011-02-27
          • 1970-01-01
          • 1970-01-01
          • 2011-01-06
          • 1970-01-01
          • 1970-01-01
          • 2011-05-17
          相关资源
          最近更新 更多