Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
egrep from a variabl

Posted by neo (The Architect), 3 May 2005
How do i use egrep when I have the pattern to be matched in a variable?
I need to do an exact match of a pattern from a file. I tried using egrep in the TCL script but it keeps failing because of the variable that I use as the pattern.
How is this done generally?

Posted by admin (Graham Ellis), 3 May 2005
egrep is a Unix / Linux operating system command rather than a part of Tcl.   If you're looking to filter the contents of variables in your Tcl program, use the regexp command instead.

Posted by neo (The Architect), 3 May 2005
Thanks will start using that.
BTW does it differ significantly in any way from Unix's egrep?

Posted by neo (The Architect), 3 May 2005
Does the regexp command take variables as the pattern?

Posted by admin (Graham Ellis), 3 May 2005
The regexp command takes POSIX regular expressions which are considerably extended beyond those of egrep (at least the old version of egrep I'm familiar with).  Variable substitution for the regular expression pattern works exactly the same way that it works elsewhere in Tcl ... it has to, as variables are interpretted before the parameters ever gat passed to the command itself  

Posted by neo (The Architect), 3 May 2005
I have a  file with the following contents
Quote:
file1();
file2();

I need tocheck if the given pattern (by the user) matchesa an uncommented linein the file.
I use this
Code:
egrep ^file1\(\) filename

How can I do this in TCL?

Posted by admin (Graham Ellis), 4 May 2005
Code:
set fh [open filename r]
while {[gets $fh line] > -1} {
   if {[regexp {^file1\(\)} $line]} {
       puts $line
   }
}


Tested on you data, and works.

I see your comment that you need to test for a pattern entered by the user.

Regular expressions in the hand of the "unaware" can lead to some fascinatingly weird results and strange error messages as far as they are concerned. They also provide potential security holes in some situations.

Is your user community is highly technical (i.e. a group of programmers who understand regular expressions) and friendly to your code? If not, I suggest that you ask them to input (say) just the file name part of what you're looking for and add in the ^ and  \(\) in your own code.  You will probably also want to check that their input comprises only letters and digits which you can do with another regular expression.


Posted by neo (The Architect), 4 May 2005
Graham, what would TCL do without you.  
Thanks a lot for all the help I must go and try it now.


BTW did you try it with the pattern in a variable?

Posted by neo (The Architect), 11 May 2005
That doesnt work I guess?

Posted by admin (Graham Ellis), 11 May 2005
It will work perfectly well.   A regular expression is just a string. You can put it into a variable and have Tcl use the contents of that variable in exactly the same way as anywhere else you would use the contents of a variable.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho