Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change lower case to upper case in Tcl?

Tags:

regex

tcl

I am trying to regsub all lower case letters to upper case in a file using character classes:

 regsub -all { [:lower:] } $f { [:upper:] } f

but it doesn't do the substitution.

like image 205
TheBlackCorsair Avatar asked Feb 18 '23 23:02

TheBlackCorsair


2 Answers

Just read the file into a string and use string toupper. Then write it back out to a file.

set fp [open "somefile" r]
set file_data [read $fp]
close $fp

set file_data [string toupper $file_data]

set fp [open "somefile" "w"]
puts -nonewline $fp $file_data
close $fp
like image 95
TrojanName Avatar answered Mar 04 '23 21:03

TrojanName


yes, above will work like charm.

set f [string toupper $f]

f is some list or string. If you want file operations, as usual read from file and write .

Although if you just want to use regsub, give this a try

set f "this is a line"

regsub -all {.*} $f {[string toupper {&}]} f

set f [subst -nobackslashes -novariables $f]

now your contents in f is uppercased.

note: it looks like long way but useful when selecting just particular text to be upper or lowercased

Thanks,

like image 37
Downey_HUff Avatar answered Mar 04 '23 21:03

Downey_HUff