bash · grep · sed · awk · local

Generate Bash / grep / sed one-liners from a regex

Drop a pattern in, pick flags, and get a Unix shell snippet — grep -E / -P matching, sed substitution, awk splitting, and Bash's built-in =~ operator — with proper single-quote escaping.

Regex → Bash
//g
grep -P (PCRE) is only available on GNU grep. macOS users may need ggrep (brew install grep) or fall back to -E with POSIX ERE syntax.
Bash / grep / sed / awk
#!/usr/bin/env bash
input='Reach alice@example.com or bob@test.org.'
pattern='\\b\\w+@\\w+\\.\\w+\\b'

# Match (POSIX ERE via grep)
if echo "$input" | grep -E '\\b\\w+@\\w+\\.\\w+\\b' > /dev/null; then
  echo "matched"
fi

# First match only (PCRE — requires GNU grep with -P)
first=$(echo "$input" | grep -Po '\\b\\w+@\\w+\\.\\w+\\b' | head -n1)
echo "first=$first"

# All matches, one per line
echo "$input" | grep -Po '\\b\\w+@\\w+\\.\\w+\\b'

# Replace with sed (ERE)
replaced=$(echo "$input" | sed -E 's/\\b\\w+@\\w+\\.\\w+\\b/[email]/g')
echo "replaced=$replaced"

# Split (treat pattern as the separator, output one field per line)
echo "$input" | awk -v RS='\\b\\w+@\\w+\\.\\w+\\b' '{ print }'

# Bash built-in =~ (POSIX ERE, no flags)
if [[ "$input" =~ \\b\\w+@\\w+\\.\\w+\\b ]]; then
  echo "bash match: ${BASH_REMATCH[0]}"
fi
Quick Start

How to use a regex in Bash

Unix has three regex engines: grep -E (POSIX ERE), grep -P (PCRE, GNU-only), and Bash's built-in =~. The snippet shows all three.

  1. 01

    Type a pattern

    Pattern is single-quoted to keep $ and ` literal. Backslashes pass through to the regex engine unchanged.

    Patterns are parsed with the JavaScript RegExp constructor, so syntax errors surface immediately and engine quirks like Unicode property escapes (\p{L}) require the u flag.

    patternECMAScriptliteralregex
    input · literalpattern · regex
    '\b\w+@\w+\b'
    loaded · ECMAScripttarget · ready
  2. 02

    Tune flags

    Only the i flag maps cleanly — into grep -i, sed I modifier. Multiline behaviour is implicit (grep processes one line at a time).

    Each flag character mirrors the ECMAScript literal: g enables iteration, i folds case, m anchors per line, s lets . match newlines, u unlocks proper Unicode, y is sticky, and d emits indices.

    flagssampleg · i · mlive
    config · optionsflags · flags
    -iE / -iPo
    flags applied · livelive · preview
  3. 03

    Read the snippet

    Includes grep -E (POSIX), grep -Po (PCRE), sed substitution, awk splitting, and a Bash [[ =~ ]] block — pick whichever fits your environment.

    The generator rewrites the regex for the target language — handling string-literal escaping, raw-string prefixes, and engine-specific syntax tweaks.

    codegentargetsnippetcopy
    run · outputpreview · code
    grep -E '…' file
    snippet · livecode · ready
  4. 04

    Copy code

    One click copies the full script — paste into a .sh file or a terminal. chmod +x and go.

    Copy writes via navigator.clipboard.writeText, so nothing transits the network and the output is byte-identical to what you see on screen.

    copyexportclipboardblob
    export · exportedready · saved
    Copied
    ready to shipsuccess
Pro tips

Regex patterns that don't melt down

Use named groups

(?<year>\d{4}) reads better than $1 and survives reordering. Supported in ECMAScript 2018+.

Anchor strictly

Use ^/$ with the m flag when matching whole lines. Without anchors, partial matches sneak through.

Avoid catastrophic backtracking

Nested quantifiers like (a+)+ can explode. Run patterns through ReDoS Detector first.

Escape every literal

Backslash any meta character you want literally: \.\$\^\*\+\?\(\)\[\]\{\}\|\\.

Test edge cases

Empty string, single character, unicode, the maximum length you expect — write fixtures for each.

Patterns aren&rsquo;t parsers

Don’t parse HTML, JSON, or balanced parens with regex. Use a proper parser when the grammar is recursive.

Use Cases

When to generate Bash regex code

Skip the boilerplate when wiring a pattern into a CI step, a deploy script, or a sysadmin one-liner.

Log filtering

Pipe tail -f into grep to watch only matching lines. Compiles through the JavaScript RegExp constructor — engine quirks (Unicode property escapes, sticky matching, named groups) work when their flags are on. Error messages cite the exact position of any syntax issue.

ReDoS-safe

Batch rename

Use sed in a for loop to rename files by pattern. Pairs with the ReDoS detector to catch catastrophic backtracking before you deploy. A pattern that handles 1k-character input fine can become a denial-of-service vector on attacker-controlled 1M-character input.

ECMA-compliant

CI assertions

Fail a build step if grep finds a forbidden string in output. Output exports as JavaScript, Python re, Java util.regex, .NET System.Text.RegularExpressions, PCRE, and POSIX — language quirks are highlighted on conversion so you don't ship a JS-only pattern to a Go service.

spec-grade

Config validation

Validate config-file values match a pattern before reloading services. Capture groups (named or numbered) round-trip cleanly into replacement strings, JSON output, or CSV extraction without re-typing. Backreferences ($1, ${name}) preserve semantics across languages.

cross-engine

Log rotation

Find old log files matching a date pattern and archive them. Test against edge cases — empty input, single character, max length, multibyte boundaries — to catch off-by-one and zero-length match bugs before they reach production.

test-ready

Quick analytics

awk + regex to bucket events from raw access logs. All matching runs client-side so secret patterns never leave the browser. Useful when the pattern itself is sensitive (PII regexes, internal data formats, security filters).

production-ready
FAQ

Frequently asked questions

grep -E for POSIX ERE (portable). grep -P for full PCRE (lookarounds, lazy quantifiers — GNU only). grep alone is BRE — older, requires backslashes for groups/alternation. The snippet uses -E by default, with -P alongside for richer features.
Bash's =~ uses POSIX ERE but doesn't accept flags. It captures into the BASH_REMATCH array. Good for inline string checks, less convenient when you need /g-style "all matches".
Without -E, sed uses BRE — () and {} need backslashes. -E gives you ERE — the same syntax as grep -E. BSD sed (macOS) uses -E too, so this is portable.
Standard sed doesn't support PCRE. Use perl -pe 's/pattern/repl/g' as a drop-in replacement when you need full PCRE features.
No. The code is generated entirely in your browser.
Suggest a tool

Something missing?

Tell us what you need. We ship the top requests every two weeks.

Sent straight to the maintainers · no account needed
47 in queue·8 shipped this month
Request queued. Thanks!