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

Replacing or substituting string

Replacing the nth occurrence of a pattern in a line

Replacing all the occurrence of the pattern in a line

Replacing from nth occurrence to all occurrences in a line

Changing the slash (/ -> _) delimiter

Using & as the matched string ( adding curly braises )

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

Switch the words “unixlinux” as “linuxunix”

Switching the first three characters in a line

Duplicating the replaced line with /p flag

Printing only the replaced lines

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

To run multiple sed commands in a single sed command

Replacing string on a specific line number

Replacing string on a range of lines

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

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”

Deleting lines

Duplicating lines

Sed as grep command

Add a line after a match

Add a line before a match

Change a line

Transform like tr command

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

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

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

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

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

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

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

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

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

Remove all digits and replace trailing spaces into a single space

Last updated