info
-
sed
short forstream editor
-
filter
andtransform
text
advanced demo
-
sed substitution delimiter
-
@
delimiter$ sed 's@/path/to/file@/path/toto/file@g' path.txt
-
/
delimiter$ sed 's/\/path\/to\/file/\/path\/toto\/file/g' path.txt
-
-
sed
&
get matched string-
substitute
usr/bin
tousr/bin/local
$ sed 's@/usr/bin@&local@g' path.txt # & in the replacement part # will be replace with `/usr/bin`
-
match the whole line
# & replaces whatever matches with give regexp $ sed 's@^.*$@<<<&>>>@g' path.txt # `^.*$` matches the whole line
-
-
grouping and back-references
-
get only the first path in each line
$ sed 's/\(\/[^:]*\).*/\1/g' path.txt # `\(\/[^:]*\)` matches the path # avaiale before first `:` comes # `\1` replaces the first matched group
-
multigrouping
$ sed '$s@\([^:]*\):\([^:]*\):\([^:]*\)@\3:\2:\1@g' path.txt
-
get the list of usernames in
/etc/passwd
file$ sed 's/\([^:]*\).*/\1/' /etc/passwd
-
parenthesize first character of each word
$ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(1\)/g'
-
commify the simple numbers
$ cat numbers 1234 12121 3434 123 $ sed 's/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g' numbers
-