Solved

search for a specific text  pattern

Posted on 2014-07-27
24
203 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
Comment Utility
comments please
0
 

Author Comment

by:cofactor
Comment Utility
comments please
0
 
LVL 26

Expert Comment

by:dpearson
Comment Utility
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
 

Author Comment

by:cofactor
Comment Utility
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
Comment Utility
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
Comment Utility
>>>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
Comment Utility
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
Comment Utility
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
Comment Utility
If you could attach (with extension renamed to .txt) an actual jsp, i'll see what i can do
0
 

Author Comment

by:cofactor
Comment Utility
>>>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
Comment Utility
http://sourceforge.net/projects/unxutils/

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

Author Comment

by:cofactor
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
>>>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
Comment Utility
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
Comment Utility
@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
Comment Utility
Yes - you'll need to write your own recursion routines (if you need to recurse)
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

743 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now