Extract C code from response

 Depending on your needs (speed vs. regex power), here are the three best ways to do this.


### 1. The `awk` Way (Fastest & Most Standard)

This is the best approach if you are processing large files. It works like a "state machine": it turns a flag on when it sees the start tag and off when it sees the end tag.


```bash

awk '/^```c/ {flag=1; next} /^```/ {flag=0} flag'

```


*   **`^```c`**: Matches a line starting with the C code delimiter.

*   **`{flag=1; next}`**: Sets a variable `flag` to 1 and skips to the next line (so we don't print the delimiter itself).

*   **`^````**: Matches a line starting with the closing delimiter.

*   **`{flag=0}`**: Sets the flag to 0 to stop printing.

*   **`flag`**: A shorthand in awk that means "if flag is non-zero, print the current line."


---


### 2. The `perl` Way (Most Robust)

If your code blocks aren't always on their own lines, or if you want to use advanced Regular Expressions, Perl is the best tool. This version reads the entire input into memory at once to allow for multi-line matching.


```bash

perl -0777 -ne 'while(/```c\n(.*?)\n```/sg){print "$1\n"}'

```


*   **`-0777`**: Tells Perl to "slurp" the entire input into one single string instead of processing it line-by-line.

*   **`s` flag**: Allows the dot (`.`) to match newline characters.

*   **`g` flag**: Global search (finds all occurrences, not just the first one).

*   **`.*?`**: A "non-greedy" match. This ensures that if you have two code blocks, it doesn't accidentally match from the start of the first to the end of the last.


---


### 3. The `python` Way (Most Readable)

If you want to include this in a larger, more complex script, Python is the most maintainable.


```python

#!/usr/bin/env python3

import sys

import re


def extract_c_code():

    # Read all input from stdin

    data = sys.stdin.read()

    

    # Regex: Find content between ```c and ```

    # re.DOTALL allows the '.' to match newlines

    pattern = r'```c\n(.*?)\n```'

    matches = re.findall(pattern, data, re.DOTALL)

    

    for block in matches:

        print(block)


if __name__ == "__main__":

    extract_c_code()

```


---


### Summary: Which one should I use?


| Feature | **awk** | **perl** | **python** |

| :--- | :--- | :--- | :--- |

| **Speed** | Extremely Fast | Fast | Moderate |

| **Memory Usage** | Very Low (Streamed) | High (Loads all) | High (Loads all) |

| **Complexity** | Low | Low (One-liner) | Medium |

| **Best for...** | Large files / Simple CLI | Complex Regex / One-liners | Integration into apps |


**Quick Test:**

You can test any of these by running:

```bash

echo -e "Some text\n\`\`\`c\nint x = 10;\n\`\`\`\nMore text\n\`\`\`c\nprintf(\"hi\");\n\`\`\`" | awk '/^```c/ {flag=1; next} /^```/ {flag=0} flag'

```

Popular posts from this blog

Dearpygui resizable table

Fixing DNS Resolution problems with Linux Mint 19

Create a new repo