Posted on
Artificial Intelligence

AI-Assisted Text Processing

Author
  • User
    linuxbash
    Posts by this author
    Posts by this author

AI-Assisted Text Processing on the Linux Command Line (Bash)

If you’ve ever stared at a 200 MB log file hunting for “the important parts,” or copy-pasted ad‑hoc text into a temporary script just to convert it into JSON, you know the feeling: there must be a faster way. Today, there is. With AI-assisted text processing, you can keep your hands in Bash, pipe text straight from your usual tools, and get smart summaries, classifications, structured conversions, or draft one-liners—without leaving your terminal.

This article shows you how to pair Bash with AI responsibly and productively. You’ll see why it’s worth doing, what to install, and 3–5 practical recipes you can use right now.


Why bring AI into Bash?

  • Speed and focus: Large Language Models (LLMs) can summarize noisy text (logs, diffs, tickets) in seconds, spotlighting the bits you actually need to act on.

  • Structure on demand: Turn unstructured text into JSON or CSV so your existing Unix pipeline can consume it.

  • Drafting power: Ask for sed/awk/grep suggestions to accelerate tedious transformations—and then test safely.

  • Local or cloud: Run models locally for privacy-sensitive data, or use cloud APIs for stronger quality and tooling.

  • Complements, not replaces: You’ll still use grep/sed/awk/jq. AI just helps when rules break down or the text is too diverse for deterministic regex alone.


Installation and setup

We’ll use:

  • curl and jq for HTTP and JSON handling

  • pipx + the “llm” CLI (a flexible model-agnostic LLM client)

  • optional: a local model runtime (Ollama) if you prefer to keep data on your machine

Install prerequisites with your distro’s package manager.

1) curl and jq

  • Debian/Ubuntu (apt):

    sudo apt update
    sudo apt install -y curl jq
    
  • Fedora/RHEL (dnf):

    sudo dnf install -y curl jq
    
  • openSUSE (zypper):

    sudo zypper refresh
    sudo zypper install -y curl jq
    

2) pipx (to install Python CLIs in isolated environments)

  • Debian/Ubuntu (apt):

    sudo apt update
    sudo apt install -y pipx
    pipx ensurepath
    exec $SHELL
    
  • Fedora/RHEL (dnf):

    sudo dnf install -y pipx
    pipx ensurepath
    exec $SHELL
    
  • openSUSE (zypper):

    sudo zypper refresh
    sudo zypper install -y pipx
    pipx ensurepath
    exec $SHELL
    

3) Install the “llm” CLI and provider plugins

pipx install llm
pipx inject llm llm-openai
pipx inject llm llm-ollama

If you’ll use a cloud provider, set your API key (example shows an OpenAI-compatible variable; adapt to your provider if different):

export OPENAI_API_KEY="your_api_key_here"

4) Optional: Install a local runtime (Ollama) for on-device models

curl -fsSL https://ollama.com/install.sh | sh
# example small model:
ollama pull llama3.2:3b

Tip: With the llm CLI, you can use:

  • Cloud: llm -m gpt-4o-mini "Your prompt"

  • Local (via Ollama): llm -m ollama/llama3.2:3b "Your prompt"

For stdin, pass - as the prompt to read from a pipe.


1) Summarize noisy logs in seconds

When you’re buried in logs, ask AI for a structured, actionable summary. Use stdin so you can keep your normal filters (grep, awk, journalctl).

Example (cloud model):

journalctl -u myservice --since "1 hour ago" -o short-iso \
| llm -m gpt-4o-mini \
     --system "You are a precise SRE assistant. Summarize errors and anomalies with counts, modules, and suspected causes. End with 3 prioritized actions." \
     -

Local-only alternative (Ollama):

journalctl -u myservice --since "1 hour ago" -o short-iso \
| llm -m ollama/llama3.2:3b \
     --system "You are a precise SRE assistant. Summarize errors and anomalies with counts, modules, and suspected causes. End with 3 prioritized actions." \
     -

Pro tip:

  • Pre-trim with grep/awk to cut volume and cost: journalctl -u myservice --since "1 hour ago" -o short-iso \ | grep -E "ERROR|WARN|FATAL" \ | tail -n 2000 \ | llm -m gpt-4o-mini --system "..." -

2) Classify lines (e.g., severity or category) in bulk

You can batch a list of lines and ask the model to output machine-parseable mappings. Keep the output strict so your pipeline can trust it.

Example: Map log lines to one of {info, warn, error, debug}. We number the lines, ask for CSV back, and join the result.

# Create a numbered block for the model
head -n 200 app.log \
| nl -ba -w1 -s $'\t' \
| awk -F'\t' '{print $1": "$2}' \
| llm -m gpt-4o-mini \
     --system "Classify each numbered line into one of: info|warn|error|debug. Output ONLY CSV lines: line_number,label. No extra text." \
     - \
| tee classifications.csv

Now you’ve got classifications.csv like:

12,error
13,info
14,warn
...

You can join this back to the original lines:

paste -d',' <(seq 1 200) <(head -n 200 app.log) \
| join -t',' -1 1 -2 1 - <(sort -t',' -k1,1 classifications.csv) \
| awk -F',' '{print $3": "$2}'

Notes:

  • For larger volumes, chunk input (e.g., 200 lines at a time) to control latency/cost.

  • Set temperature low (default is low in the llm CLI) to keep outputs consistent.


3) Turn free text into reliable JSON

Ask the model to emit strict JSON, validate with jq, and your downstream tools will thank you.

Example: Convert mixed lines like “[WARN] 2026-07-07T12:03Z cache miss user=42” into structured JSON.

tail -n 1000 app.log \
| llm -m gpt-4o-mini \
     --system "You convert log lines into JSON. For each input line, output ONE compact JSON object on its own line with fields: level, timestamp, message, and kv map for any key=value pairs. Output ONLY JSON lines. No explanations." \
     - \
| jq -c . \
| tee logs.jsonl

Now you can:

# Count by level
jq -r '.level' logs.jsonl | sort | uniq -c | sort -nr

# Filter errors since a timestamp
jq -c 'select(.level=="ERROR" and .timestamp >= "2026-07-07T12:00:00Z")' logs.jsonl

Local-only alternative:

tail -n 1000 app.log \
| llm -m ollama/llama3.2:3b --system "..." - \
| jq -c . > logs.jsonl

Tips:

  • Always validate with jq -c . to fail fast if the model emits invalid JSON.

  • Keep prompts short and strict: “Output ONLY JSON lines” reduces chatter.


4) Draft sed/awk one‑liners—safely

AI can quickly propose transformations. You should still gate and review the result before executing.

A helper that asks the model for a single sed command, prints it, and only runs if you confirm:

ai_sed() {
  local spec="$1"
  local sample="${2:-}"
  # Ask for a single sed command only
  cmd=$(
    printf '%s' "$sample" \
    | llm -m gpt-4o-mini \
         --system "You are a shell expert. Output ONLY a single portable sed command that performs the requested transformation. No explanations, no backticks, no comments." \
         "Write a sed command to: $spec
Sample input:
---8<---
$sample
---8<---"
  )
  echo "Proposed command:"
  echo "$cmd"
  printf "Run it? [y/N] "
  read -r ans
  if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then
    eval "$cmd"
  fi
}

Usage:

# Example: redact emails in a file (confirm before applying)
ai_sed "redact email addresses by replacing user@host with [REDACTED]" "$(head -n 20 users.txt)"

Safety tips:

  • Never pipe AI output straight to sh without review.

  • Request “ONLY a single command” to avoid multi-line surprises.

  • Test on samples; commit before bulk transforms.


No extra CLI? Pure Bash + curl + jq (OpenAI‑compatible API)

If you’d rather not install the llm CLI, here’s a small Bash function that hits a Chat Completions endpoint compatible with many providers.

ai() {
  : "${OPENAI_API_KEY:?Set OPENAI_API_KEY}"
  local model="${MODEL:-gpt-4o-mini}"
  local base="${OPENAI_API_BASE:-https://api.openai.com/v1}"
  local prompt
  if [ -t 0 ]; then
    # from argument
    prompt="$*"
  else
    # from stdin
    prompt="$(cat)"
  fi

  curl -sS "${base}/chat/completions" \
    -H "Authorization: Bearer ${OPENAI_API_KEY}" \
    -H "Content-Type: application/json" \
    -d "$(jq -n --arg m "$model" --arg p "$prompt" \
          '{model:$m, messages:[{role:"user", content:$p}], temperature:0.2, max_tokens:800}')" \
  | jq -r '.choices[0].message.content'
}

Examples:

# Summarize a log chunk
tail -n 500 app.log | ai "Summarize errors with counts and likely causes."

# Turn a paragraph into JSON
printf 'user=42 action=login status=ok\n' \
| ai "Return a JSON object with fields {user:int, action, status}"

Note: Replace MODEL and OPENAI_API_BASE for your provider as needed. Mind data sensitivity and your organization’s policies.


Best practices

  • Privacy first: For sensitive data, prefer local models (Ollama) or on-prem endpoints. If you must use cloud, redact secrets first (e.g., with sed) and review your provider’s data retention policy.

  • Constrain outputs: Ask for “ONLY JSON”, “ONLY CSV”, or “ONE sed command” to keep outputs machine-friendly.

  • Validate: Pipe JSON through jq, and never run code blindly.

  • Chunk wisely: Large inputs increase latency and cost. Pre-filter with grep/awk and process in manageable batches.

  • Version prompts: Save prompts in your repo alongside scripts so results are reproducible and improvable.


Conclusion and next steps

AI-assisted text processing lets you keep your Bash muscle memory while getting on-demand structure, summaries, and draft transformations. Start with one pain point today:

  • If privacy is a must: install Ollama and try the log summarization recipe locally.

  • If you want the quickest path: set an API key and use the llm CLI for summaries and JSON conversion.

  • Bake it in: wrap your favorite prompts as small shell functions and share them with your team.

Your terminal just got a smarter coworker—put it to work on the next messy text you meet.