Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help using '\n' command in Tcl

Status
Not open for further replies.

july07

Programmer
Aug 26, 2009
33
US
I have a output file from sql database using tcl, the file has this format:

{testing1 read-write {This testing went well.} {} {} Integer32} {testing2 read-write {This testing went well.} {} {} Integer32}{testing3 read-write {This testing went well.} {} {} Integer32}{testing4 read-write {This testing went well.} {} {} Integer32}

The output is on one line:

I want each testing to be on different line that is:

{testing1 read-write {This testing went well.} {} {}Integer32}
{testing2 read-write {This testing went well.} {} {} Integer32}
{testing3 read-write {This testing went well.} {} {} Integer32}
{testing4 read-write {This testing went well.} {} {} Integer32}

This is the code, how do i make it output each testing on different lines:

Code:
set outfile [open "july07.txt" w]
set query "select name, Access, description, number, units, syntax from testingresults 
WHERE LIKE 'testing%' AND Access='read-write'"
set foo [sql $query]
puts $outfile $foo

I think its more of tcl than sql
I know i would have to use '\n', but i dont know where to insert it. Any help will be appreciated:

 
Hi july07,
july07 said:
I want each testing to be on different line that is:

{testing1 read-write {This testing went well.} {} {}Integer32}
{testing2 read-write {This testing went well.} {} {} Integer32}
{testing3 read-write {This testing went well.} {} {} Integer32}
{testing4 read-write {This testing went well.} {} {} Integer32}
Try this:
Code:
[COLOR=#804040][b]set[/b][/color] mystring [COLOR=#ff00ff]"{testing1 read-write {This testing went well.} {} {} Integer32} {testing2 read-write {This testing went well.} {} {} Integer32}{testing3 read-write {This testing went well.} {} {} Integer32}{testing4 read-write {This testing went well.} {} {} Integer32}"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]mystring = '$mystring'"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]""[/color]

[COLOR=#0000ff]# create lines separators [/color]
[COLOR=#804040][b]set[/b][/color] mystring [[COLOR=#804040][b]string[/b][/color] map {[COLOR=#ff00ff]"Integer32[/color][COLOR=#6a5acd]\}[/color][COLOR=#ff00ff]"[/color] [COLOR=#ff00ff]"Integer32[/color][COLOR=#6a5acd]\}[/color][COLOR=#ff00ff]$"[/color]} [COLOR=#008080]$mystring[/color]]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Modified [/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]mystring = '$mystring'"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]""[/color]

[COLOR=#0000ff]# split string into array[/color]
[COLOR=#804040][b]set[/b][/color] linenum [COLOR=#ff00ff]0[/color]
[COLOR=#804040][b]foreach[/b][/color] line [[COLOR=#804040][b]split[/b][/color] [COLOR=#008080]$mystring[/color] [COLOR=#ff00ff]"$"[/color]] {
  [COLOR=#804040][b]incr[/b][/color] linenum
[COLOR=#0000ff]  # remove leading and trailing spaces[/color]
  [COLOR=#804040][b]set[/b][/color] line [[COLOR=#804040][b]string[/b][/color] trim [COLOR=#008080]$line[/color]]
  [COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$line[/color] != {}} {
    [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"$linenum.line = '$line'"[/color]
  }
}
Output:
Code:
$mystring = '{testing1 read-write {This testing went well.} {} {} Integer32} {te
sting2 read-write {This testing went well.} {} {} Integer32}{testing3 read-write
 {This testing went well.} {} {} Integer32}{testing4 read-write {This testing we
nt well.} {} {} Integer32}'

Modified $mystring = '{testing1 read-write {This testing went well.} {} {} Integ
er32}$ {testing2 read-write {This testing went well.} {} {} Integer32}${testing3
 read-write {This testing went well.} {} {} Integer32}${testing4 read-write {Thi
s testing went well.} {} {} Integer32}$'

1.line = '{testing1 read-write {This testing went well.} {} {} Integer32}'
2.line = '{testing2 read-write {This testing went well.} {} {} Integer32}'
3.line = '{testing3 read-write {This testing went well.} {} {} Integer32}'
4.line = '{testing4 read-write {This testing went well.} {} {} Integer32}'
 
Hi Mikrom, thanks for the reply.

Because the file is a big one and its not exactly like the example i gave you, i modified your code to this:

Code:
set mystring [open "readwrite.txt" r]
set outputfile [open "readwrite2.txt" w]
puts $outputfile "\$mystring = '$mystring'"
puts $outputfile ""

# create lines separators 
set mystring [string map {"Integer32\}" "Integer32\}$"} $mystring]
puts $outputfile "Modified \$mystring = '$mystring'"
puts $outputfile ""

# split string into array
set linenum 0
foreach line [split $mystring "$"] {
  incr linenum
  # remove leading and trailing spaces
  set line [string trim $line]
  if {$line != {}} {
    puts $outputfile "$linenum.line = '$line'"
  }
}


But not every thing ends with ends with Integer32} and they are not all the same length so your code wont work.

what im trying to say is that each time my code above finds a match, I want it to put it on another line instead of the same line.

Thanks again.

 
july07 said:
But not every thing ends with ends with Integer32} and they are not all the same length so your code wont work.
But according to the data you posted first I thought, that every line should end with Integer32}
You need to define a rule how every line should look, otherwise you cannot split the input into lines.
 
Hi july07

Second try - I used regexp in a loop:
Code:
[COLOR=#804040][b]set[/b][/color] mystring [COLOR=#ff00ff]"{testing1 read-write {This testing went well.} {} {} Integer32} {testing2 read-write {This testing went well.} {} {} Integer32}{testing3 read-write {This testing went well.} {} {} Integer32}{testing4 read-write {This testing went well.} {} {} Integer32}"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]mystring = '$mystring'"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]""[/color]

[COLOR=#804040][b]set[/b][/color] count [COLOR=#ff00ff]0[/color]
[COLOR=#0000ff]# start from beginning of the string[/color]
[COLOR=#804040][b]set[/b][/color] pos [COLOR=#ff00ff]0[/color]
[COLOR=#0000ff]# match the lines repeatedly[/color]
[COLOR=#804040][b]while[/b][/color] {[[COLOR=#804040][b]regexp[/b][/color] -start [COLOR=#008080]$pos[/color] [COLOR=#ff0000]\[/color]
        {(\{[^\{]+\{[^\{]+\}\s*\{[^\{]*\}\s*\{[^\{]*\}[^\{]+\})} [COLOR=#ff0000]\[/color]
        [COLOR=#008080]$mystring[/color] match line] == [COLOR=#ff00ff]1[/color]} {
[COLOR=#0000ff]  # increment line counter[/color]
  [COLOR=#804040][b]incr[/b][/color] count
[COLOR=#0000ff]  # print line found  [/color]
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"$count.line: '$line'"[/color]
[COLOR=#0000ff]  # adjust position[/color]
  [COLOR=#804040][b]set[/b][/color] pos [[COLOR=#804040][b]expr[/b][/color] {[COLOR=#008080]$pos[/color]+[[COLOR=#804040][b]string[/b][/color] length [COLOR=#008080]$line[/color]]}]
}
Output:
Code:
$mystring = '{testing1 read-write {This testing went well.} {} {} Integer32} {te
sting2 read-write {This testing went well.} {} {} Integer32}{testing3 read-write
 {This testing went well.} {} {} Integer32}{testing4 read-write {This testing we
nt well.} {} {} Integer32}'

1.line: '{testing1 read-write {This testing went well.} {} {} Integer32}'
2.line: '{testing2 read-write {This testing went well.} {} {} Integer32}'
3.line: '{testing3 read-write {This testing went well.} {} {} Integer32}'
4.line: '{testing4 read-write {This testing went well.} {} {} Integer32}'
Try it on your data.
 
Thanks Mikrom, your code works for a small file, my file is large and it not working for it. Thanks for your help. I will keep trying and see if i can figure it out.
 
july07 said:
code works for a small file, my file is large and it not working for it
Maybe the problem is, how you read your file.
 
Thanks i was able to use foreach

Code:
set outfile [open "july07.txt" w]
set query "select name, Access, description, number, units, syntax from testingresults 
WHERE LIKE 'testing%' AND Access='read-write'"
set foo [sql $query]

foreach result $foo {
	puts $outfile $result
}
[/code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top