使用linux中的awk从第二列打印出最后4位的mac地址

时间:2022-09-25 15:39:13

I have made a shell script for getting the list of mac address using awk and arp-scan command. I want to strip the mac address to only last 4 digits i.e (i want to print only the letters yy)

我使用awk和arp-scan命令创建了一个shell脚本来获取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

5 个解决方案

#1


2  

Try cut -d: -f5-

尝试切-d:-f5-

(Options meaning: delimiter : and fields 5 and up.)

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

EDIT: Or in awk, as you requested: awk -F: '{ print $5 ":" $6 }'

编辑:或者在awk中,按照您的要求:awk -F:'{print $ 5“:”$ 6}“

#2


0  

Your expected output is NOT clear but based on your example output shown.

您的预期输出不明确,但基于您显示的示例输出。

your_command | awk -F":" '{print $NF}' 

I am setting : as field separator so you could get any field which you want to print, so to see field numbers following may help you in same.

我正在设置:作为字段分隔符,因此您可以获得要打印的任何字段,因此查看以下字段编号可能会对您有所帮助。

your_command | awk -F":" '{for(i=1;i<=NF;i++){print "field number",i,"field value",$i}}'

#3


0  

here are a few

这里有几个

line=cx:bf:9c:a4:yy:yy
echo ${line:(-5)}

line=cx:bf:9c:a4:yy:yy
echo $line | cut -d":" -f5-

#4


0  

I imagine you want to strip the trailing spaces, but it isn't clear whether you want yy:yy or yyyy.

我想你想要去除尾随空格,但是你不清楚你是否想要yy:yy或yyyy。

Anyhow, there are multiple ways to it but you already are running AWK and have the MAC in $2.

无论如何,有多种方法,但你已经运行AWK并且MAC为2美元。

In the first case it would be:

在第一种情况下,它将是:

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

In the second (no colon :):

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

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

In case you don't have match available in your AWK, you'd need to resort to gensub.

如果您的AWK中没有可用的匹配项,则需要使用gensub。

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

or:

要么:

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

Edit:

编辑:

I now realized the trailing spaces were added by anubhava in his edit; they were not present in the original question! You can then simply keep the last n characters:

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

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

or:

要么:

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

#5


0  

Taking into account that the mac address always is 6 octets, you probably could just do something like this to get the last 2 octets:

考虑到mac地址总是6个八位字节,你可能只需要做这样的事情来得到最后2个八位字节:

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

While testing on the fly by using arp -an I notice that the output was not always printing the mac addresses in some cases it was returning something like:

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

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

Therefore probably is better to filter the input to guarantee a mac address, this can be done by applying this regex:

因此,最好过滤输入以保证mac地址,这可以通过应用此正则表达式来完成:

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

Applying the regex in awk and only printing the 2 last octecs:

在awk中应用正则表达式并仅打印最后两个octec:

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

This will filter the column $4 and verify that is a valid MAC address, then it uses substr to just return the last "letters"

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

You could also split by : and print the output in multiple ways, for example:

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

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

Notice the exp ~ /regexp/

注意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

#1


2  

Try cut -d: -f5-

尝试切-d:-f5-

(Options meaning: delimiter : and fields 5 and up.)

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

EDIT: Or in awk, as you requested: awk -F: '{ print $5 ":" $6 }'

编辑:或者在awk中,按照您的要求:awk -F:'{print $ 5“:”$ 6}“

#2


0  

Your expected output is NOT clear but based on your example output shown.

您的预期输出不明确,但基于您显示的示例输出。

your_command | awk -F":" '{print $NF}' 

I am setting : as field separator so you could get any field which you want to print, so to see field numbers following may help you in same.

我正在设置:作为字段分隔符,因此您可以获得要打印的任何字段,因此查看以下字段编号可能会对您有所帮助。

your_command | awk -F":" '{for(i=1;i<=NF;i++){print "field number",i,"field value",$i}}'

#3


0  

here are a few

这里有几个

line=cx:bf:9c:a4:yy:yy
echo ${line:(-5)}

line=cx:bf:9c:a4:yy:yy
echo $line | cut -d":" -f5-

#4


0  

I imagine you want to strip the trailing spaces, but it isn't clear whether you want yy:yy or yyyy.

我想你想要去除尾随空格,但是你不清楚你是否想要yy:yy或yyyy。

Anyhow, there are multiple ways to it but you already are running AWK and have the MAC in $2.

无论如何,有多种方法,但你已经运行AWK并且MAC为2美元。

In the first case it would be:

在第一种情况下,它将是:

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

In the second (no colon :):

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

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

In case you don't have match available in your AWK, you'd need to resort to gensub.

如果您的AWK中没有可用的匹配项,则需要使用gensub。

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

or:

要么:

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

Edit:

编辑:

I now realized the trailing spaces were added by anubhava in his edit; they were not present in the original question! You can then simply keep the last n characters:

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

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

or:

要么:

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

#5


0  

Taking into account that the mac address always is 6 octets, you probably could just do something like this to get the last 2 octets:

考虑到mac地址总是6个八位字节,你可能只需要做这样的事情来得到最后2个八位字节:

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

While testing on the fly by using arp -an I notice that the output was not always printing the mac addresses in some cases it was returning something like:

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

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

Therefore probably is better to filter the input to guarantee a mac address, this can be done by applying this regex:

因此,最好过滤输入以保证mac地址,这可以通过应用此正则表达式来完成:

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

Applying the regex in awk and only printing the 2 last octecs:

在awk中应用正则表达式并仅打印最后两个octec:

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

This will filter the column $4 and verify that is a valid MAC address, then it uses substr to just return the last "letters"

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

You could also split by : and print the output in multiple ways, for example:

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

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

Notice the exp ~ /regexp/

注意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