sed

source (C) https://github.com/mirror/sed
home https://www.gnu.org/software/sed/
manual https://www.gnu.org/software/sed/manual/sed.html
wiki https://en.wikibooks.org/wiki/Sed
wiki https://en.wikipedia.org/wiki/Sed

language

#!/bin/sed -f
s/Hello/Hell/g
  • "Stream EDitor"
  • Author: Lee E. McMahon
  • 1974-
  • line-oriented text processing utility
  • reads text, line by line, into a temporary buffer called pattern space
  • each line read starts a cycle
  • there is a special buffer called hold space used by some commands to hold and accumulate text between cycles
  • https://en.wikipedia.org/wiki/Leaning_toothpick_syndrome aka when a quoted expresion becomes unreadable because it contains a large number of escape characters
  • can separate commands with either
    • cmd1;cmd2
    • -e cmd2 -e cmd2
  • [address]command address { cmd1; cmd2; } # cannot have a space before ";"

addresses

  • If
    • NO address, command is applied to each line
    • has address, applies to the line matching
      • address is followed by a (!), inverse the address match
  • Can be
    • 1,2 where 1=startline and 2=endline a comma separated tuple, command is applied from and upto including the matched line "You can think of the 1st address as enabling the action and the second address as disabling it."
    • 1~2 where 1=startline and 2=step
    • 1,+2 where 1=startline and +2=distance/length
    • aregex
      • also supports arbitrary characters as delimitators, with a backslash for the left one
      • \:aregex:
    • NUMBER of line (not reset by different files)
    • $ represents the last line
    • {} to group different commands to the same OR different address
    • line addressing symbol
    • can mix up line number and regex

commands

  • Might take an additional (1 or 2) address
  • 25 commands
<r> name description
q quit  
d delete the pattern space
D delete only the first line from the pattern space, and run again
n next reads the next line into the pattern space, and prints previous value there
N next appends the next line into the pattern space, in a new line
p print the pattern space
P print the first line on the pattern space
=   prints the current line number plus a new line
y   srcstr/dststr (tr)ansliterates src by dst
l   list the pattern space unambigously, nice for debugging
r reads from provided filename
w writes to provided filename
t   jump to given tag if previous substitution succeded
T   jump to given tag if previous substitution failed
[line]a   appends, after current pattern space
[line]i   inserts, before current pattern space
[addr]c   change, replaces current pattern space with given argument
s   /pattern/replacement/flags
    uses address for pattern if missing
    replacement can be ("&" pattern matched, or \N where N is the N-match group)
    [w FILE,g,p,I,NUMBEROFMATCH]
    hk
h hold copy pattern -> hold
H hold append \npattern -> hold
g get copy hold -> pattern
G get append \nhold -> pattern
x exchange aka swap content between hold space and pattern space

script

  • a series of operations/actions
  • the "body of a loop" that iterates through lines
  • each line of a script is a pattern-action pair aka a conditional statement

operations

  • of a command
  • applied over the pattern space
  • by default, after run, sed output the pattern space
  • and begins the cycle again with the next line

flags

-e script inline script
-f script-file path to script-file
-n only output via the p command
-i inplace edit
-i.bkp inplace edit, saves old version with the .bkp extension

snippets

https://sed.sourceforge.io/#scripts

  flag description
p   prints every line twice
p -n prints every line
1p -n prints first line
$p -n prints last line
1,3p -n prints lines 1 through 3
20,$p -n prints from line 20 to end
#,$p -n remove before comment (#)
=;n   interleaves printing line number, every 2 numbers (1,3..) and each line
$= -n prints the number of lines (slow?)
$a 8.8.8.8 google.com -i /etc/hosts appends ip/hostname at the end
1i #Managed by sed -i /etc/hosts inserts before line 1, a comment
$d -i /etc/hosts deletes last line
2d   delete line 2
1,10d   remove the first 10 lines
/^ /d   filters out lines starting with space
/^ *$/d   filters out lines containing only spaces
50,$d   deletes from line 50 to the end
/needle/d   deletes lines containing "needle"
1,/^$/d   deletes from 1st line to the first blank line
/^(#¦$)/d -E remove comments and empty lines
/^#/d;/^$/d   remove comments and empty lines
/^\s*(#¦$)/d -E remove comments, indentend comments, and empty lines
---!s/–/\\(em/g   on all lines that do not have 3(-), replace 2(-)
s/.*/Hello/;q   reads 1st line of input and prints "Hello"
s/needle//g   deletes "needle" from lines
s/.$//   dos2unix, aka CRLF to LF
/ant/s/needle//g   delete needle on lines containing "ant"

codebases


Created: 2023-09-04

Updated: 2024-11-16

Back