egrep from a variabl
Posted by neo (The Architect), 3 May 2005How 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 2005egrep 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 2005Thanks will start using that.
BTW does it differ significantly in any way from Unix's egrep?
Posted by neo (The Architect), 3 May 2005Does the regexp command take variables as the pattern?
Posted by admin (Graham Ellis), 3 May 2005The 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 2005I have a file with the following contents
I need tocheck if the given pattern (by the user) matchesa an uncommented linein the file.
I use this
How can I do this in TCL?
Posted by admin (Graham Ellis), 4 May 2005Code:
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 2005Graham, 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 2005That doesnt work I guess?
Posted by admin (Graham Ellis), 11 May 2005It 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.
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho