You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

文本处理:如何逆序提取Pattern A至首个Pattern B匹配区间行

Text Processing Solutions for Your Two Scenarios

Let's break down practical solutions for both of your text extraction tasks using standard Unix tools (awk, tac) — they're widely available and ideal for these jobs.

Problem 1: Extract from Pattern A to the First Pattern B

If you need to grab all lines starting from the first match of Pattern A up to (and including) the first subsequent Pattern B, here's a straightforward awk one-liner:

awk '/Pattern A/{flag=1} flag; /Pattern B/{flag=0}' example_file.txt

How this works:

  • When awk hits a line matching Pattern A, it sets a flag to 1 (active).
  • The flag; line tells awk to print the current line only if the flag is on.
  • Once we encounter a line with Pattern B, the flag is turned off, stopping further printing.

Edge Case Adjustments:

  • No Pattern B after A: This will print everything from Pattern A to the end of the file.
  • Only want the first occurrence: Add exit after turning off the flag to stop processing once the first Pattern B is found:
    awk '/Pattern A/{flag=1} flag; /Pattern B/{flag=0; exit}' example_file.txt
    

Problem 2: Bottom-Up Interval Extraction (AK5*R to AK2)

For this task, we need to scan from the end of the file upwards, find every line matching AK5*R, and extract the lines from that line up to the first preceding AK2. We'll name these intervals E1 (last occurrence), E2 (second-last), etc.

Full Step-by-Step Solution:

  1. First, reverse the file so we can process from bottom to top using tac:

    tac example_file.txt > reversed_file.txt
    
  2. Use awk to split the reversed file into target blocks and save them to numbered files:

    awk '
    /AK5\*R/ {
        if (block) { print block > "E" cnt; cnt++ }
        block = $0 "\n"
        next
    }
    /AK2/ {
        block = block $0 "\n"
        print block > "E" cnt
        cnt++
        block = ""
        next
    }
    { if (block) block = block $0 "\n" }
    END { if (block) print block > "E" cnt }
    ' reversed_file.txt
    
  3. Reverse each E file to restore the original line order:

    for file in E*; do tac "$file" > "$file.tmp" && mv "$file.tmp" "$file"; done
    

How this works:

  • tac reverses the input file, so the last line of the original becomes the first line we process.
  • The awk script collects lines into a block when it hits AK5*R. It keeps adding lines until it finds AK2, then saves the block to a numbered E file. If there's an unfinished block at the end (no AK2 after the last AK5*R), it saves that too.
  • Finally, we reverse each E file to get the lines back in their original order (since we processed reversed input).

Result:

  • E1 contains the last occurrence of AK5*R in the original file, along with all lines up to the first AK2 above it.
  • E2 contains the second-last AK5*R interval, and so on.

内容的提问来源于stack exchange,提问作者WashichawbachaW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:31:31