how to get rid of all chars after the last decimal points

Hi All,

Here is my original string: 192.168.2.1.8088. The target string I want: 192.168.2.1, how can I use awk or sed or other command to get rid of .8088 in the string?

Thanks,
Ray

string="192.168.2.1.8088"
string=${string%.*}
echo '192.168.2.1.8088' |sed 's/.[^.]*$//'
192.168.2.1
1 Like

yet another way...

echo "192.168.2.1.8088" | awk -F. '{for(i=1;i<NF;i++) printf("%s%s",$i,i==NF-1?"\n":".")}'
# uname -a
SunOS 5.10 Generic_144488-06 sun4u sparc SUNW,Sun-Fire-V240
@:/opt/tools>
# echo "192.168.2.1.8088" | awk -F"." '{for(i=1;i<NF;i++) printf("%s%s",$i,i==NF-1?"\n":".")}'
awk: syntax error near line 1
awk: illegal statement near line 1

for solaris please use nawk... :slight_smile:

$ echo "192.168.2.1.8088" | nawk -F. '{for(i=1;i<NF;i++) printf("%s%s",$i,i==NF-1?"\n":".")}'
192.168.2.1
echo '1.2.3.4.5' | nawk -F. 'sub(FS "[^"FS"]*$", "")'
cut -d. -f1-4

Why should you use a external command if this is suffice?

string="192.168.2.1.8088"
string=${string%.*}
 
perl -pe '/(.*)\.\d+/;$_=$1'

or

 
perl -pe 's/\.\d+$//'