Search code examples
cut

gnu cut remove everything from :


I am trying to clean up a set of ipv6 ip addresses.

cat /tmp/ipaddress.txt | /bin/grep -i "unexpectedly shrunk window" | /bin/cut -d' ' -f3 | /bin/cut -d'/' -f1 | /bin/cut -d' ' -f3 | /bin/sort --uniq > /var/log/dos_ip6.txt

/tmp/ipaddress.txt contains the following

TCP: Peer 0000:0000:0000:0000:0000:ffff:4df6:3e12:12345/80 unexpectedly shrunk window 1550831482:1550831483 (repaired)

/var/log/dos_ip6.txt contains the following

0000:0000:0000:0000:0000:ffff:4df6:3e12:12345

i would like to cut the last "*:12345" out.

How can i do this


Solution

  • If your thinking of using two or more cut commands, perhaps consider using awk:

    awk 'BEGIN { IGNORECASE=1 } { print gensub(/(.*):.*/, "\\1", "g", $3) | "sort -u" }' /tmp/ipaddress.txt
    

    Result:

    0000:0000:0000:0000:0000:ffff:4df6:3e12
    

    Otherwise, a simple pipe to sed should suffice:

    ... | sed 's/\(.*\):.*/\1/'