Linux awk team

I am starting to use Linux, I have an input file with content like:

00.11.11.11.11.11
177.22.22.22

one line of Ethernet address and one line of IP address, how can I convert this file to:

IP: 177.22.22.22  MAC: 00.11.11.11.11.11

I think awk will do this, but I don't know how to do it. Any ideas?

Thank!

+3
source share
7 answers

This can also be done with sed, but since you are requesting "awk", "awk", it should be.

awk '/^([0-9]+\.){5}[0-9]+$/ { mac = $1 }
     /^([0-9]+\.){3}[0-9]+$/ { printf "IP: %s MAC: %s\n", $1, mac }' data

(Previous version:

awk '/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ { mac = $1 }
     /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ { printf "IP: %s MAC: %s\n", $1, mac }'

Not so good because of the written repetition, not the counted repetition.)

The first line corresponds to the MAC addresses and stores the last in the mac variable. The second corresponds to IP (IPv4) addresses and prints the current MAC address and IP address.

, .

+3

ideone:

!

{mac=$0;
 getline ip;
 print "IP: " ip  " MAC: " mac}
+3

paste - - < input_file | awk '{print "IP: " $2 " MAC: " $1}'

+3

awk -vRS = '{print "IP:", $2, "MAC:", $1;}'

awk 'BEGIN {RS = "";} {print "IP:", $2, "MAC:", $1;}'

:
"
00.11.22.33.44.55
123.45.67.89

11.22.33.44.55.66
11.22.33.99
"

- >

"
IP: 123.45.67.89 MAC: 00.11.22.33.44.55
IP: 11.22.33.99 MAC: 11.22.33.44.55.66
"

+2
{ if(mac) { print "IP:", $0, "MAC:", mac; mac = 0 } else mac = $0 }
+1

a sed :

sed 's/^/MAC: /;x;N;s/\n/IP: /;G;s/\n/  /' inputfile

:

sed 's/^/MAC: /;x;s/.*//;N;s/\n/IP: /;G;s/\n/  /' inputfile
+1
awk -F"." 'NF>4{m=$0}NF==4{print "IP:"$0" MAC:"m}' file
+1

Source: https://habr.com/ru/post/1772220/


All Articles