Author Topic: How to create a file of just matched regular expressions  (Read 9247 times)

dwsmith37

  • Community Member
  • Posts: 6
  • Hero Points: 0
How to create a file of just matched regular expressions
« on: August 26, 2007, 07:52:18 AM »
I am trying to create a file containing one line per matched regular expression and containing only the string that matched.  I have tried using both the find command in SlickEdit and the SlickEdit grep.  All I seem to be able to do is create a file with a line for each filename the regexp is found in and the complete line for each instance of the regexp.  The found regular expressions are highlighted in blue, but that does not translate into selection for a cut.

This is possible with Gnu grep from SourceForge using the -o and -h options.

Am I just missing something or is this not possible without programming SlickEdit?

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: How to create a file of just matched regular expressions
« Reply #1 on: August 26, 2007, 11:01:04 AM »
I'd propose to use [gui-]replace and '[gui-]save-as' afterwards. This is even more flexible since you're able to 're-format' the matches at will and it's also multi-file capable - but double-check the configuration (e.g. 'Leave modified files open') first ;)
The [gui-] commands are those in the menu: 'Search>Replace [in Files]' resp. 'File>Save As'.

HS2
« Last Edit: August 26, 2007, 11:03:59 AM by hs2 »

dwsmith37

  • Community Member
  • Posts: 6
  • Hero Points: 0
Re: How to create a file of just matched regular expressions
« Reply #2 on: August 26, 2007, 07:16:01 PM »
I am not exactly sure what you mean.  If I do a replace, all of the file that did not match is still there.  If I do a Find, the entire line is listed for every match.  The matches are highlighted, but when I Save, everything is in the output file, including all the other text on the lines that had matches.

My goal is to pull all unique structure field names out of a large MATLab program.  Unfortunately, MATLab doesn't require (or allow) the definition of a structure before it is used, so structure fields can be added anywhere in the program making documentation and maintenance a nightmare.  I match every use of a structure field with a regular expression like: G_PrgParms.:w      (any field of structure G_PrgParms)

At the end, after sorting and removing duplicates in SlickEdit I have (ex:)

G_PrgParms.bAtEndOfFile
G_PrgParms.bFileOpen
G_PrgParms.ColorRunning
G_PrgParms.ColorStart
G_PrgParms.DirectoryName
G_PrgParms.ExitHandle
G_PrgParms.hSTAT
G_PrgParms.iActualSamplesPerCycle

Am I not doing something I should?

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: How to create a file of just matched regular expressions
« Reply #3 on: August 26, 2007, 08:21:32 PM »
You could use this on command line (Slick regex) to extract all 'G_PrgParms' (as tagged expression) line separated (in case there is more than match per line):
c/(?*){G_PrgParms.:w}(?*)/#0\n/r*$#
options:
r -> Slick regex
* -> do all w/o prompting
$# -> hilite matches / replaces

This removes all blank lines afterwards:
c/^[ \t]*\n//r*$#   

After that you can 'Tools>Sort: entire buffer + remove duplicates' (great feature, isn't it ?) as you probably did and you're almost finished...
HS2

dwsmith37

  • Community Member
  • Posts: 6
  • Hero Points: 0
Re: How to create a file of just matched regular expressions
« Reply #4 on: August 27, 2007, 05:21:01 AM »
We are getting closer!  I appreciate your staying with me hs2.

This last iteration has two problems.  First, while it works properly on lines containing matching text, it leaves lines alone that do not.  Unfortunately, that is the majority of lines.

Second, it only works on the current buffer.  I need to run this on selected files in an entire directory tree.

If you have any more ideas, I am including an example file.  I would need to run this on many different files like this contained in a top level and several subdirectories in a directory tree.

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: How to create a file of just matched regular expressions
« Reply #5 on: August 27, 2007, 12:25:13 PM »
Even closer now ;)

Try this procedure:
- retrieve all occurences: Search>Find in Files: setup your expression e.g. 'G_PrgParms' match case + whole word and check 'Output to editor win' and uncheck 'List matching lines only'

Now (maybe delete 1st non-result line and) strip down the search result as if would be a normal buffer to operate on:
- extract (already line separated) matches: Search>Replace e.g. replace: '(?*){G_PrgParms:w}' with '#0' and use Slick regex -> 'Replace All'
- delete junk lines: Search>Replace e.g. replace: '^~(G_PrgParms:w)?*\n' with '' and use Slick regex -> 'Replace All'

And finally 'Tools>Sort: buffer + remove dups' should give the desired result file to 'File>Save As'.

Amazing, isn't it ?
HS2
« Last Edit: August 27, 2007, 09:14:24 PM by hs2 »

dwsmith37

  • Community Member
  • Posts: 6
  • Hero Points: 0
Re: How to create a file of just matched regular expressions
« Reply #6 on: August 30, 2007, 08:05:15 AM »
We've got it!  What ended up working was:

Find in Files -> (G_PrgParms.:a#) to editor window
Replace in current buffer -> (?*){G_PrgParms.:a#}(?#) with #0
Then sort and remove duplicates

Thanks so much HS2.

MindprisM

  • Senior Community Member
  • Posts: 127
  • Hero Points: 8