Last active
July 2, 2024 12:12
-
-
Save kanikash4/2ea98be94701b6ff89c000ea0fae7d32 to your computer and use it in GitHub Desktop.
This script will help to find the keywords in the java projects with filename and line number for all the extension files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Checkout repo <repo_name> | |
2. cd <repo_name> (go to repo folder) | |
3. put scanner.sh and ReadFileLineByUsingBufferedReader.java in folder | |
4. Open scanner.sh and change/add keyword if required | |
5. compile ReadFileLineByUsingBufferedReader | |
javac ReadFileLineByUsingBufferedReader.java | |
6. Run scanner.sh script | |
sh scanner.sh > scanning_result.txt | |
7. Print Beautify result using below command scanning_result.txt | |
java ReadFileLineByUsingBufferedReader ./scanningresult.txt ./scanner.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.util.*; | |
import java.lang.reflect.Array; | |
import java.io.IOException; | |
public class ReadFileLineByUsingBufferedReader { | |
static String keywordsFile[] = {"java", "xml", "conf", "config", "yaml", "yml", "properties", "prop"}; | |
static class LineData { | |
String str; | |
String lineNo; | |
String line; | |
public LineData(String str, String lineNo, String line) { | |
this.str = str; | |
this.lineNo = lineNo; | |
this.line = line; | |
} | |
} | |
static class Data { | |
List<LineData> list; | |
String fileName; | |
public Data(String fileName, String line, String lineNo, String str) { | |
this.list = new ArrayList<>(); | |
list.add(new LineData(lineNo, str, line)); | |
this.fileName = fileName; | |
} | |
public void addLine(String fileName, String line, String lineNo, String str) { | |
list.add(new LineData(lineNo, str, line)); | |
} | |
} | |
static Map<String, Data> map = new TreeMap<>(); | |
public static List<String> set1 = Arrays.asList(keywordsFile); | |
public static List<String> getFileAsList(String filename) { | |
BufferedReader reader; | |
List<String> list = new ArrayList<>(); | |
try { | |
reader = new BufferedReader(new FileReader(filename)); | |
String line = reader.readLine(); | |
while(line!= null) { | |
list.add(line); | |
line = reader.readLine(); | |
} | |
reader.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return list; | |
} | |
public static void main(String[] args) { | |
BufferedReader reader; | |
String fileName = ""; | |
fileName = args[0]; | |
List<String> list = getFileAsList(fileName); | |
try { | |
for(String line : list) | |
splitTheLine(line); | |
} catch(Exception ex) { | |
ex.printStackTrace(); | |
} | |
printSummary(args[1]); | |
printMap(); | |
printDetailMap(); | |
} | |
public static void printMap() { | |
System.out.println(""); | |
System.out.println(""); | |
System.out.println(""); | |
System.out.println("######################### PRINTING DETAIL SUMMARY #########################\n\n"); | |
for(Map.Entry<String, Data> M: map.entrySet()) { | |
int matchedCount = M.getValue().list.size(); | |
System.out.println("FileName [" + M.getKey() +"] matchedCount[" + matchedCount +"]"); | |
} | |
System.out.println("####################### PRINTING DETAIL SUMMARY END #######################\n\n"); | |
} | |
public static void printSummary(String fileName2) { | |
System.out.println(""); | |
System.out.println(""); | |
System.out.println(""); | |
System.out.println("######################### PRINTING SUMMARY #########################\n\n"); | |
System.out.println("File extensions searched " + Arrays.asList(keywordsFile)); | |
List<String> list = getFileAsList(fileName2); | |
int x = 0; | |
for(String line : list) { | |
System.out.println(line); | |
x++; | |
if(x >= 4) break; | |
} | |
Map<String, Integer> summaryMap = new HashMap<>(); | |
for(Map.Entry<String, Data> M : map.entrySet()) { | |
String fileName = getFileExtension(M.getKey()); | |
if(summaryMap.containsKey(fileName)) { | |
summaryMap.put(fileName, summaryMap.get(fileName) +1); | |
} else { | |
summaryMap.put(fileName, 1); | |
} | |
} | |
for(Map.Entry<String, Integer> M : summaryMap.entrySet()) { | |
System.out.println("Total " + M.getKey() + " file count " + M.getValue()); | |
} | |
System.out.println("##################### PRINTING SUMMARY END #####################\n\n"); | |
} | |
public static String getFileExtension(String fileName) { | |
int dotIndex = fileName.lastIndexOf("."); | |
if(dotIndex != -1 && dotIndex < fileName.length()-1) { | |
return fileName.substring(dotIndex+1); | |
} | |
return "NULL"; | |
} | |
public static void printDetailMap() { | |
System.out.println(""); | |
System.out.println(""); | |
System.out.println(""); | |
System.out.println("##################### PRINTING DETAILS #####################\n\n"); | |
for(Map.Entry<String, Data> M : map.entrySet()) { | |
printDetails(M.getValue()); | |
} | |
System.out.println("################### PRINTING DETAILS END ###################\n\n"); | |
} | |
private static void printDetails(Data data) { | |
System.out.println("\n\n\n Details of file is "); | |
System.out.println("FileName " + data.fileName); | |
for(LineData lineData : data.list) { | |
System.out.println("found ["+ lineData.str + "] on line [" + lineData.lineNo + "]"); | |
} | |
} | |
public static void splitTheLine(String line) { | |
boolean isFound = false; | |
if(line.indexOf("finding")!= -1) { | |
return; | |
} | |
for(String keyword1 : set1 ) { | |
if(line.indexOf(keyword1) != -1) { | |
isFound = true; | |
break; | |
} | |
} | |
if(isFound) { | |
String lineArray[] = line.split("\\:", 3); | |
for(String linePart : lineArray) { | |
// System.out.println(linePart); | |
} | |
String key = lineArray[0]; | |
if(map.containsKey(key)) { | |
map.get(key).addLine(key, line, lineArray[1], lineArray[2]); | |
} else { | |
Data data = new Data(key, line, lineArray[1], lineArray[2]); | |
map.put(key, data); | |
} | |
} else { | |
System.out.println("NO Keyword Line " + line); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
keywords=("todo" "log.debug") | |
extensions=("*.xml" "*.java" "*.yml" "*.yaml" "*.prop" "*.properties" "*.conf" "*.config") | |
for ext in "${extensions[@]}"; do | |
for keyword in "${keywords[@]}"; do | |
echo "finding $ext $keyword" | |
find . -name "$ext" -exec grep -Hn "$$keyword" {} + | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment