Skip to main content

sed

Intro

We can use sed to modify text lines, the popular use case of it is text replacement.

Syntax: sed <script> <input_file>

Basic usage

For replace text, the <script> will look like this:

s/regex_pattern/replacement/

tip

the separator / maybe replaced with other characters. So the below examples are all the same:

s/regex_pattern/replacement/
s@regex_pattern@replacement@
s|regex_pattern|replacement|
sed
# we can replace text
$ echo "sohan blog" | sed "s/blog/docs/"
sohan docs

# or delete line at n-th
$ seq 1 5 | sed 4d
1
2
3
5

# we also can delete line with our pattern
# remove lines end with even numbers
$ seq 1 5 | sed '/[02468]$/d'
1
3
5

# we can do more with sed with subexpression
# for example to remove the my_ keyword in this below files
$ ls
my_cat my_dog my_funny_parrot my_penguin my_talkative_parrot

$ ls | sed 's/my_\(.*\)/\1/'
# my_\(.*\) we group the part after my_
# and refer to it later with \1
cat
dog
funny_parrot
penguin
talkative_parrot

Organize files to sub-dir alphabetically

For example, the sohan will go to s/ directory

$ touch sohan blog is useful and fun

# list file begin with 2 characters
$ ls ??*
and blog fun is sohan useful

# use Shell Expansion for pre-create these alphabet directories
$ mkdir {a..z}

# create commands with sed
$ ls ??* | sed 's@^\(.\)\(.*\)$@mv \1\2 \1@'
# @ is the separator
# ^\(.\) => get first character of a file, group as \1
# \(.*\) => get the rest character of a file, group as \2
# mv \1\2 \1 => our final command
mv and a
mv blog b
mv fun f
mv is i
mv sohan s
mv useful u

# pipe it with bash to execute
$ ls ??* | sed 's@^\(.\)\(.*\)$@mv \1\2 \1@' | bash