Search code examples
unixcommandcut

Using the cut command in UNIX to get the last period


Lets say I have lots of ip numbers (2 ip numbers per line separated by a space) to look through (here are two lines):

67.21.89.48.1623 139.91.131.115.110
211.47.82.64 139.91.134.123.445

One of them might not have a port so the number of periods are never consistent. I only want the ip number of the first one (without the port) and only the port of the second set (without the ip number). So it should look something like:

67.21.89.48 110
211.47.82.64 445

Or it could look like this:

67.21.89.48.110
211.47.82.64.445

It doesn't really matter as long as I know where the IP and port are located.

I've been using something like this:

cut -d'.' -f1-4,9 < file.txt

But that only works with a consistent amount of periods. Any way to cut from the back instead?


Solution

  • As Jonathan points out in the comment, using cut would be very complicated because the numbers of the columns you need may differ.

    Here's an example in sed:

    $ echo "67.21.89.48.1623 139.91.131.115.110
    211.47.82.64 139.91.134.123.445" | sed -r 's/^(([0-9]{1,3}\.){3}[0-9]{1,3})(.*)\.([0-9]{1,4})$/\1 \4/'
    67.21.89.48 110
    211.47.82.64 445
    

    You can run it as:

    sed -r 's/^(([0-9]{1,3}\.){3}[0-9]{1,3})(.*)\.([0-9]{1,4})$/\1 \4/' logfile.txt
    

    [0-9]{1,3}\.){3}[0-9]{1,3} is probably a lame regex for an IP address, but it was the first that I could think of. You can replace it with something smarter. Maybe you don't even need to check what's between the dots, just take everything before the 4th period and after the last one.