Awk: Help with how to remove 4rth octet :

Experts,
In one example I have seen how to get output upto 3rd octet, when there is a ":" separated with the 4rth octet.

However in this example how to remove 4rth octet and to keep upto 3rd octet with regular expressions and awk sub function:

I have tried with :but not working:

# awk '{ sub(/\.[0-9]+[ ]/,x,$3); print $2,$3}' file
HOST= cmiHOST06    10.26.107.73 /data120 /nbu/cmiHOST06/athpx07/aa1
HOST= cmiHOST05    10.26.12.76 /data120 /nbu/cmiHOST05/athpx07/cc1
HOST= cmiHOST05    10.26.1.75 /data120 /nbu/cmiHOST05/athpx07/dd1

output should be look like:

cmiHOST06 10.26.107
cmiHOST05 10.26.12
cmiHOST05 10.26.1

Thanks a lot,

Using sed:

$ sed 's/^[^=]*= \([^ ]*\)[ ]*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)\..*$/\1 \2/' foo.txt 
cmiHOST06 10.26.107
cmiHOST05 10.26.12
cmiHOST05 10.26.1

Cheers,
ZB

1 Like

Your awk program were working fine if you searched for the end of $3, represented by "$", instead of looking for whitespace which, being the field separator, was stripped off:

$ awk '{ sub(/\.[0-9]+$/,x,$3); print $2,$3}' file
cmiHOST06 10.26.107
cmiHOST05 10.26.12
cmiHOST05 10.26.1
1 Like

zazzybob,

  • Thanks..
sed 's/^[^=]*= \([^ ]*\)[ ]*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)\..*$/\1 \2/' foo.txt 

The code Looks very complicated, But it worked perfectly , can you please explain a bit.

---------- Post updated at 05:40 PM ---------- Previous update was at 05:30 PM ----------

RudiC,
Thanks it worked very well, thanks for the $ at the end.