sed
[!NOTE] references:
[[: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
/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
~:patternmatchescomments1~21,3,5,7,...start frmo
1, print every2lines2~22,4,6,8,...start from
2, print every2lines1~31,4,7,10,...start from
1, print every3lines2~32,5,8,11,...start from
2, print every3lines
pattern matches range
between
pattern_1topattern_2:/pattern_1/,/pattern_2/ <opt>first line to
pattern_2:0,/pattern_2/ <opt>
from pattern to first empty line
print
print all lines
print every line twice
print all lines :
$ sed -n 'p' employee.txt
range print
print the 2nd line :
$ sed -n '2 p' employee.txtn,mrangeprint 1~4 lines :
$ sed -n '1,4 p' employee.txtprint all lines since the 2nd line:
$ sed -n '2,$ p' employee.txt
~to skip linesprint only odd numbered lines :
sed -n '1~2 p' employee.txt
+(n, +m) :sed -n 'n,+m p' employee.txt
print matched pattern
find pattern to the end :
find pattern and line after the matches line :
find pattern to 4th line :
find pattern until find another pattern (
JasontoAnand) :
print line number of matched pattern
[!NOTE|label:references:]
delete
delete all
range delete
delete the 2nd line :
$ sed '2 d' /path/to/filedelete between
1and4lines :$ sed '1,4 d' /path/to/file
conditional delete
delete all empty lines:
$ sed '/^$/ d' /path/to/filedelete all comment lines :
$ sed '/^#/ d' /path/to/file
substitute
substitute-flags
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
&
&substitution grouping
or via -r
cheatsheet
get first matching patten ( for CERTIFICATE )
CERTIFICATE )[!TIP]
sample.crt
regular pattern
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
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
add
'or"to strings[!NOTE|label:references:]
Last updated
Was this helpful?