SED Command

«  Reset Github Commit Author
UICollectionView Updates in WWDC2020  »

sed is the shorten from Stream Editor, and it can process some function like search, find, replace, insert and delete. By using sed, we can edit files without opening it.

Here is a list of GNU sed command line options. I’d like to just memo some common command at here.

Search & Find & Replace

# Replace word "a" with "b"
$sed 's/a/b/' test.txt

# Replace 2 occurrence of word "a" with "b"
$sed 's/a/b/2' test.txt

# Parenthesize first character of each word
$ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
# (W)elcome (T)o (T)he (G)eek (S)tuff

# Replace string on a range of lines (ex: 1-3 lines)
$sed '1,3 s/a/b/' test.txt

Insert

# add line "hello word" after 3rd line
$sed '3 a/hello word/' test.txt

# insert line before 4th line
$sed '4 i/hello word/' test.txt

Delete

# Delete particular line (ex: 5 line)
$ sed '5d' test.txt

# Delete last line
$ sed '$d' test.txt

# Delete 3-6 lines
$ sed '3,6d' test.txt

# Delete from 10th to last line
$ sed '12,$d' test.txt

# Delete pattern matching line
$ sed '/pattern/d' test.txt

Reference

Published on 14 May 2021 Find me on Facebook, Twitter!

«  Reset Github Commit Author
UICollectionView Updates in WWDC2020  »

Comments

    Join the discussion for this article at here . Our comments is using Github Issues. All of posted comments will display at this page instantly.