TCL (Tool Command Language) Question: Download TCL PDF

How to Swap 30 & 40 in IP address 192.30.40.1 using TCL script?

Tweet Share WhatsApp

Answer:

There are three solutions.

set a 192.30.40.1
set b [ string range $a 3 4 ]
set c [ string range $a 6 7 ]
set d [ string replace $a 3 4 $c ]
set e [ string replace $d 6 7 $b]
puts $e

===OR=====
set a 192.30.40.1
set b [ split $a .]
set u [lindex $b 0]
set v [lindex $b 3]
set x [lindex $b 1]
set y [lindex $b 2]
set z [join "$u $y $x $v" .]
puts $z

====OR====
set ip 192.30.40.1
regexp {([0-9]+\.)([0-9]+\.)([0-9]+\.)([0-9]+)} $ip match 1st 2nd 3rd 4th
append new_ip $1st $3rd $2nd $4th
puts $new_ip

Download TCL PDF Read All 11 TCL Questions
Previous QuestionNext Question
How to extract "information" from "ccccccccaaabbbbaaaabbinformationabcaaaaaabbbbbbbccbb" in tcl using a single command?How do you find the length of a string without using string length command in TCL?