+4

[ShellScript] `Sed` for dummies

Command sed được sử dụng rất nhiều khi thao tác với file hay shellscript, nay được cu em share cho 1 cái tuts về nó thấy khá hay và tiện ích nên mình share lên đây, về cơ bản để sử dụng lệnh sed tốt bạn cần biết các tham số của nó và hơn cả là hiểu về Regex. Dưới đây là các command hay dùng của sed

NUMBERING

  • Đếm số line của một file
# count lines (emulates "wc -l")
sed -n '$=' file.txt
  • Đánh số cho mỗi dòng của file
# Numbering lines (same as "cat -n file.txt")
sed = file.txt
sed = file.txt | sed 'N;s/\n/\t/'

TEXT CONVERSION AND SUBSTITUTION

  • Convert DOS newlines (CR/LF) to Unix format
# Assumes that all lines end with CR/LF
sed 's/.$//'
  • Replace sử dụng regex
sed 's/foo/bar/'             # replaces only 1st instance in a line
sed 's/foo/bar/4'            # replaces only 4th instance in a line
sed 's/foo/bar/g'            # replaces ALL instances in a line
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case, '\1' and '\2' are groups in Regex
sed 's/\(.*\)foo/\1bar/'            # replace only the last case
# substitute "foo" with "bar" ONLY for lines which contain "baz"
sed '/baz/s/foo/bar/g'

# substitute "foo" with "bar" EXCEPT for lines which contain "baz"
sed '/baz/!s/foo/bar/g'
  • Nếu muốn replace trực tiếp file thêm tham số -i
sed -i  's/foo/bar/g'  file.txt # Replace inplace file.txt
sed -i.bak  's/foo/bar/g' file.txt  # Create backup file.txt.bak first then modifying file.txt
  • Join 2 lines với nhau
# join pairs of lines side-by-side (like "paste")
sed '$!N;s/\n/ /'

SELECTIVE PRINTING OF CERTAIN LINES

  • In header của file
# print first 10 lines of file (emulates behavior of "head")
sed 10q

# print first line of file (emulates "head -1")
sed q
  • In các dòng cuối của file
# print the last 2 lines of a file (emulates "tail -2")
sed '$!N;$!D'

# print the last line of a file (emulates "tail -1")
sed '$!d'                    # method 1
sed -n '$p'                  # method 2
  • In các line match với Regex
# print only lines which match regular expression (emulates "grep")
sed -n '/regexp/p'           # method 1,
sed '/regexp/!d'             # method 2

# print section of file from regular expression to end of file
sed -n '/regexp/,$p'
  • In các line không match với Regex
sed -n '/regexp/!p'          # method 1, corresponds to above
sed '/regexp/d'              # method 2, simpler syntax
  • Print các lines dựa vào line number
# print section of file based on line numbers (lines 8-12, inclusive)
sed -n '8,12p'               # method 1
sed '8,12!d'                 # method 2

# print line number 52
sed -n '52p'                 # method 1
sed '52!d'                   # method 2
sed '52q;d'                 # method 3, efficient on large files

SELECTIVE DELETION OF CERTAIN LINES

  • Xóa các dòng duplicate liền kề
# delete duplicate, consecutive lines from a file (emulates "uniq").
# First line in a set of duplicate lines is kept, rest are deleted.
sed '$!N; /^\(.*\)\n\1$/!P; D'
# delete all lines except duplicate lines (emulates "uniq -d").
sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'

# delete the first 10 lines of a file
sed '1,10d'

# delete the last line of a file
sed '$d'

# delete the last 2 lines of a file
sed 'N;$!P;$!D;$d'
  • Delete lines matching pattern
sed '/pattern/d'
  • Delete ALL blank lines from a file (same as "grep '.' ")
sed '/^$/d'                          # method 1
sed '/./!d'                           # method 2

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí