如何读取java中特定行号上方和下方的行?

发布于 2024-12-29 10:50:37 字数 132 浏览 2 评论 0原文

我需要解析 txt 格式的日志文件。

我需要匹配字符串并读取匹配字符串的上方和下方行。

我需要执行类似于 grep -A 30 -B 50 'hello' 的操作。

如果您有任何其他建议,欢迎您这样做。

I need to parse a log file which is in txt format.

I need to match the String and read above and below lines of the matched String.

I need to do something similar to grep -A 30 -B 50 'hello'.

If You have any other suggestions to do it you are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阿楠 2025-01-05 10:50:37

逐行读取文件,并匹配您的字符串(正则表达式或 String.indexOf(""))。将前 n 行保留在内存中,以便在匹配字符串时可以打印它们。使用 BuffereReader.readLine( ) 逐行读取文件(请注意,使用 BufferedReader 实际上更复杂,因为无法跳回)。
或者为了获得更大的灵活性RandomAccessFile。使用此方法,您可以标记您的位置,打印接下来的 m 行,然后跳回从上次离开的位置继续搜索。

Read the file line by line, and match your string (either regexp or String.indexOf("")). Keep the previous n lines in memory so when you've matched your string you can print them. Use BuffereReader.readLine() to read the file line by line (note that using the BufferedReader is actually more complicated because you cannot jump back).
Or for more flexibility RandomAccessFile. With this method you can mark your position, print the next m lines and then jump back to continue your search from where you left it.

羁绊已千年 2025-01-05 10:50:37

伪代码:

initialize a Queue
for each line
    if line matches regex
        read/show lines from queue;
        read/show next lines;
    else {
        if queue size > 30
             queue.remove() // removes head of the queue
        add this line to queue;
    }

您可以使用 BufferedReader 逐行读取文件,Pattern 根据正则表达式检查该行,以及 队列 存储之前的行。

pseudocode:

initialize a Queue
for each line
    if line matches regex
        read/show lines from queue;
        read/show next lines;
    else {
        if queue size > 30
             queue.remove() // removes head of the queue
        add this line to queue;
    }

You can use BufferedReader to read a file line by line, Pattern to check the line against a regular expression, and Queue to store previous lines.

谁的新欢旧爱 2025-01-05 10:50:37

您可以使用以下代码(使用 java 5 api java.util.Scanner):

    Scanner scanner = new Scanner(new File("YourFilePath"));
    String prev = null;
    String current;
    while (scanner.hasNextLine())
    {
        current = scanner.nextLine();
        if (current.contains("YourRegEx"))
            break;
        else
            prev = current;
    }
    String next = scanner.nextLine();

您可能需要添加额外的检查来检查 prev 不为 null 并调用 scanner.hasNextLine()String next =scanner.nextLine()之前

You may use following code(uses java 5 api java.util.Scanner):

    Scanner scanner = new Scanner(new File("YourFilePath"));
    String prev = null;
    String current;
    while (scanner.hasNextLine())
    {
        current = scanner.nextLine();
        if (current.contains("YourRegEx"))
            break;
        else
            prev = current;
    }
    String next = scanner.nextLine();

You may want to add additional checks for prev being not null and Calling scanner.hasNextLine() before String next = scanner.nextLine()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文