Solved

search for a specific text  pattern

Posted on 2014-07-27
24
210 Views
Last Modified: 2014-09-08
hi,
I am trying to  search  in all JSP pages of my code for a specific text  pattern . ... ....using  editor Eclipse and editplus

text pattern is like this ..

<display:column  ......
<s:checkbox name="anything"  ..........  value="$displaytable......"


I am finding it difficult to find the matching jsp page...... How can I achieve this ?

Thanks
0
Comment
Question by:cofactor
  • 12
  • 7
  • 3
  • +1
24 Comments
 

Author Comment

by:cofactor
ID: 40222633
comments please
0
 

Author Comment

by:cofactor
ID: 40222715
comments please
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40222940
I don't use Eclipse myself, but what you need to find is where you can enter a search using "Regular expressions".  Regular expressions are used to find patterns in things - such as a pattern in your code.

I'm attaching an example of how this looks in IntelliJ (IDEA) which is another Java development environment.

See if you can find something similar in Eclipse.  Once you have located where you can enter a regular expression, then this page http://www.eclipse.org/tptp/home/downloads/installguide/gla_42/ref/rregexp.html will help explain the format of the regular expression you need to use.

The example you give is complex and it will take time to figure out the correct regular expression - but it is possible using this approach.

Doug
file-search.jpg
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:cofactor
ID: 40223401
I know regular expression. .... but that would be complex using here.

I am looking for a solution. It does not  matter whether solution comes through regular expression or through some hacks or tricks.  I want to find the jsp pages where that matches.

Do you have any other alternative approach ?
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40223431
Do you have to find the pages from within Eclipse?

If not I think one reasonable tool for this sort of problem is 'awk'.   It allows you to match patterns of text in files and do things when you match that pattern.

In particular I think you're looking for what they call a record range within two patterns:
https://www.gnu.org/software/gawk/manual/html_node/Ranges.html

Here's more help on awk pattern matching:
http://www.theunixschool.com/2012/05/awk-match-pattern-in-file-in-linux.html

I'm not awk expert, but I think if you follow some of those examples you should be able to get what you need pretty quickly.

Doug
0
 

Author Comment

by:cofactor
ID: 40223566
>>>Do you have to find the pages from within Eclipse?

No.

I can use Textpad , Editplus  or  any text editor  or  any tool which could help me to find the matching jsp page.

>>>If not I think one reasonable tool for this sort of problem is 'awk'.  

I am in windows. no linux tool please.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40223607
I am in windows. no linux tool please.
Then you'll probably need to use Powershell to enable use of searching with regex
0
 

Author Comment

by:cofactor
ID: 40223992
But what is the regex for this ?

I may not need PowerShell ...because in my EDITPLUS text editor ..there is a  regex search choice.

Without regex ... If you have any  hacks / tricks to meet the goal ...please let me know.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40224009
If you could attach (with extension renamed to .txt) an actual jsp, i'll see what i can do
0
 

Author Comment

by:cofactor
ID: 40224390
>>>If you could attach (with extension renamed to .txt) an actual jsp, i'll see what i can do

I am yet to find the matching actual jsp  :)  ...... its a jungle of JSP pages.


Any thoughts ..how I can reach my goal to finding that JSP ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40224564
http://sourceforge.net/projects/unxutils/

is what i often use on Windows. That will give you grep
0
 

Author Comment

by:cofactor
ID: 40224970
OK. have downloaded it . I can run sh.exe

what is   the grep  command for this ? stuck here

<display:column  ......
<s:checkbox name=" ..........  value="$displaytable......"


I am worried because its a two liner ...is grep capable of searching such patterns ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40225560
It's actually not possible with that version of grep, though sed could do it. What about using a small Java app? (pass the directory to the app below) IOUtils class is at

http://technojeeves.com/index.php/aliasjava1/57-file-to-string-in-java

import net.proteanit.io.IOUtils;

import java.io.*;

import java.nio.file.AccessDeniedException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import java.util.*;
import java.util.regex.*;


public class FileSearcher extends SimpleFileVisitor<Path> {
    private File startDir;
    private String pattern = "(?ms)<display:column.*<s:checkbox name=.*value=\"\\$displaytable";

    public FileSearcher(File startDir) {
        this.startDir = startDir;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public static void main(String[] args) {
        try {
            File startDir = new File(args[0]);
            FileSearcher fs = new FileSearcher(startDir);

            if (args.length > 1) {
                fs.setPattern(args[1]);
            }

            fs.search();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void search() throws IOException {
        Files.walkFileTree(startDir.toPath(), this);
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
        throws IOException {
        File f = file.toFile();
        String filePath = f.getAbsolutePath();

        if (filePath.matches("(?i).*\\.jsp$")) {

            String s = IOUtils.fileToString(filePath);
            Matcher m = Pattern.compile(this.pattern).matcher(s);

            if (m.find()) {
                System.out.println(f.getAbsolutePath());
            }
        }

        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
        throws IOException {
        return FileVisitResult.CONTINUE;
    }
}

Open in new window

0
 

Author Comment

by:cofactor
ID: 40225944
>>>What about using a small Java app?
sure .  thats ok .... any tricks / hacks .....anything which meet the goal is  just fine.

ok....trying to understand your code ....  regex is scary always :-)

anyway ..

here..
    private String pattern = "(?ms)<display:column.*<s:checkbox name=.*value=\"\\$displaytable";

Can you please explain this two bold colored part.

for first part ...I got this from manual
.  = >everything except \n in a regular expression within parentheses
*  = >0 or more times

but when combined .*    .... this does not seem bear any meaning .....please clarify this.




for second part...
\\  I think you added this to match the string literal just.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40226132
for first part ...I got this from manual
.  = >everything except \n in a regular expression within parentheses
*  = >0 or more times

but when combined .*    .... this does not seem bear any meaning .....please clarify this.
Well spotted - but - you didn't read deeply enough ;)
http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#DOTALL
Also i would urge you to take your info from the API docs for Pattern. The dot comes in as the first item under "Predefined character classes"
for second part...
\\  I think you added this to match the string literal just.
Yes, that's there to escape the character '$' (which in regex is a reserved character, meaning the end of input.
0
 

Author Comment

by:cofactor
ID: 40237314
@CHEJ,

I tried to compile the code.  I am on jdk 1.6 ....its not compiling .  not possible to upgrade jdk further....any workaround ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40237321
Yes - you'll need to write your own recursion routines (if you need to recurse)
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40237853
As an alternative, going back to your earlier remark about using other methods than java:

In EditPlus (and other editors) you can use "Find in Files" (under the Search menu) to find a text like this in files in a folder including subfolders (or for example all open files). Also specific to EditPlus, if you use the "More" button in the Find/Replace/Find in Files dialog, just use an enter like this:
<display:column .*
<s:checkbox name=".*" .* value="\$displaytable.*"

Open in new window

The regular expression is similar to java just use 1 backslash. Be careful using more than 1 space as in your original question because it won't match.
capture
0
 

Author Comment

by:cofactor
ID: 40240609
Its not matching in my set up.

Are you putting new line (press Enter)  after <display:column ?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40240635
Yes, either use a normal newline (directly after .* without a space there) after using the "More" button or \n in the normal find field.

I included the screenprint to show it matches up with the text from the question but it may help to show a little more text from a real file to see what might be failing the match.

Also, what I usually do when I don't get a match is to start with a smaller part of the regular expression and keep adding bits until it's clear which part causes a mismatch. So for example only searching for "<display:column" should certainly find something, then extend to "<display:column .*" which should find the same but now until the end of the line and so on.

If that doesn't help maybe you can post a screen capture?
0
 

Author Comment

by:cofactor
ID: 40280685
It works when you search in open files .  but it does not work when text is in file and file in a sub folder.



Follow these steps :

please keep the text in a file in a sub folder
now try searching for the same text from the root directory of the sub folder using your method.
This does not work .

Please let me know if this is working for you.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40280730
Try this

import java.io.*;

import java.util.*;
import java.util.regex.*;


public class FileSearcher {
    private File startDir;
    private String pattern = "(?ms)<display:column.*<s:checkbox name=.*value=\"\\$displaytable";

    public FileSearcher(File startDir) {
        this.startDir = startDir;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public static void main(String[] args) {
        try {
            File startDir = new File(args[0]);
            FileSearcher fs = new FileSearcher(startDir);

            if (args.length > 1) {
                fs.setPattern(args[1]);
            }

            fs.search();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void search() throws IOException {
        processDir(startDir);
    }

    protected void processDir(File dir) throws IOException {
        File[] files = dir.listFiles();

        for (int i = 0; i < files.length; ++i) {
            if (files[i].isDirectory()) {
                processDir(files[i]);
            } else {
                processFile(files[i]);
            }
        }
    }

    protected void processFile(File f) throws IOException {
        String filePath = f.getAbsolutePath();

        if (filePath.matches("(?i).*\\.jsp$")) {
            String fileContent = readFile(f);
            Matcher m = Pattern.compile(this.pattern).matcher(fileContent);

            if (m.find()) {
                System.out.println(f.getAbsolutePath());
            }
        }
    }

    private static String readFile(File file) throws IOException {
        FileReader fr = new FileReader(file);
        StringBuffer buf = new StringBuffer((int) file.length());
        int c = fr.read();

        while (c != -1) {
            buf.append((char) c);
            c = fr.read();
        }

        fr.close();

        return buf.toString();
    }
}

Open in new window

0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40280739
It takes quite a while on a large folder and it doesn't give a progress indication while it's searching, but it does give a summary when it's done. You can speed it up by selecting files like *.jsp only. Please double check the correct options have been chosen. If it still doesn't work it sounds like a bug in the version you're using but this should not really be a problem in any version.

I have included another screenshot to show it works fine for me (using version 3.12 by the way).
new capture
0
 

Author Closing Comment

by:cofactor
ID: 40309502
Thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 49
spring jars download 1 27
by zero exception 10 41
Is there a simpler dropbox system? 10 20
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question