Lập trình shell

Xử lý text với sed

Nguyễn Hải Châu (nhchau@gmail.com)
Trường Đại học Công nghệ, ĐHQGHN

Giới thiệu sed

  • sed (viết tắt của stream editor) là một chương trình soạn thảo text từ stdin, theo phương thức từng dòng và không có tương tác
  • sed không thay thế được cho các hệ soạn thảo text có tương tác, nhưng là một công cụ mạnh để xử lý text khi viết shell script

Cách sử dụng sed

  • Cách sử dụng sed phổ biến nhất là từ dòng lệnh:

sed [options] commands [file-to-edit]

  • Có thể sử dụng sed kết hợp với các cơ chế định hướng lại vào/ra, pipe của UNIX:

command | [sed options] commands

hoặc

sed [options] commands < file

  • Lệnh của sed thường được nằm trong cặp dấu ''

Giới thiệu sed

  • Cách sử dụng đơn giản nhất, với lệnh rỗng:
sed '' test.txt
## 1 Introduction
## 2
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 5
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 7 sed will execute the directions automatically. This may seem confusing or unintuitive,
## 8 but it is a very powerful and fast way to transform text.
  • Hoặc sed '' < test.txt hoặc cat test.txt | sed ''

In các dòng của file

sed '1p' test.txt
## 1 Introduction
## 1 Introduction
## 2
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 5
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 7 sed will execute the directions automatically. This may seem confusing or unintuitive,
## 8 but it is a very powerful and fast way to transform text.
sed -n '3p' test.txt
## 3 The sed stream editor is a text editor that performs editing operations on information

In các dòng của file

sed -n '1,3p' test.txt
## 1 Introduction
## 2
## 3 The sed stream editor is a text editor that performs editing operations on information
sed -n '2,+2p' test.txt
## 2
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
  • Lưu ý: Dòng đầu tiên của file là 1, dòng cuối cùng của file là $

In các dòng của file

sed -n '1~2p' test.txt
## 1 Introduction
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 5
## 7 sed will execute the directions automatically. This may seem confusing or unintuitive,

In các dòng của file

sed -n '1,2!p' test.txt
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 5
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 7 sed will execute the directions automatically. This may seem confusing or unintuitive,
## 8 but it is a very powerful and fast way to transform text.
sed -n '1~2!p' test.txt
## 2
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 8 but it is a very powerful and fast way to transform text.

Chèn dòng

sed '3i New line before 3rd line' test.txt
## 1 Introduction
## 2
## New line before 3rd line
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 5
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 7 sed will execute the directions automatically. This may seem confusing or unintuitive,
## 8 but it is a very powerful and fast way to transform text.

Xóa dòng

sed '1~2d' test.txt
## 2
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 8 but it is a very powerful and fast way to transform text.
sed '1,2d' test.txt
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 5
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 7 sed will execute the directions automatically. This may seem confusing or unintuitive,
## 8 but it is a very powerful and fast way to transform text.

Tìm kiếm và in các dòng khớp với mẫu cần tìm

sed -n '/edit/p' test.txt
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 6 This means that you make all of the editing decisions as you are calling the command and       

Thay thế text

  • Thay thế text là một trong các ứng dụng phổ biến nhất của sed: 's/old word/new word/': Thay thế lần xuất hiện đầu tiên của old word bằng new word
cat test.txt | sed 's/line/LINE/'
## 1 Introduction
## 2
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 4 coming from standard input or a file. Sed edits LINE-by-line and in a non-interactive way. 
## 5
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 7 sed will execute the directions automatically. This may seem confusing or unintuitive,
## 8 but it is a very powerful and fast way to transform text.

Thay thế text

  • 's/old word/new word/g': Thay thế toàn bộ các từ old word bằng new word
cat test.txt | sed 's/line/LINE/g'
## 1 Introduction
## 2
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 4 coming from standard input or a file. Sed edits LINE-by-LINE and in a non-interactive way. 
## 5
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 7 sed will execute the directions automatically. This may seem confusing or unintuitive,
## 8 but it is a very powerful and fast way to transform text.

Thay thế text

  • 's/old word/new word/n': Thay thế lần xuất hiện thứ n của old word bằng new word, trong đó n là một số nguyên dương
cat test.txt | sed 's/edit/EDIT/2'
## 1 Introduction
## 2
## 3 The sed stream editor is a text EDITor that performs editing operations on information  
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 5
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 7 sed will execute the directions automatically. This may seem confusing or unintuitive,
## 8 but it is a very powerful and fast way to transform text.

Thay thế text

  • Có thể dùng tham số p trong phần cuối của lệnh tìm kiếm để in ra các dòng có thay đổi:
cat test.txt | sed -n 's/edit/EDIT/2p'
## 3 The sed stream editor is a text EDITor that performs editing operations on information

Tham chiếu đến text được tìm thấy

  • Tìm text bắt đầu từ đầu dòng đến "ing":
sed 's/^.*ing/REPLACED/' test.txt
## 1 Introduction
## 2
## REPLACED operations on information  
## REPLACED from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 5
## REPLACED the command and     
## REPLACED or unintuitive,
## 8 but it is a very powerful and fast way to transform text.

Tham chiếu đến text được tìm thấy

  • Thêm cặp dấu ngoặc vào mẫu text được tìm thấy:
sed 's/^.*ing/(&)/' test.txt
## 1 Introduction
## 2
## (3 The sed stream editor is a text editor that performs editing) operations on information  
## (4 coming) from standard input or a file. Sed edits line-by-line and in a non-interactive way.   
## 5
## (6 This means that you make all of the editing decisions as you are calling) the command and     
## (7 sed will execute the directions automatically. This may seem confusing) or unintuitive,
## 8 but it is a very powerful and fast way to transform text.

Thực hiện nhiều lệnh sed

  • Cách 1: sed 'command 1' file | sed 'command 2'
    • Tốn bộ nhớ và phải tạo nhiều tiến trình
    • Thích hợp cho các lệnh cần có sự tuần tự về dữ liệu output
  • Cách 2: sed -e 'command 1' -e 'command 2' file
  • Cách 3: sed 'command 1;command2' file
sed -n '3p' test.txt | sed 's/ing/ING/'
sed -n -e '3p' -e's/ing/ING/' test.txt
sed -n '3p;s/ing/ING/' test.txt
## 3 The sed stream editor is a text editor that performs editING operations on information  
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 3 The sed stream editor is a text editor that performs editing operations on information

Tìm kiếm nâng cao

  • sed cho phép sử dụng biểu thức chính qui trong tìm kiếm. Ví dụ sau thay 2 dòng đầu tiên bằng REPLACED:
sed '1,2s/.*/REPLACED/' test.txt
## REPLACED
## REPLACED
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 5
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 7 sed will execute the directions automatically. This may seem confusing or unintuitive,
## 8 but it is a very powerful and fast way to transform text.

Tìm kiếm nâng cao

  • Ví dụ sau thay it bằng IT cho tất cả các dòng chứa ing:
sed '/ing/s/it/IT/g' test.txt
## 1 Introduction
## 2
## 3 The sed stream edITor is a text edITor that performs edITing operations on information  
## 4 coming from standard input or a file. Sed edITs line-by-line and in a non-interactive way. 
## 5
## 6 This means that you make all of the edITing decisions as you are calling the command and       
## 7 sed will execute the directions automatically. This may seem confusing or unintuITive,
## 8 but it is a very powerful and fast way to transform text.

Sử dụng buffer

  • Buffer = vùng nhớ tạm thời có thể được thay đổi bởi các lệnh sed kế tiếp.
  • Các lệnh với buffer:
    • h: copy mẫu tìm kiếm hiện tại vào buffer (và do đó xóa nội dung trước của buffer)
    • H: copy mẫu tìm kiếm hiện tại nối tiếp vào buffer (append), phân cách nhau bởi dấu xuống dòng \n
    • g: copy nội dung buffer vào mẫu tìm kiếm hiện tại
    • G: nối nội dung buffer vào mẫu tìm kiếm hiện tại, phân cách nhau bởi dấu \n
    • x: Đổi nội dung mẫu tìm kiếm hiện tại và buffer cho nhau

Ví dụ sử dụng buffer

sed -n '1~2h;g;s/it/IT/p' test.txt
## 3 The sed stream edITor is a text editor that performs editing operations on information  
## 3 The sed stream edITor is a text editor that performs editing operations on information  
## 7 sed will execute the directions automatically. This may seem confusing or unintuITive,
## 7 sed will execute the directions automatically. This may seem confusing or unintuITive,

Ví dụ sử dụng buffer

sed -n '2,$G;h;$p' test.txt
## 8 but it is a very powerful and fast way to transform text.
## 7 sed will execute the directions automatically. This may seem confusing or unintuitive,
## 6 This means that you make all of the editing decisions as you are calling the command and       
## 5
## 4 coming from standard input or a file. Sed edits line-by-line and in a non-interactive way. 
## 3 The sed stream editor is a text editor that performs editing operations on information  
## 2
## 1 Introduction

Sử dụng sed script

  • Viết các lệnh sed trong một file, sau đó sử dụng lệnh sed -f sed_script_file
cat sedscript
## s/it/IT/g
## 1,3p
sed -n -f sedscript test.txt
## 1 Introduction
## 2
## 3 The sed stream edITor is a text edITor that performs edITing operations on information

Thực hành sed

  • 5.1. Xóa 3 dòng đầu tiên của 1 file và đưa các dòng còn lại ra màn hình
  • 5.2. Hãy xóa các dòng trắng của một file và đưa các dòng còn lại ra màn hình
  • 5.3. Hãy tìm các dòng của file có ký tự trắng ở cuối dòng và in ra các dòng đó
  • 5.4. Hãy thay thế từ Multics (không phân biệt chữ hoa/thường) bằng MULTICS trong file sedtext.txt
  • 5.5. Hãy đếm tần suất các từ xuất hiện trong một file text và in ra các từ kèm theo tần suất tương ứng (Lưu ý không phân biệt chữ hoa/thường)