sed

sed linux command cheatsheet by Thamizhiniyan C S

Introduction

Sed is a Stream Editor used for modifying the files in unix (or linux).


Syntax

sed [flags] [pattern/script] [input file]
'[condition(s)(optional)] [command/mode(optional)]/[source/to-be-searched pattern(mandatory)]/[to-be-replaced pattern(depends on command/mode you use)]/[args/flags to operate on the pattern searched(optional)]'

Important Flags

FlagsDescription

-e

To add a script/command that needs to be executed with the pattern/script(on searching for pattern)

-f

Specify the file containing string pattern

-E

Use extended regular expressions

-n

Suppress the automatic printing or pattern spacing


Modes / Commands

CommandsDescription

s

(Most used)Substitute mode (find and replace mode)

y

Works same as substitution; the only difference is, it works on individual bytes in the string provided(this mode takes no arguments/conditions)


Arguments

Flags/ArgsDescription

/g

globally(any pattern change will be affected globally, i.e. throughout the text; generally works with s mode)

/i

To make the pattern search case-insensitive(can be combined with other flags)

/d

To delete the pattern found(Deletes the whole line; takes no parameter like conditions/modes/to-be-replaced string)

/p

prints the matching pattern(a duplicate will occur in output if not suppressed with -n flag.)

/1,/2,/3../n

To perform an operation on an nth occurrence in a line(works with s mode)


Examples

CommandDescription
sed 's/unix/linux/' file.txt

Replacing or substituting string

sed 's/unix/linux/2' file.txt

Replacing the nth occurrence of a pattern in a line

sed 's/unix/linux/g' file.txt

Replacing all the occurrence of the pattern in a line

sed 's/unix/linux/3g' file.txt

Replacing from nth occurrence to all occurrences in a line

sed 's_http://_www_' file.txt

Changing the slash (/ -> _) delimiter

sed 's/unix/{&}/' file.txt

Using & as the matched string ( adding curly braises )

sed 's/(unix)/11/' file.txt

Replace the word “unix” in a line with twice as the word like “unixunix”

sed 's/(unix)(linux)/21/' file.txt

Switch the words “unixlinux” as “linuxunix”

sed 's/^(.)(.)(.)/321/' file.txt

Switching the first three characters in a line

sed 's/unix/linux/p' file.txt

Duplicating the replaced line with /p flag

sed -n 's/unix/linux/p' file.txt

Printing only the replaced lines

sed 's/unix/linux/' file.txt | sed 's/os/system/'

Run multiple sed commands by piping the output of one sed command as input to another sed command

sed -e 's/unix/linux/' -e 's/os/system/' file.txt

To run multiple sed commands in a single sed command

sed '3 s/unix/linux/' file.txt

Replacing string on a specific line number

sed '1,3 s/unix/linux/' file.txt

Replacing string on a range of lines

sed '2,$ s/unix/linux/' file.txt

Replacing the text from second line to last line in the file

sed '/linux/ s/unix/centos/' file.txt

Replace on a lines which matches a pattern. Here the sed command first looks for the lines which has the pattern “linux” and then replaces the word “unix” with “centos”

sed '2 d' file.txt

Deleting lines

sed 'p' file.txt

Duplicating lines

sed -n '/unix/ p' file.txt

Sed as grep command

sed '/unix/ a "Add a new line"' file.txt

Add a line after a match

sed '/unix/ i "Add a new line"' file.txt

Add a line before a match

sed '/unix/ c "Change line"' file.txt

Change a line

sed 'y/ul/UL/' file.txt

Transform like tr command

sed 's/\bthe\b/this/'

Transform the first occurrence of the word 'the' with 'this' in each line

sed 's/\bthy\b/your/Ig'

Case Insensitive transform all the occurrences of the word 'thy' with 'your'

sed 's/thy/{&}/Ig'

Case Insensitive. Highlight all the occurrences of 'thy' by wrapping them up in brace brackets

sed 's/\(\d\{4\}\s\)\{2\}\(\d\{4\}\)/**** **** ****/g'

Replace occurrences of credit card numbers in the format XXXX XXXX XXXX with a masked version **** **** **** in each line

sed -E 's/([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)/\4 \3 \2 \1/' 

Rearranges these groups in reverse order, separated by whitespace, effectively reversing the order of the groups of digits in each line

sed 's/hack/back/3' file.txt

Substitute every 3rd occurrence of the word 'hack' to 'back' on every line

sed '3,4 s/hack/back/3' file.txt

Substitute every 3rd occurrence of the word 'hack' to 'back' on 3rd and 4th line

sed 's/  */:/g' sed1.txt

Formatting the trailing spaces in sed1.txt with a colon(:)

sed 's/\(^\b[[:alpha:] ]*\)\([[:digit:]]*\)/\=\> \1\[\2\]/g' file.txt

Making every line to start with a bullet point and enclose the digits in square brackets using regex

sed -e 's/[[:digit:]]//g' -e 's/  *//'g sed2.txt

Remove all digits and replace trailing spaces into a single space

Last updated