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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating a new file using tcl 2

Status
Not open for further replies.

july07

Programmer
Aug 26, 2009
33
0
0
US
I have a big file with a list of testings and there are couple of the testings which are similar. I need some ideas on how i can write a tcl code that will create a new file with the first and last testing of each categories...

I am totally new to tcl, i really dont have any idea on how it works, so any help will be appreciated.

For example i have something like this in ma file currently

Activestatusconnection.5 LED 012356
Activestatusconnection.5 LED 012357
Activestatusconnection.5 LED 012358
Activestatusconnection.5 LED 012359
Activestatusconnection.5 LED 012360
Nonactivestatusconnection LED 60.50.0.0.01
Nonactivestatusconnection LED 60.50.0.0.02
Nonactivestatusconnection LED 60.50.0.0.03
Nonactivestatusconnection LED 60.50.0.0.04
Nonactivestatusconnection LED 60.50.0.0.05

and all i want in the new file is

Activestatusconnection.5 LED 012356
Activestatusconnection.5 LED 012360
Nonactivestatusconnection LED 60.50.0.0.01
Nonactivestatusconnection LED 60.50.0.0.05

Does anybody have any idea on how i can get this.

Can tcl recognize the first and last testing of each categories?
 
Hi

july07 said:
I am totally new to tcl, i really dont have any idea on how it works
Just as a curiosity, then why using Tcl and not a programming language you already know ?
Code:
[b]set[/b] infile [teal][[/teal][b]open[/b] [green][i]"july07.txt"[/i][/green] [green][i]"r"[/i][/green][teal]][/teal]
[b]set[/b] outfile [teal][[/teal][b]open[/b] [green][i]"july07-b.txt"[/i][/green] [green][i]"w"[/i][/green][teal]][/teal]

[b]set[/b] lline [teal]{}[/teal]
[b]set[/b] ltype [teal]{}[/teal]

[b]while[/b] [teal]{[/teal][teal]![/teal] [teal][[/teal][b]eof[/b] [navy]$infile[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
  [b]set[/b] line [teal][[/teal][b]gets[/b] [navy]$infile[/navy][teal]][/teal]
  [b]set[/b] type [teal][[/teal][b]string[/b] range [navy]$line[/navy] [purple]0[/purple] [purple]2[/purple][teal]][/teal]
  [b]if[/b] [teal]{[/teal][navy]$ltype[/navy][teal]!=[/teal][navy]$type[/navy][teal]}[/teal] [teal]{[/teal]
    [b]if[/b] [teal]{[/teal][navy]$lline[/navy][teal]!=[/teal][teal]{}}[/teal] [teal]{[/teal] [b]puts[/b] [navy]$lline[/navy] [teal]}[/teal]
    [b]set[/b] ltype [navy]$type[/navy]
    [b]puts[/b] [navy]$line[/navy]
  [teal]}[/teal]
  [b]set[/b] lline [navy]$line[/navy]
[teal]}[/teal]

[b]if[/b] [teal]{[/teal][navy]$lline[/navy][teal]!=[/teal][teal]{}}[/teal] [teal]{[/teal] [b]puts[/b] [navy]$lline[/navy] [teal]}[/teal]

[b]close[/b] [navy]$outfile[/navy]
[b]close[/b] [navy]$infile[/navy]


Feherke.
 
Hi,

feherke said:
Just as a curiosity, then why using Tcl and not a programming language you already know ?

I want to learn tcl that is why i decided to use it.

Thanks for your help feherke, but when i tried to run your code, the output file was empty.
 
It's because the output goes only to the screen.
But you can easily modify the script so, that the output goes to the $outfile.
 
To write into $outfile
replace
puts <something>
with
puts $outfile <something>
 
mikrom said:
Then google for a Tcl tutorial or book,
yes thank you ive been doing that.

I was thinking of another way to go by this program, and i have a question, hope someone can help.

"If the word before the first dot is the same, output the first and last testing". How do i put this in tcl code.

Thanks any help or idea would be appreciated.
 
Hi feherke,

And you forgot to remove the last line before closing the files too
Code:
if {$lline!={}} { puts $lline }
It's not necessary.
 
Mikrom said:
you can use therefore
string first command:

so im kind of confused with the "string first" i know the statement has to look like this:

string first string1 string2

I know string2 is $line and im thinking string1 should be each line. can you explain how i can write string1 in tcl?

I tried this but it did not work.
Code:
set type [string first $line $line]

Thanks for your help.
 
Hi july07,

Using string first
Code:
  set result [string first "." $line]
  if {$result != -1} {
    set type [string range $line 0 $result]
  } else {
    set type [string range $line 0 2]
  }

Using regexp
Code:
  set result [regexp {([^\.]+)\.} $line match first_word_before_the_dot]
  if {$result == 1} {
    set type $first_word_before_the_dot
  } else {
    set type [string range $line 0 2]
  }

Note: In both cases if a word before firts dot not found, then the feherke' approach will be used.
 
Hi july07,

Say us:
Why you try to learn exactly Tcl of all the other scripting languages available: Perl, Python, Ruby, ... ???
 
Hi Mikrom,

I've heard people talk about it and i figured it would be fun to learn. Hopefully I will learn as much programming as i want as time goes by.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top