0

For example I have IP addresses list

192.168.20.5
192.168.20.12
192.168.20.141

How to remove all symbols after last dot and get this list (below) at the output

192.168.20.
192.168.20.
192.168.20.

I tried this:

LAN=$(nslookup localhost | awk '{print $2}' | head -1)
hosts=$(echo "${LAN::-1}"{1..254} | xargs -n1 -P0 ping -c 1 | grep "bytes from" | awk '{print $4}')

But ::-1 not suitable, because the number of digits may vary

  • On SO we do encourage users to add their efforts which they have put in order to solve their own problems, please do add your efforts and let us know then. – RavinderSingh13 Dec 30 '19 at 16:12
  • 1
    If you are using Awk anyway, use it to its full potential. Replace `grep "bytes from" | awk '{print $4}'` with `awk '/bytes from/ { a=$4; gsub(/[.][^.]*$/, ".", a); print a}'`; see also [useless use of `grep`.](http://www.iki.fi/era/unix/award.html#grep) – tripleee Dec 30 '19 at 16:18
  • 1
    Similarly, `awk '{print $2}' | head -1` can be replaced with `awk '{ print $2; exit }'` – tripleee Dec 30 '19 at 16:19
  • With **GNU** awk: `awk '{NF--} NF++' FS="." OFS="."` – Cyrus Dec 30 '19 at 17:34

0 Answers0