TCL How to read , extract and count the occurent in .txt file (Current Directory)

Posted by Passion on Stack Overflow See other posts from Stack Overflow or by Passion
Published on 2010-04-26T23:01:47Z Indexed on 2010/04/27 4:53 UTC
Read the original article Hit count: 369

Filed under:
|
|
|

Hi Folks, I am beginner to scripting and vigorously learning TCL for the development of embedded system.

I have to Search for the files with only .txt format in the current directory, count the number of cases of each different "Interface # nnnn" string in .txt file, where nnnn is a four or 32 digits max hexadecimal no and o/p of a table of Interface number against occurrence. I am facing implementation issues while writing a script i.e, Unable to implement the data structure like Linked List, Two Dimensional array. I am rewriting a script using multi dimension array (Pass values into the arrays in and out of procedure) in TCL to scan through the every .txt file and search for the the string/regular expression ‘Interface # ’ to count and display the number of occurrences. If someone could help me to complete this part will be much appreciated.

Search for only .txt extension files and obtain the size of the file

Here is my piece of code for searching a .txt file in present directory

set files [glob *.txt]
if { [llength $files] > 0 } {
    puts "Files:"
    foreach f [lsort $files] {
        puts "    [file size $f] - $f"
    }
} else {
    puts "(no files)"
}

I reckon these are all the possible logical steps behind to complete it i) Once searched and find the .txt file then open all .txt files in read only mode ii) Create a array or list using the procedure (proc) Interface number to NULL and Interface count to zero 0 iii) Scan thro the .txt file and search for the string or regular expression "interface # iv) When a match found in .txt file, check the Interface Number and increment the count for the corresponding entry. Else add new element to the Interface Number list v) If there are no files return to the first directory

My o/p is like follows

Interface    Frequency
123f            3
1232       

 4

© Stack Overflow or respective owner

Related posts about tcl

Related posts about read