1

Can you help me

Here is the situation

I have a string similar to this one

profile1 { context all } profile2 { context serverside } profile3 { context clientside } profile4 { context clientside } profile5 { context serverside }

I want to change the string to have each profile with its context on its own line like that:

profile1 { context all }
profile2 { context serverside }
profile3 { context clientside } 
profile4 { context clientside } 
profile5 { context serverside }

I was thinking of replacing "} " with "}\n" using regsub

set modified_profilelist [regsub -all "string_to_replace" $profilelist "replacement_string" ]

but I can`t find a way to escape the curly brace

Anything I try gives me an error

Thanks in advance

Lauren Yim
  • 12,700
  • 2
  • 32
  • 59

3 Answers3

1

My solution is to use string map to replace the right curly brace + a space with a right curly brace + a new line:

set originalString "profile1 { context all } profile2 { context serverside } profile3 { context clientside } profile4 { context clientside } profile5 { context serverside }"
puts "$originalString"
puts "---"
set newString [string map { "\} " "\}\n"}  $originalString]
puts "$newString"

Output:

profile1 { context all } profile2 { context serverside } profile3 { context clientside } profile4 { context clientside } profile5 { context serverside }
---
profile1 { context all }
profile2 { context serverside }
profile3 { context clientside }
profile4 { context clientside }
profile5 { context serverside }
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
1

If your real data, like the example, makes a valid tcl list, you can treat it as one, and iterate through two elements at a time:

set profiles {profile1 { context all } profile2 { context serverside } profile3 { context clientside } profile4 { context clientside } profile5 { context serverside }}                                                                           
                                                                                                                                                                                                                                                  
foreach {name value}  $profiles {                                                                                                                                                                                                                 
    puts [list $name $value]                                                                                                                                                                                                                      
}                                                                                                                                                                                                                                                 
Shawn
  • 47,241
  • 3
  • 26
  • 60
0

In Tcl a closing curly bracket has no special meaning without an opening curly bracket before it, so you can safely do the following:

regsub -all } $profilelist }\n

If you want to use it inside a procedure, then this will also work:

regsub -all \} $profilelist \}\n
steveh1491
  • 11
  • 3