Last active
May 2, 2022 05:58
-
-
Save SaifAqqad/b335ab2a0c2a01188422c8d3a1eaa2b3 to your computer and use it in GitHub Desktop.
Mustafa's parse script
This file contains 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
package main; | |
import java.io.BufferedWriter; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.StandardOpenOption; | |
import java.util.Arrays; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class Main { | |
private static final Pattern usernamePattern = Pattern.compile("Username:\\s+(.+)"); | |
private static final Pattern passwordPattern = Pattern.compile("Password:\\s+(.+)"); | |
private static final String splitPattern = "==============="; | |
private static final Path outputFilePath = Path.of("./output.txt"); | |
public static void main(String[] args) throws IOException { | |
if (args.length < 1) { | |
System.out.println("Usage: java -jar <jar-file> <file-to-parse>"); | |
System.exit(1); | |
} | |
Path filePath = Path.of(args[0]); | |
if (!Files.isReadable(filePath)) { | |
throw new IllegalArgumentException("File not found or not readable"); | |
} | |
String fileContent = Files.readString(filePath, StandardCharsets.UTF_8); | |
Files.deleteIfExists(outputFilePath); | |
BufferedWriter outputFile = Files.newBufferedWriter(outputFilePath, StandardOpenOption.CREATE,StandardOpenOption.APPEND); | |
Arrays.stream(fileContent.split(splitPattern)) | |
.forEach(chunk -> { | |
Matcher usernameMatcher = usernamePattern.matcher(chunk); | |
Matcher passwordMatcher = passwordPattern.matcher(chunk); | |
if (usernameMatcher.find() && passwordMatcher.find()) { | |
String username = usernameMatcher.group(1); | |
String password = passwordMatcher.group(1); | |
try { | |
outputFile.append(username + ":" + password + "\n"); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment