search for a specific text pattern

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
cofactorAsked:
Who is Participating?
 
Robert SchuttSoftware EngineerCommented:
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
 
cofactorAuthor Commented:
comments please
0
 
cofactorAuthor Commented:
comments please
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
dpearsonCommented:
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
 
cofactorAuthor Commented:
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
 
dpearsonCommented:
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
 
cofactorAuthor Commented:
>>>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
 
CEHJCommented:
I am in windows. no linux tool please.
Then you'll probably need to use Powershell to enable use of searching with regex
0
 
cofactorAuthor Commented:
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
 
CEHJCommented:
If you could attach (with extension renamed to .txt) an actual jsp, i'll see what i can do
0
 
cofactorAuthor Commented:
>>>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
 
CEHJCommented:
http://sourceforge.net/projects/unxutils/

is what i often use on Windows. That will give you grep
0
 
cofactorAuthor Commented:
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
 
CEHJCommented:
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
 
cofactorAuthor Commented:
>>>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
 
CEHJCommented:
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
 
cofactorAuthor Commented:
@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
 
CEHJCommented:
Yes - you'll need to write your own recursion routines (if you need to recurse)
0
 
Robert SchuttSoftware EngineerCommented:
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
 
cofactorAuthor Commented:
Its not matching in my set up.

Are you putting new line (press Enter)  after <display:column ?
0
 
Robert SchuttSoftware EngineerCommented:
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
 
cofactorAuthor Commented:
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
 
CEHJCommented:
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
 
cofactorAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.