From: Joseph Pecoraro Date: 2008-01-26T14:59:11+09:00 Subject: Re: regular expression negate a word (not character) I just wrote up a quick script to do what I was thinking. I decided to make a different utility only because of the complications that would arise with tons of switches on the command line if I were to add it to a find/replace utility. (The user would have to say which regex they wanted for the actual replacement, and other inherent problems... moving on) So without further ado, here is my example ------------------------------ joe[~/code/script]$ cat > input winter tire tire retire tired snow tire snow tire some snowtires joe[~/code/script]$ grepall -2 tire --neg snow input input [1]: winter tire input [2]: tire input [3]: retire input [4]: tired joe[~/code/script]$ grepall usage: grepall [-#] ( [-n] regex ) [filenames] # - the number of regular expressions, defaults to 1 regex - regular expessions to be checked on the line filenames - names of the input files to be parsed, if blank uses STDIN options: --neg or -n do not match this regular expression special note: When using bash, if you want backslashes in the replace portion make sure to use the multiple argument usage with single quotes for the replacement. ------------------------------------------------------ The utility is hopefully easily to understand, although the usage is tough to present: - line by line processing - in the above example the -2 says there will be two regular expressions - the first is /tire/ and that needs to match - the second is /snow/ and that is Negated because of the --neg (or just -n) option - the last argument is the filename The output needs to be tweaked, maybe so its more like grep. Right now it allows for multiple files so it prints the filename, [line number], and the line where there was a full match for all the regular expressions as correctly matched (negated where necessary). Obviously this is very simple at the moment and it doesn't cover the specific situation you mentioned where there was the word tire and snowtire on the same line. However if that is an issue you can: - find and replace all words SNOW with SPECIAL_STRING in all files - do what you have to do... - turn all SPECIAL_STRINGs back into SNOW in all files That can be done rather easily. You will have lost the case sensitivity in the word SNOW, but you can get around that by making your SPECIAL_STRING something like XsXnXoXwX based on the original case values of snow. I hope that made sense. Well I better get to bed, you made my night interesting! -- Posted via http://www.ruby-forum.com/.