Sed
Usage
See the manual locally with info sed.
sed OPTIONS INPUTFILE| Option | Description |
|---|---|
-E | Use extended regular expressions |
-e | Add the script to the commands to be executed (for adding multiple commands) |
-i | Edit files in place |
-n | Suppress empty patterns output |
-z | Separate lines by NUL characters |
Commands
Replace: s
Any character other than backslash or newline can be used instead of a slash to delimit the pattern and the replacement.
Within the pattern and the replacement, the pattern delimiter itself can be used as a literal character if it is preceded by a backslash.
sed 's/pattern/replacement/g'
sed 's#pattern#replacement#g' # Using hash instead of slash
sed '0,/pattern/s//replacement/' # Once
sed 's/.*/\u&/' # CapitalizeRemove ANSI escape sequences:
sed 's/\x1b\[[0-9;]*m//g' # Remove color sequences only
sed 's/\x1b\[[0-9;]*[mGKH]//g' # Remove color and move sequences
sed 's/\x1b\[[0-9;]*[a-zA-Z]//g' # Remove all escape sequencesRemove carriage return \M:
sed 's/\r//'Remove trailing newlines:
sed -Ez '$ s/\n+$//'Find and replace all matches recursively in files:
git grep --files-with-matches foo | xargs sed -i 's/foo/boo/g'Finding patterns
Regex option p : If the substitution was made, then print the new pattern space.
sed -nE 's/.*"total":([0-9]+).*/\1/p'
sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' .pylintNote: \d does not work on sed. Use [0-9] instead.
For example, to make a linter fail if it contains more than N errors, run:
echo 'Found 4 errors' | sed -nE 's/^Found ([0-9]+) error.*$/\1/p' | xargs test 4 -geIt is also possible to just print some specific lines, for example from line 10 to 20:
sed -n '10,20p'Change: c
Replaces a line with some text.
sed '1c\#!/data/data/com.termux/files/usr/bin/env fish' script.fish # Changes only the first line
sed '1c\first line\
second line' # Add multiple linesInsert: i
Add text to previous line.
sed '/pattern/i\new text'
sed '0,/pattern/i\new text' # Once
sed '1i\first line\
second line' # Add multiple linesAppend: a
Add text to next line.
sed '/pattern/a\new text'
sed '0,/pattern/a\new text' # Once
sed '1a\first line\
second line' # Add multiple linesDelete: d
Delete line.
sed '/pattern/d'
sed '/start_pattern/,/end_pattern/d' # Delete lines between two patterns (inclusive)
sed '/start_pattern/,/end_pattern/{/end_pattern/!d}' # Delete lines between two patterns (exclude)
sed '/start_pattern/,+1{/start_pattern/!d}' # Delete following line to the matchRead file: r
Append text read from file. Do not escape the / character on the file path.
sed -e '/{{ CONTENTS }}/r /tmp/contents.html' -e '/{{ CONTENTS }}/d'Stop reading input: q
Given that we just want to print one line from a file, one can use this command.
sed '5q;d' fileExplanation:
Commands within a script or script-file can be separated by semicolons
;or newlines. Multiple scripts can be specified with-eor-foptions.
We have two commands: 5q d.
5q: This will only run at the 5th line.sedby default prints the line (-nis not specified). So the 5th line is printed and sed will quit before executingdfor that line.d: This will run on all lines from 1st to 4th. Which means that even thoughsedprints all lines by default, it is also deleting all lines by default. Except for the 5th line, whichqis run and the script stops immediately.