TCL to open and combine multiple text files extracting only integer values at specified lines
TCL to open and combine multiple text files extracting only integer values at specified lines
(OP)
Hi I am new to tcl programming. I am in process of creating a automation tcl and i would like to request for ur help.
Scenario:
I have multiple text files which contains integer data of some variables called nodes and ele.The sample file looks like this.
I have hundreds of those text files.
One similarity in all files is that in line 3 and 4 , we have the element ids and node ids
Now, the thing i would like to automate is that , i want to build a scripts in tcl, after running which, it reads all the node ids and elements ids in lines 3 and 4; only integer values (in all of those 100 txt files) and stores them in a separate txt file, each one for node and for element.
1. Final node txt file should have only node ids from all files
2. Final elem txt file should have only elem ids from all files
It is possible through array? how can we best code it in tcl. pls help. its urgent.
I lack direction. can u help me and guide me , how to create that tcl?
thank you in advance
Scenario:
I have multiple text files which contains integer data of some variables called nodes and ele.The sample file looks like this.
#C
##Id Files
Ele 15235,16845,555845,21452,451254
Nodes 15478,26558,78549,12547,12546,13556
Thickness 2.5
Nodal thickness 0
##Id Files
Ele 15235,16845,555845,21452,451254
Nodes 15478,26558,78549,12547,12546,13556
Thickness 2.5
Nodal thickness 0
I have hundreds of those text files.
One similarity in all files is that in line 3 and 4 , we have the element ids and node ids
Now, the thing i would like to automate is that , i want to build a scripts in tcl, after running which, it reads all the node ids and elements ids in lines 3 and 4; only integer values (in all of those 100 txt files) and stores them in a separate txt file, each one for node and for element.
1. Final node txt file should have only node ids from all files
2. Final elem txt file should have only elem ids from all files
It is possible through array? how can we best code it in tcl. pls help. its urgent.
I lack direction. can u help me and guide me , how to create that tcl?
thank you in advance
RE: TCL to open and combine multiple text files extracting only integer values at specified lines
OK, you need a little help but first show us an effort what have you done so far.
You need to divide your complex task into simple tasks -i.e.:
1. create a procedure which reads one file line by line - for that you can use the tcl commands open, close and gets.
2. create a procedure which processes one line and if possible it extracts the desired numbers - for this you can use several string and list operations
3. create a procedure which delivers list of all files in a given directory and process this list - use the command glob.
Have you tried to solve something of these tasks ?
RE: TCL to open and combine multiple text files extracting only integer values at specified lines
As for the task no.2 (see my previous post): I would create 2 global lists (i.e. arrays): one for elements and other for nodes.
The procedure which processes lines should read this data from every file and append them to the global lists - e.g.:
CODE
RE: TCL to open and combine multiple text files extracting only integer values at specified lines
Other tasks (from my previous post) are not difficult:
1. This procedure opens a file with a name from the argument for reading, then
reads it line by line and for every line calls other procedure (see my previous post. And at end it closes the file.
CODE
3. If your data files would be in same directory named e.g. *.txt, then you could create a list of them using the glob command - like this:
CODE
Let me know if you have other questions ...
RE: TCL to open and combine multiple text files extracting only integer values at specified lines
However, in the meantime as I tried to help OP how to start with this task, I created fully functional script which works according to the initial specification. Maybe, it wouldn't be good idea simply to throw it away, because it could help other beginning Tcl programmers how to start.
So, I rather post it here:
First I created some data files: radicalgiant_01.txt, radicalgiant_02.txt, radicalgiant_03.txt
which contain the data like given above - e.g.:
radicalgiant_01.txt
CODE
Then I created the program - some procedures are discussed above:
radicalgiant.tcl
CODE
Then running the script above causes this output and produces 2 files:
CODE
The result file radicalgiant_elem.txt contains the numerical data for elements
CODE