Search code examples
linuxbashsedavahi

Remove IPv6 addresses from a list using sed


Using avahi I have populated a list of ip-addresses on my network range. The information populated is then refined using sed to give the following output

Initial data = address = [xxx.xxx.xxx.xxx]
Refined data = xxx.xxx.xxx.xxx

The command to do so is as follows:

avahi-browse -alrt | grep -w address | sort -u | sed -e 's/address = //' | sed -e 's/\[//' | sed -e 's/\]//'

This works correctly most of the time however on the odd occasion addresses such as xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx are displayed in the list and I would like to omit them.

I know I could possibly use a regex expression or something to ensure the data always matches a valid ip i.e xxx.xxx.xxx.xxx but I am unsure as to how to go about this. Any help is much appreciated.

The command is run on linux using a bash script and I wish it to return only the valid ip addresses in the xxx.xxx.xxx.xxx format.


Solution

  • Replace your 3 sed commands with this one:

    sed 's/address = \[\|\]//g' 
    

    OR:

    sed -r 's/address = \[|\]//g' 
    

    EDIT: To remove invalid IPs also use this sed:

    sed -r -e 's/address = \[|\]//g' -e 's/= +([^:]+\:){5,}.*$/=/' file