Hi Everyone,
In Tcl, how to get the the contents of user specified file.
ie: Output the number of lines in the file, output number of words in the file and take a regular expression input and display its occurence in that file. { all operations prompting user to give inputs}
Regards,
tclUser
Hi
these are the some code line which can fulfill your requirement .
No of lines in file
wc -l file_name
No of words in file
wc -w file_name
Search the regular expression in file
sed '/regular exp/p' file_name
now you can write the code which ask for the user input.
refer the below code and modify accord to your needs.
Counting comments in a source
In Tcl you can achieve this as follows:
set fo [open filename.txt r]
set file_data [read $fo]
close $fo
puts "Number of words in file are: [llength $file_data]"
puts "Number of lines in file are: [llength [split $file_data \n]]"
For finding regular expression and print its occurrence, it can be done using regexp command.
regexp pattern $file_data value
Here pattern is regular expression on basis of which you need to find data, and if it is found it will store this in value variable. You can use any variable name here.
Hi sarbjit,
The code would work to get number of lines but not number of words as to get number of words in file , i think i have split file with delimiter as "space"
Regards,
tclUser
For getting number of lines, once you have read the entire file using read if you split it with new line character and use llength it will be returning number of lines. In actual number of lines should be 1 less than result provided.
Modified code for number of lines
set fo [open file r]
set file_data [read $fo]
close $fo
puts "Number of words in file are: [llength $file_data]"
puts "Number of lines in file are: [expr [llength [split $file_data \n]] -1 ]"
If my sample file has following data:
a b
c
d
e f
g
h
I ran this code and got the following result:
132 C% tclsh tclscript
Number of words in file are: 8
Number of lines in file are: 6
Hi Unix Experts,
Am trying to cut the contents of my file(ddl.txt) before the first occurrence of the character '('.
Example if my file is-
HE IS OK
HE IS FINE
(
HE IS THERE
)
I want the output into another file(output.txt) as
HE IS OK
HE IS FINE
Please help me to do this