Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Getting Started

sed and AWK Versions and Portability

Know which sed and AWK you have: GNU, BSD, BusyBox, gawk, mawk and one-true-awk, how to check, what differs, and how to write scripts that run everywhere.

"It works on my machine" is a classic problem with shell tools, and sed and AWK are famous for it. A sed -i command that works on Linux fails on macOS. An AWK script that uses gensub() runs on Ubuntu with gawk but fails on a server that has mawk. This lesson shows you which variants exist, how to find out which one you are using, and how to write portable code.

The sed family

Variant Where you find it Notes
GNU sed Linux distributions, Git Bash, gsed on macOS Most features: \L \U, -z, -s, e, I flag, 0,/re/
BSD sed macOS, FreeBSD POSIX plus little; -i needs an argument
BusyBox sed Alpine Linux, containers, routers Small; supports most GNU basics
POSIX sed the standard the portable target

To check: sed --version works on GNU sed. BSD sed prints an error for that flag.

Bash Which sed is this?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

On this site you will see GNU sed. The second command uses a GNU-only escape, \U, to upper-case the match.

The AWK family

Variant Where you find it Notes
gawk most Linux distributions (installed), Homebrew Superset: gensub, strftime, asort, length(array), BEGINFILE, PROCINFO
mawk Debian and Ubuntu default awk, our runner Fast; a few POSIX gaps in old versions
original-awk (bwk awk) macOS, BSDs The "one true awk"
busybox awk Alpine small

To find out: awk --version (gawk), awk -W version (mawk), awk -version (one true awk). Also ls -l $(command -v awk) shows what the name points to.

Bash Which awk is this?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The portable core

POSIX specifies the language that every implementation has. In AWK that means: print, printf, fields and NF/NR/FS/OFS/RS/ORS/FILENAME, arithmetic and string operators, arrays, if/while/for/do, delete, user functions, and built-ins length, substr, index, split, sub, gsub, match, sprintf, sin, cos, atan2, exp, log, sqrt, int, rand, srand, tolower, toupper, system, close, getline.

Not in POSIX AWK (gawk extensions): gensub, strftime, systime, asort, asorti, length(array) (this one is widely supported but formally an extension in old standards), nextfile (common now), BEGINFILE/ENDFILE, IGNORECASE, RS as a regex, \y word boundaries, --re-interval features in old mawk, @include.

In sed, the portable core is: s p d q n N D P g G h H x y a i c = b t : and { }, with options -n -e -f. GNU-only: -i without suffix, -E before POSIX 2024 (now standard), -s, -z, \n in replacement text, \U \L, the I flag, 0~3 steps, e, F, z, T, Q, W, R, v.

The in-place trap

This is the number one portability problem:

TEXT
GNU sed:   sed -i 's/a/b/' file          # edits in place
BSD sed:   sed -i '' 's/a/b/' file       # the suffix argument is mandatory (empty = no backup)

The portable way that works with both is to attach a backup suffix and delete the backup afterwards, or avoid -i and write to a temporary file:

Bash A portable replacement for sed -i

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

-i.bak (suffix directly after -i, no space) is accepted by both GNU and BSD sed, and it keeps a handy backup. The temp-file pattern works everywhere, including BusyBox.

Other differences to watch

  • \n in the replacement: GNU sed turns \n into a newline in s/,/\n/. BSD sed does not; there you must put a real newline after a backslash.
  • \+, \?, \| in BRE are GNU extensions. Use sed -E with ERE for portable code.
  • \t for tab works in GNU sed; BSD sed needs a literal tab. In AWK "\t" works everywhere.
  • sed -n '$=' counts lines everywhere; wc -l also does, but counts newlines.
  • awk '{ print length }' works everywhere; length($0) is the more explicit form.
  • Character classes in awk: avoid [[:digit:]] and if mawk might run your code.
  • Locale: character ranges like [a-z] can behave differently in some locales; LC_ALL=C forces byte-wise, predictable behaviour and speeds up big jobs.
Bash Writing for the lowest common denominator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

A portability checklist

  1. Prefer sed -E over backslash-heavy BRE.
  2. Avoid -i in scripts you share; use a temp file or -i.bak.
  3. In AWK, stick to the POSIX built-ins; reimplement gensub with sub/gsub and strftime with date.
  4. Put #!/usr/bin/awk -f scripts through a second implementation if you can: gawk --posix and mawk catch non-portable code.
  5. Note your assumptions in a comment, for example # requires GNU sed.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command edits a file in place on both GNU and BSD sed?

    A suffix attached right after -i is understood by both. BSD sed requires the suffix argument, and GNU sed treats a separate '' as a script, so only the attached suffix is portable.
  2. 2. Which of these is NOT available in POSIX awk?

    gensub is a gawk extension. POSIX gives you sub and gsub.
  3. 3. How do you write 'one or more' portably in sed?

    \+ is a GNU extension to BRE. -E (extended regex) is standard across modern seds and + is part of ERE.
  4. 4. What does LC_ALL=C do for a sed or awk command?

    The C locale treats text as bytes with a fixed collation order, which avoids locale-dependent ranges and is often faster.

Summary

  • Know your tools: GNU, BSD and BusyBox sed; gawk, mawk and one-true-awk.
  • Check versions with sed --version, awk --version or awk -W version.
  • Stick to POSIX features for portable scripts: sed -E, no bare -i, no gawk-only functions.
  • Mark GNU-only code clearly when you use it.

Next: sed Substitute