Hide lines NOT matching search string

Find, replace, find in files, replace in files, regular expressions

Hide lines NOT matching search string

Postby spaceBAR » Mon Mar 10, 2008 11:37 pm

Using: UEStudio 6.50+4

Would someone please post and/or send me a simple sample script and/or macro to hide all lines NOT matching a search string in file that is opened in one of the tabs(i.e UEStudio window).

tia
User avatar
spaceBAR
Basic User
Basic User
 
Posts: 18
Joined: Tue Oct 19, 2004 11:00 pm

Re: Hide lines NOT matching search string

Postby Mofi » Tue Mar 11, 2008 3:38 am

Hiding single lines is not possible at the moment. Only a block can be made hidden. And as several times written a search for a line not containing a string is not possible (maybe with Perl, I'm not a Perl expert). You can run a regular expression replace for lines containing a string, which adds a special character at start of the lines containing the string, then run a replace which deletes all lines not starting with the special character and last run a third regular expression to delete the special character at start of the remaining lines. Or you search for lines with a specific string and use the find option List Lines Containing String which you then can copy to a new file.

Edited on 2010-03-02: See also my post Filtering lines using the Find command with UE v16.00+.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4040
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Hide lines NOT matching search string

Postby pietzcker » Tue Mar 11, 2008 4:23 am

Well, a Perl regex that matches a consecutive block of lines that don't contain a certain string is

Code: Select all
(?:^(?:(?!%REGEX%).)*$\r\n)+

where %REGEX% has to be substituted by the search string that you need (careful: special characters like [\^$.|?*+(){} have to be escaped by a backslash, i.e. \* will match a *).

However, I'm running into a problem when trying to hide a block like this as the option to hide selected lines is grayed out in my UE V14.00+4 (even if there are multiple lines selected), and I don't know why. Maybe Mofi can help here.

The problem remains, though, that single lines (that will also be matched by this regex) cannot be hidden. So maybe using a search for

Code: Select all
^(?:(?!%REGEX%).)*$\r\n

and choosing the option "List lines containing string" will approach better what you need.

HTH,
Tim
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Hide lines NOT matching search string

Postby Mofi » Tue Mar 11, 2008 4:29 am

Tim, you have to enable at Configuration - Editor Display - Code Folding the 2 settings:

Enable show/hide lines and code folding
Enable show/hide lines in non syntax highlighted files
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4040
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Hide lines NOT matching search string

Postby pietzcker » Tue Mar 11, 2008 5:36 am

Thanks, Mofi!

I've just tried it, and it works "as designed", i.e., it won't be (I guess) what spaceBAR wanted because the first line of a hidden block remains visible (and therefore a single line can't be hidden, either). So it comes back to the "List lines containing string" method mentioned earlier...
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Hide lines NOT matching search string

Postby Bego » Tue Mar 11, 2008 7:19 am

You won't get lucky trying to find a solution with any regexp-approach NOT finding sth.
So you might write a small macro that goes through the code line by line,
examine that line and if "string not found" then append to "clipboard 9".
So you can collect all lines without the string and paste them to a new document.
Disadvantage: Potentially slow, only a copy of the document is created (not a filter or sth.)
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Hide lines NOT matching search string

Postby pietzcker » Tue Mar 11, 2008 11:23 am

Hi Bego,

what's wrong with "List lines containing string" using ^(?:(?!%REGEX%).)*$\r\n as the search string? I'm probably overlooking something...

Cheers,
Tim
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Hide lines NOT matching search string

Postby Bego » Tue Mar 11, 2008 2:23 pm

Hi Tim,

maybe I am missing sth.
If I have a file like this:
Code: Select all
bblabla
mmgla
lalala
blubb
blubb lala blubb
kjf
another lala blub
end


and I do a regexp-search with
Code: Select all
^(?:(?!%lala%).)*$\r\n
and "list lines containing" checked, i get all lines displayed.

UE 14.00+3
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Hide lines NOT matching search string

Postby pietzcker » Tue Mar 11, 2008 3:09 pm

Hi Bego,

remove the % signs - you want to search for ^(?:(?!lala).)*$\r\n instead.

:)

Tim

P.S.: The last line has to be \r\n terminated, too, or it won't match...
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Hide lines NOT matching search string

Postby Bego » Tue Mar 11, 2008 3:20 pm

Aaaah, bingo. OK, that is really a good thing now, thx Tim :-)
So, even myself can learn a bit from time to time ;-)

cu, Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Hide lines NOT matching search string

Postby boefei » Tue Apr 15, 2008 1:09 pm

The approach I took on this was to use grep.

I have cygwin installed on my machine and simply created a tool
Inverse Grep

C:\cygwin\bin\grep.exe -v %sel% %F


I also have one without -v simply called grep.

I like this because it spits it out into a new window for easy further editing.
User avatar
boefei
Newbie
 
Posts: 3
Joined: Tue Apr 15, 2008 12:53 pm
Location: Reno, NV

Re: Hide lines NOT matching search string

Postby Bego » Tue Apr 15, 2008 1:58 pm

Hi boefei,

I tried it out because I did not know Cygwin yet and also miss grep on Windows systems. Runs smart. Thank you for the tip.

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Hide lines NOT matching search string

Postby boefei » Tue Apr 15, 2008 3:51 pm

No problem

Forgot to add just highlight the item you want(want removed) and run the command.


MFG
User avatar
boefei
Newbie
 
Posts: 3
Joined: Tue Apr 15, 2008 12:53 pm
Location: Reno, NV

Re: Hide lines NOT matching search string

Postby psstarkey » Thu Apr 24, 2008 11:58 am

I am wanting something like what spacebar is asking for except I don't want to create a new file with the lines matching the search. I want to collapse all lines NOT matching the search so I can easily edit the ones matching the criteria and then re-expand the whole file with my changes in place.

What about something like that?
psstarkey
Newbie
 
Posts: 3
Joined: Thu Apr 24, 2008 11:56 am

Re: Hide lines NOT matching search string

Postby Bego » Thu Apr 24, 2008 2:00 pm

There is just the find-option "list lines containing string" that opens a new window with all those lines.
You then can double-click those lines one by one and edit them.
But the feature you describe is not implemented (yet)
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Next

Return to Find/Replace/Regular Expressions