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
-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
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
/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
sed 's/unix/linux/' file.txtReplacing or substituting string
sed 's/unix/linux/2' file.txtReplacing the nth occurrence of a pattern in a line
sed 's/unix/linux/g' file.txtReplacing all the occurrence of the pattern in a line
sed 's/unix/linux/3g' file.txtReplacing from nth occurrence to all occurrences in a line
sed 's_http://_www_' file.txtChanging the slash (/ -> _) delimiter
sed 's/unix/{&}/' file.txtUsing & as the matched string ( adding curly braises )
sed 's/(unix)/11/' file.txtReplace the word “unix” in a line with twice as the word like “unixunix”
sed 's/(unix)(linux)/21/' file.txtSwitch the words “unixlinux” as “linuxunix”
sed 's/^(.)(.)(.)/321/' file.txtSwitching the first three characters in a line
sed 's/unix/linux/p' file.txtDuplicating the replaced line with /p flag
sed -n 's/unix/linux/p' file.txtPrinting 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.txtTo run multiple sed commands in a single sed command
sed '3 s/unix/linux/' file.txtReplacing string on a specific line number
sed '1,3 s/unix/linux/' file.txtReplacing string on a range of lines
sed '2,$ s/unix/linux/' file.txtReplacing the text from second line to last line in the file
sed '/linux/ s/unix/centos/' file.txtReplace 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.txtDeleting lines
sed 'p' file.txtDuplicating lines
sed -n '/unix/ p' file.txtSed as grep command
sed '/unix/ a "Add a new line"' file.txtAdd a line after a match
sed '/unix/ i "Add a new line"' file.txtAdd a line before a match
sed '/unix/ c "Change line"' file.txtChange a line
sed 'y/ul/UL/' file.txtTransform 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.txtSubstitute every 3rd occurrence of the word 'hack' to 'back' on every line
sed '3,4 s/hack/back/3' file.txtSubstitute every 3rd occurrence of the word 'hack' to 'back' on 3rd and 4th line
sed 's/ */:/g' sed1.txtFormatting the trailing spaces in sed1.txt with a colon(:)
sed 's/\(^\b[[:alpha:] ]*\)\([[:digit:]]*\)/\=\> \1\[\2\]/g' file.txtMaking 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.txtRemove all digits and replace trailing spaces into a single space
Last updated
Was this helpful?