sed

[!NOTE] references:

CHARACTER
REGULAR EXPRESSION
EXPLANATION

[[:alnum:]]

[A-Za-z0-9]

Alphanumeric characters

[[:alpha:]]

[A-Za-z]

Alphabetic characters

[[:blank:]]

[ \t]

Space or tab characters only

[[:cntrl:]]

[\x00-\x1F\x7F]

Control characters

[[:digit:]]

[0-9]

Numeric characters

[[:graph:]]

[!-~]

Printable and visible characters

[[:lower:]]

[a-z]

Lower-case alphabetic characters

[[:print:]]

[ -~]

Printable (non-Control) characters

[[:punct:]]

[!-/:-@[-{-~]`

Punctuation characters

[[:space:]]

[ \t\v\f\n\r]

All whitespace chars

[[:upper:]]

[A-Z]

Upper-case alphabetic characters

[[:xdigit:]]

[0-9a-fA-F]

Hexadecimal digit characters

execute multiple sed commands

[!TIP]

references:

example : show only root and nobody in /etc/passwd

  • -e :

  • ; :

  • '{}' :

range

specific line

  • 2nd line : N <opt>

until empty line

  • or

n~m range

  • n~m lines : n,m <opt>

  • n to end lines : n,$ <opt>

  • m lines starting with n : n, +m <opt>

  • start n skip m via ~ :

    pattern
    matches
    comments

    1~2

    1,3,5,7,...

    start frmo 1, print every 2 lines

    2~2

    2,4,6,8,...

    start from 2, print every 2 lines

    1~3

    1,4,7,10,...

    start from 1, print every 3 lines

    2~3

    2,5,8,11,...

    start from 2, print every 3 lines

pattern matches range

  • between pattern_1 to pattern_2 : /pattern_1/,/pattern_2/ <opt>

  • first line to pattern_2 : 0,/pattern_2/ <opt>

from pattern to first empty line

print

  • print every line twice

  • print all lines : $ sed -n 'p' employee.txt

range print

  • print the 2nd line : $ sed -n '2 p' employee.txt

  • n,m range

    • print 1~4 lines : $ sed -n '1,4 p' employee.txt

    • print all lines since the 2nd line: $ sed -n '2,$ p' employee.txt

  • ~ to skip lines

    • print only odd numbered lines : sed -n '1~2 p' employee.txt

  • + ( n, +m ) : sed -n 'n,+m p' employee.txt

  • find pattern to the end :

  • find pattern and line after the matches line :

  • find pattern to 4th line :

  • find pattern until find another pattern ( Jason to Anand ) :

[!NOTE|label:references:]

delete

delete all

range delete

  • delete the 2nd line : $ sed '2 d' /path/to/file

  • delete between 1 and 4 lines : $ sed '1,4 d' /path/to/file

conditional delete

  • delete all empty lines: $ sed '/^$/ d' /path/to/file

  • delete all comment lines : $ sed '/^#/ d' /path/to/file

substitute

substitute-flags

flag
comments

i

ignore case flag

g

global flag

1,2,...

number flag

p

print flag

w

write flag

e

execute flag

multiple replaces

get matched pattern

&

When & is used in the replacement-string, it replaces it with whatever text matched the original-string or the regular-expression.

substitution grouping

or via -r

cheatsheet

get first matching patten ( for CERTIFICATE )

[!TIP]

  • sample.crt

remove both '#' and empty lines

[!NOTE|label:references:]

  • example

remove tailing spaces

[!TIP|label:available patterns]

  • 's/[ \t]*$//'

  • 's/[[:blank:]]*$//'

  • 's/[[:space:]]*$//'

show top summary

[!NOTE] see sed until empty line

  • contains empty line

  • without empty line

    [!TIP|label:references:]

    manual:

    • The "q" command prints the current line again in less the -n flag was used on the command line and exits the script completely

transform literal string in regex

escape

[!NOTE|label:references:]

  • [\ to \](https://stackoverflow.com/a/67170003/2940319)

tricky

Last updated

Was this helpful?