Skip to content

Instantly share code, notes, and snippets.

@rcanepa
Last active August 8, 2016 03:58
Show Gist options
  • Save rcanepa/07d414b20d6f757cf6d04dc7e6ec4ebf to your computer and use it in GitHub Desktop.
Save rcanepa/07d414b20d6f757cf6d04dc7e6ec4ebf to your computer and use it in GitHub Desktop.
UNIX sed (stream editor)
$ cd /tmp/
$ echo '10 tiny toes' >> text
$ echo 'this is that' >> text
$ echo '5 funny 0' >> text
$ echo 'one two three' >> text
$ echo 'tree twice' >> text
$ cat text
10 tiny toes
this is that
5 funny 0
one two three
tree twice
$ cat text | sed 's/t/T/'
^
substitute
10 Tiny toes
This is that
5 funny 0
one Two three
Tree twice
$ cat text | sed 's/t/T/g'
^
apply globally
10 Tiny Toes
This is ThaT
5 funny 0
one Two Three
Tree Twice
$ sed -i 's/t/T/g' text
^
edit file in place (it will modify the file!)
$ cat text
10 Tiny Toes
This is ThaT
5 funny 0
one Two Three
Tree Twice
$ sed -i 's/T/t/g' text
$ cat text
10 tiny toes
this is that
5 funny 0
one two three
tree twice
$ sed 's/^t/oooo/g' text
^
replace ts on the beginning of a line
10 tiny toes
oooohis is that
5 funny 0
one two three
ooooree twice
$ sed 's/t$/oooo/g' text
^
replace ts on the end of a line
10 tiny toes
this is thaoooo
5 funny 0
one two three
tree twice
$ sed -i 's/t/T/g' text
$ cat text
10 Tiny Toes
This is ThaT
5 funny 0
one Two Three
Tree Twice
$ sed 's/[0-9]/*/g' text
^
digits
** Tiny Toes
This is ThaT
* funny *
one Two Three
Tree Twice
$ sed 's/[a-z]/*/g' text
^
just lowercase letters
10 T*** T***
T*** ** T**T
5 ***** 0
*** T** T****
T*** T****
$ sed 's/[a-z][A-Z]/*/g' text
^
look for a lowercase letter followed by an uppercase letter
10 Tiny Toes
This is Th*
5 funny 0
one Two Three
Tree Twice
$ sed 's/[a-zA-Z]/*/g' text
^
all lowercase and uppercase letters
10 **** ****
**** ** ****
5 ***** 0
*** *** *****
**** *****
$ sed 's/[A-z]/*/g' text
^
same as above (match all letters)
10 **** ****
**** ** ****
5 ***** 0
*** *** *****
**** *****
$ sed 's/[A-z0-9]/*/g' text
^
all letters and digits
** **** ****
**** ** ****
* ***** *
*** *** *****
**** *****
$ sed 's/[0-z]/*/g' text
^
same as above (all letters and digits)
** **** ****
**** ** ****
* ***** *
*** *** *****
**** *****
$ sed 's/[0-9]//g' text
^
replace with nothing (remove)
Tiny Toes
This is ThaT
funny
one Two Three
Tree Twice
$ sed 's/[0-9]/{&}/g' text
^
& contains the matched content
{1}{0} Tiny Toes
This is ThaT
{5} funny {0}
one Two Three
Tree Twice
$ sed 's/[0-9][0-9]*/**&**/g' text
^ ^
match any 1 or more digit number and surround it with **
**10** Tiny Toes
This is ThaT
**5** funny **0**
one Two Three
Tree Twice
$ sed 's/y/\//g' text
^
escape character (this will substitute any 'y' for a '/')
10 Tin/ Toes
This is ThaT
5 funn/ 0
one Two Three
Tree Twice
$ sed 's/y/\/\\/g' text
^ ^
escaping '\' and '/' (this will substitute any 'y' for '\/')
10 Tin/\ Toes
This is ThaT
5 funn/\ 0
one Two Three
Tree Twice
$ echo '/usr/share/local/' >> text
$ cat text
10 Tiny Toes
This is ThaT
5 funny 0
one Two Three
Tree Twice
/usr/share/local/
$ sed 's_/usr/share/local/_!_g' text
^ ^ ^
delimiters were changed from / to _, so escaping / was unnecessary
10 Tiny Toes
This is ThaT
5 funny 0
one Two Three
Tree Twice
!
$ sed 's/[^0-9]/*/g' text
^
replace everything but numbers with a '*'
10**********
************
5*******0
*************
**********
*****************
$ echo '[Is] this .a. weird line?' >> text
$ cat text
10 Tiny Toes
This is ThaT
5 funny 0
one Two Three
Tree Twice
/usr/share/local/
[Is] this .a. weird line?
$ sed 's/[^0-9a-zA-Z ]/*/g' text
^ ^ ^
replace everything that is not an alphanumeric character of a space with '*'
10 Tiny Toes
This is ThaT
5 funny 0
one Two Three
Tree Twice
*usr*share*local*
*Is* this *a* weird line*
$ sed 's/funny/fluffy/g;s/one/XXX/g' text
^
delimiter to apply another replacement (funny -> fluffy and one -> XXX)
10 Tiny Toes
This is ThaT
5 fluffy 0
XXX Two Three
Tree Twice
/usr/share/local/
[Is] this .a. weird line?
$ echo -e 'this is that\none two three\nfour five six' > text2
$ cat text2
this is that
one two three
four five six
$ sed 's/^[a-zA-Z0-9_]* //' text2
^ ^^
remove any starting string of alphanumeric characters followed by a space (remove the first word)
is that
two three
five six
$ sed 's/ [a-zA-Z0-9_]*$//' text2
^ ^ ^^
remove any ending alphanumeric seq of chars which are prefixed by a space (remove the last word)
this is
one two
four five
$ sed 's/[a-zA-Z0-9_]* //g' text2
^ ^^ ^
remove all ocurrences of an alphanumeric seq of chars which are followed by a space (leave the last word)
that
three
six
$ sed 's/./;; &/' text2
^ ^
replace the first char of every line by itself prefixed by ';; ' (comment lines)
;; this is that
;; one two three
;; four five six
$ sed 's/.\{3\}/333/' text2
^ ^
replace the first 3 chars with a '333'
333s is that
333 two three
333r five six
$ sed 's/.* \(.*\) .*/\1/' text2
^ ^ ^
capture a group which is surrounded by spaces and replace everything by the group (leave the word of the middle)
is
two
five
$ sed 's/\(.* \).*\( .*\)$/g1: \1, g2:\2/' text2
^ ^ ^ ^
capture a group which is a word ended by an space, a group which is a word prefixed by an space and replace everything by 'g1: ' + group1 + ', g2:' plus group 2
g1: this , g2: that
g1: one , g2: three
g1: four , g2: six
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment