Python in ký tự đặc biệt

Quyền được cấp để sao chép, phân phối và/hoặc sửa đổi tài liệu này theo các điều khoản của Giấy phép Tài liệu Tự do GNU, Phiên bản 1. 3 hoặc bất kỳ phiên bản nào mới hơn do Tổ chức Phần mềm Tự do xuất bản; . A copy of the license is included in the section entitled “GNU Free Documentation License”

Show

1. Giới thiệu

sed -i 's/hello/world/' file.txt
22 is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as
sed -i 's/hello/world/' file.txt
24),
sed -i 's/hello/world/' file.txt
22 works by making only one pass over the input(s), and is consequently more efficient. Nhưng khả năng lọc văn bản theo đường dẫn của
sed -i 's/hello/world/' file.txt
22 đặc biệt phân biệt nó với các loại trình chỉnh sửa khác


2 Running sed

This chapter covers how to run

sed -i 's/hello/world/' file.txt
22. Details of
sed -i 's/hello/world/' file.txt
22 scripts and individual
sed -i 's/hello/world/' file.txt
22 commands are discussed in the next chapter


2. 1 Overview

Normally

sed -i 's/hello/world/' file.txt
22 is invoked like this

For example, to replace all occurrences of ‘hello’ to ‘world’ in the file input. txt

sed 's/hello/world/' input.txt > output.txt

If you do not specify INPUTFILE, or if INPUTFILE is -,

sed -i 's/hello/world/' file.txt
22 filters the contents of the standard input. The following commands are equivalent

sed -i 's/hello/world/' file.txt
0

sed -i 's/hello/world/' file.txt
22 writes output to standard output. Use -i to edit files in-place instead of printing to standard output. See also the
sed -i 's/hello/world/' file.txt
33 and
sed -i 's/hello/world/' file.txt
34 commands for writing output to other files. Lệnh sau sửa đổi tệp. txt và không tạo ra bất kỳ đầu ra nào

sed -i 's/hello/world/' file.txt

Theo mặc định,

sed -i 's/hello/world/' file.txt
22 in tất cả đầu vào đã xử lý (ngoại trừ đầu vào đã bị sửa đổi/xóa bởi các lệnh như
sed -i 's/hello/world/' file.txt
36). Sử dụng -n để chặn đầu ra và lệnh
sed -i 's/hello/world/' file.txt
37 để in các dòng cụ thể. Lệnh sau chỉ in dòng 45 của tệp đầu vào

sed -i 's/hello/world/' file.txt
22 coi nhiều tệp đầu vào là một luồng dài. Ví dụ sau in dòng đầu tiên của tệp đầu tiên (một. txt) và dòng cuối cùng của tệp cuối cùng (ba. txt). Sử dụng -s để đảo ngược hành vi này

sed -i 's/hello/world/' file.txt
9

Không có tùy chọn -e hoặc -f,

sed -i 's/hello/world/' file.txt
22 sử dụng tham số không phải tùy chọn đầu tiên làm tập lệnh và các tham số không phải tùy chọn sau làm tệp đầu vào. Nếu các tùy chọn -e hoặc -f được sử dụng để chỉ định tập lệnh, tất cả các tham số không phải tùy chọn sẽ được lấy làm tệp đầu vào. Các tùy chọn -e và -f có thể được kết hợp và có thể xuất hiện nhiều lần (trong trường hợp đó, tập lệnh hiệu quả cuối cùng sẽ là tập lệnh nối của tất cả các tập lệnh riêng lẻ)

Các ví dụ sau là tương đương

sed -i 's/hello/world/' file.txt
1


2. 2 tùy chọn dòng lệnh

Định dạng đầy đủ để gọi

sed -i 's/hello/world/' file.txt
22 là

sed -i 's/hello/world/' file.txt
3

sed -i 's/hello/world/' file.txt
22 có thể được gọi với các tùy chọn dòng lệnh sau

sed -i 's/hello/world/' file.txt
42

In ra phiên bản

sed -i 's/hello/world/' file.txt
22 đang chạy và thông báo bản quyền, sau đó thoát ra

sed -i 's/hello/world/' file.txt
44

In thông báo sử dụng tóm tắt ngắn gọn các tùy chọn dòng lệnh này và địa chỉ báo cáo lỗi, sau đó thoát

_______045____046____047

Theo mặc định,

sed -i 's/hello/world/' file.txt
22 in ra không gian mẫu ở cuối mỗi chu kỳ thông qua tập lệnh (xem Cách thức hoạt động của
sed -i 's/hello/world/' file.txt
22). Các tùy chọn này vô hiệu hóa tính năng in tự động này và
sed -i 's/hello/world/' file.txt
22 chỉ tạo đầu ra khi được thông báo rõ ràng thông qua lệnh
sed -i 's/hello/world/' file.txt
37

sed -i 's/hello/world/' file.txt
52

In chương trình sed đầu vào ở dạng chuẩn và chú thích việc thực thi chương trình

sed -i 's/hello/world/' file.txt
6

sed -i 's/hello/world/' file.txt
53
sed -i 's/hello/world/' file.txt
54

Thêm các lệnh trong tập lệnh vào tập hợp các lệnh sẽ được chạy trong khi xử lý đầu vào

sed -i 's/hello/world/' file.txt
55
sed -i 's/hello/world/' file.txt
56

Thêm các lệnh có trong tệp script-file vào tập hợp các lệnh sẽ được chạy trong khi xử lý đầu vào

sed -i 's/hello/world/' file.txt
57
sed -i 's/hello/world/' file.txt
58

Tùy chọn này chỉ định rằng các tệp sẽ được chỉnh sửa tại chỗ. GNU

sed -i 's/hello/world/' file.txt
22 thực hiện điều này bằng cách tạo một tệp tạm thời và gửi đầu ra tới tệp này thay vì tới đầu ra tiêu chuẩn. 1

Tùy chọn này ngụ ý -s

Khi đến cuối tệp, tệp tạm thời được đổi tên thành tên gốc của tệp đầu ra. Phần mở rộng, nếu được cung cấp, được sử dụng để sửa đổi tên của tệp cũ trước khi đổi tên tệp tạm thời, do đó tạo bản sao lưu2)

Quy tắc này được tuân theo. nếu phần mở rộng không chứa

sed -i 's/hello/world/' file.txt
60, thì phần mở rộng đó sẽ được thêm vào cuối tên tệp hiện tại dưới dạng hậu tố; . Điều này cho phép bạn thêm tiền tố vào tệp sao lưu, thay vì (hoặc thêm vào) hậu tố hoặc thậm chí đặt các bản sao lưu của tệp gốc vào một thư mục khác (miễn là thư mục đó đã tồn tại)

Nếu không có phần mở rộng nào được cung cấp, tệp gốc sẽ bị ghi đè mà không tạo bản sao lưu

Bởi vì -i lấy một đối số tùy chọn, nó không nên được theo sau bởi các tùy chọn ngắn khác

sed -i 's/hello/world/' file.txt
62

Tương tự như -E -i không có hậu tố sao lưu - FILE sẽ được chỉnh sửa tại chỗ mà không cần tạo bản sao lưu

sed -i 's/hello/world/' file.txt
63

Điều này tương đương với --in-place=E, tạo FILES làm bản sao lưu của FILE

Hãy thận trọng khi sử dụng -n với -i. cái trước tắt tự động in các dòng và cái sau thay đổi tệp tại chỗ mà không cần sao lưu. Được sử dụng bất cẩn (và không có lệnh

sed -i 's/hello/world/' file.txt
37 rõ ràng), tệp đầu ra sẽ trống

sed -i 's/hello/world/' file.txt
9

sed -i 's/hello/world/' file.txt
65
sed -i 's/hello/world/' file.txt
66

Chỉ định độ dài ngắt dòng mặc định cho lệnh

sed -i 's/hello/world/' file.txt
67. Độ dài bằng 0 (không) có nghĩa là không bao giờ ngắt dòng dài. Nếu không được chỉ định, nó được lấy là 70

sed -i 's/hello/world/' file.txt
68

GNU

sed -i 's/hello/world/' file.txt
22 bao gồm một số tiện ích mở rộng cho POSIX sed. Để đơn giản hóa việc viết các tập lệnh di động, tùy chọn này sẽ vô hiệu hóa tất cả các tiện ích mở rộng mà tài liệu hướng dẫn sử dụng này, bao gồm cả các lệnh bổ sung. Hầu hết các tiện ích mở rộng đều chấp nhận các chương trình
sed -i 's/hello/world/' file.txt
22 nằm ngoài cú pháp do POSIX quy định, nhưng một số trong số chúng (chẳng hạn như hành vi của lệnh
sed -i 's/hello/world/' file.txt
71 được mô tả trong Báo cáo lỗi) thực sự vi phạm tiêu chuẩn. Nếu bạn chỉ muốn tắt loại tiện ích mở rộng thứ hai, bạn có thể đặt biến
sed -i 's/hello/world/' file.txt
72 thành một giá trị không trống

sed -i 's/hello/world/' file.txt
73
sed -i 's/hello/world/' file.txt
74

Tùy chọn này khả dụng trên mọi nền tảng, nhưng chỉ hiệu quả khi hệ điều hành phân biệt giữa tệp văn bản và tệp nhị phân. Khi có sự phân biệt như vậy—như trường hợp của MS-DOS, Windows, Cygwin—các tệp văn bản bao gồm các dòng được phân tách bằng ký tự xuống dòng và ký tự nguồn cấp dữ liệu, và

sed -i 's/hello/world/' file.txt
22 không thấy CR kết thúc. Khi tùy chọn này được chỉ định,
sed -i 's/hello/world/' file.txt
22 sẽ mở các tệp đầu vào ở chế độ nhị phân, do đó không yêu cầu xử lý đặc biệt này và xem xét các dòng kết thúc tại một nguồn cấp dữ liệu

sed -i 's/hello/world/' file.txt
77

Tùy chọn này chỉ khả dụng trên các nền tảng hỗ trợ liên kết tượng trưng và chỉ có tác dụng nếu tùy chọn -i được chỉ định. Trong trường hợp này, nếu tệp được chỉ định trên dòng lệnh là một liên kết tượng trưng, ​​thì

sed -i 's/hello/world/' file.txt
22 sẽ theo liên kết và chỉnh sửa đích cuối cùng của liên kết. Hành vi mặc định là ngắt liên kết tượng trưng để đích liên kết sẽ không bị sửa đổi

sed -i 's/hello/world/' file.txt
79
sed -i 's/hello/world/' file.txt
80
sed -i 's/hello/world/' file.txt
81

Sử dụng các biểu thức chính quy mở rộng thay vì các biểu thức chính quy cơ bản. Biểu thức chính quy mở rộng là những biểu thức mà

sed -i 's/hello/world/' file.txt
82 chấp nhận; . Về mặt lịch sử, đây là một phần mở rộng GNU, nhưng phần mở rộng -E đã được thêm vào tiêu chuẩn POSIX (http. // austingroupbugs. lượt xem trên mạng. php?id=528), vì vậy hãy sử dụng -E cho tính di động. GNU sed đã chấp nhận -E như một tùy chọn không có giấy tờ trong nhiều năm và *BSD seds cũng đã chấp nhận -E trong nhiều năm, nhưng các tập lệnh sử dụng -E có thể không chuyển sang các hệ thống cũ hơn khác. Xem Biểu thức chính quy mở rộng

sed -i 's/hello/world/' file.txt
83
sed -i 's/hello/world/' file.txt
84

Theo mặc định,

sed -i 's/hello/world/' file.txt
22 sẽ coi các tệp được chỉ định trên dòng lệnh là một luồng dài liên tục duy nhất. Tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt
22 này cho phép người dùng coi chúng là các tệp riêng biệt. phạm vi địa chỉ (chẳng hạn như '/abc/,/def/') không được phép mở rộng trên nhiều tệp, số dòng có liên quan đến phần đầu của mỗi tệp,
sed -i 's/hello/world/' file.txt
87 đề cập đến dòng cuối cùng của mỗi tệp và các tệp được gọi từ
sed -i 's/hello/world/' file.txt
88

sed -i 's/hello/world/' file.txt
89

Trong chế độ hộp cát, các lệnh

sed -i 's/hello/world/' file.txt
90 bị từ chối - các chương trình chứa chúng sẽ bị hủy bỏ mà không được chạy. Chế độ hộp cát đảm bảo
sed -i 's/hello/world/' file.txt
22 chỉ hoạt động trên các tệp đầu vào được chỉ định trên dòng lệnh và không thể chạy các chương trình bên ngoài

_______092____093

Bộ đệm cả đầu vào và đầu ra ở mức tối thiểu nhất có thể. (Điều này đặc biệt hữu ích nếu đầu vào đến từ những thứ như 'tail -f' và bạn muốn xem đầu ra được chuyển đổi càng sớm càng tốt. )

________ 094 ________ 095 ________ 096

Coi đầu vào là một tập hợp các dòng, mỗi dòng được kết thúc bằng một byte 0 (ký tự ASCII 'NUL') thay vì một dòng mới. Tùy chọn này có thể được sử dụng với các lệnh như ‘sort -z’ và ‘find -print0’ để xử lý các tên tệp tùy ý

Nếu không có tùy chọn -e, -f, --expression hoặc --file nào được cung cấp trên dòng lệnh, thì đối số không phải tùy chọn đầu tiên trên dòng lệnh sẽ được coi là tập lệnh sẽ được thực thi

Nếu bất kỳ tham số dòng lệnh nào vẫn còn sau khi xử lý ở trên, các tham số này được hiểu là tên của tệp đầu vào sẽ được xử lý. Tên tệp '-' đề cập đến luồng đầu vào tiêu chuẩn. Đầu vào tiêu chuẩn sẽ được xử lý nếu không có tên tệp nào được chỉ định


2. 3 Trạng thái thoát

Trạng thái thoát bằng 0 cho biết thành công và giá trị khác không cho biết thất bại. GNU

sed -i 's/hello/world/' file.txt
22 trả về các giá trị lỗi trạng thái thoát sau

0

Hoàn thành thành công

1

Lệnh không hợp lệ, cú pháp không hợp lệ, biểu thức chính quy không hợp lệ hoặc lệnh mở rộng GNU

sed -i 's/hello/world/' file.txt
22 được sử dụng với --posix

2

Không thể mở một hoặc nhiều tệp đầu vào được chỉ định trên dòng lệnh (e. g. nếu không tìm thấy tệp hoặc quyền đọc bị từ chối). Xử lý tiếp tục với các tập tin khác

4

Lỗi I/O hoặc lỗi xử lý nghiêm trọng trong thời gian chạy, GNU

sed -i 's/hello/world/' file.txt
22 bị hủy bỏ ngay lập tức

Ngoài ra, các lệnh

sed -i 's/hello/world/' file.txt
900 và
sed -i 's/hello/world/' file.txt
901 có thể được sử dụng để chấm dứt
sed -i 's/hello/world/' file.txt
22 với giá trị mã thoát tùy chỉnh (đây là tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt
22)

sed 's/hello/world/' input.txt > output.txt
9


3 tập lệnh sed -i 's/hello/world/' file.txt 22


3. 1 Tổng quan kịch bản sed -i 's/hello/world/' file.txt 22

Chương trình

sed -i 's/hello/world/' file.txt
22 bao gồm một hoặc nhiều lệnh
sed -i 's/hello/world/' file.txt
22, được truyền vào bởi một hoặc nhiều tùy chọn -e, -f, --expression và --file hoặc đối số không phải tùy chọn đầu tiên nếu không có tùy chọn nào trong số này được sử dụng. Tài liệu này sẽ đề cập đến “tập lệnh”
sed -i 's/hello/world/' file.txt
22; . Xem Tổng quan

sed -i 's/hello/world/' file.txt
22 lệnh theo cú pháp này

X là lệnh một ký tự

sed -i 's/hello/world/' file.txt
22.
sed -i 's/hello/world/' file.txt
911 là một địa chỉ dòng tùy chọn. Nếu
sed -i 's/hello/world/' file.txt
911 được chỉ định, lệnh X sẽ chỉ được thực hiện trên các dòng phù hợp.
sed -i 's/hello/world/' file.txt
911 có thể là số dòng đơn, biểu thức chính quy hoặc dải dòng (xem địa chỉ sed).
sed -i 's/hello/world/' file.txt
914 bổ sung được sử dụng cho một số lệnh
sed -i 's/hello/world/' file.txt
22

Ví dụ sau xóa dòng 30 đến 35 trong đầu vào.

sed -i 's/hello/world/' file.txt
916 là một dải địa chỉ.
sed -i 's/hello/world/' file.txt
36 là lệnh xóa

sed -i 's/hello/world/' file.txt
04

Ví dụ sau in tất cả đầu vào cho đến khi tìm thấy một dòng bắt đầu bằng từ 'foo'. Nếu dòng đó được tìm thấy,

sed -i 's/hello/world/' file.txt
22 sẽ kết thúc với trạng thái thoát 42. Nếu dòng đó không được tìm thấy (và không có lỗi nào khác xảy ra),
sed -i 's/hello/world/' file.txt
22 sẽ thoát với trạng thái 0.
sed -i 's/hello/world/' file.txt
920 là một địa chỉ biểu thức chính quy.
sed -i 's/hello/world/' file.txt
900 là lệnh thoát.
sed -i 's/hello/world/' file.txt
922 là tùy chọn lệnh

sed -i 's/hello/world/' file.txt
00

Các lệnh trong tập lệnh hoặc tệp tập lệnh có thể được phân tách bằng dấu chấm phẩy (

sed -i 's/hello/world/' file.txt
923) hoặc dòng mới (ASCII 10). Nhiều tập lệnh có thể được chỉ định với các tùy chọn -e hoặc -f

Các ví dụ sau đây đều tương đương. Họ thực hiện hai hoạt động

sed -i 's/hello/world/' file.txt
22. xóa bất kỳ dòng nào khớp với biểu thức chính quy
sed -i 's/hello/world/' file.txt
920 và thay thế tất cả các lần xuất hiện của chuỗi 'xin chào' bằng 'thế giới'

sed -i 's/hello/world/' file.txt
01

Các lệnh

sed -i 's/hello/world/' file.txt
926,
sed -i 's/hello/world/' file.txt
927,
sed -i 's/hello/world/' file.txt
928, do cú pháp của chúng, không thể theo sau dấu chấm phẩy hoạt động như dấu phân cách lệnh và do đó nên được kết thúc bằng dòng mới hoặc được đặt ở cuối tập lệnh hoặc tệp tập lệnh. Các lệnh cũng có thể được đặt trước các ký tự khoảng trắng không quan trọng tùy chọn. Xem Cú pháp nhiều lệnh


3. 2 sed -i 's/hello/world/' file.txt 22 lệnh tóm tắt

Các lệnh sau được hỗ trợ trong GNU

sed -i 's/hello/world/' file.txt
22. Một số là các lệnh POSIX tiêu chuẩn, trong khi một số khác là các phần mở rộng của GNU. Chi tiết và ví dụ cho từng lệnh có trong các phần sau. (Mnemonics) được hiển thị trong ngoặc đơn

sed -i 's/hello/world/' file.txt
931
sed -i 's/hello/world/' file.txt
932

Nối văn bản sau một dòng

sed -i 's/hello/world/' file.txt
933

Nối văn bản sau một dòng (cú pháp thay thế)

sed -i 's/hello/world/' file.txt
934

Chi nhánh vô điều kiện để dán nhãn. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu

sed -i 's/hello/world/' file.txt
935
sed -i 's/hello/world/' file.txt
932

Thay thế (thay đổi) dòng bằng văn bản

sed -i 's/hello/world/' file.txt
937

Thay thế (thay đổi) dòng bằng văn bản (cú pháp thay thế)

sed -i 's/hello/world/' file.txt
36

Xóa không gian mẫu;

sed -i 's/hello/world/' file.txt
939

Nếu không gian mẫu chứa các dòng mới, hãy xóa văn bản trong không gian mẫu cho đến dòng mới đầu tiên và bắt đầu lại chu kỳ với không gian mẫu kết quả mà không đọc dòng đầu vào mới

Nếu không gian mẫu không chứa dòng mới, hãy bắt đầu một chu kỳ mới bình thường như thể lệnh

sed -i 's/hello/world/' file.txt
36 đã được ban hành

sed -i 's/hello/world/' file.txt
941

Thực thi lệnh được tìm thấy trong không gian mẫu và thay thế không gian mẫu bằng đầu ra;

sed -i 's/hello/world/' file.txt
942

Thực thi lệnh và gửi đầu ra của nó tới luồng đầu ra. Lệnh có thể chạy trên nhiều dòng, trừ dòng cuối cùng có dấu gạch chéo ngược

sed -i 's/hello/world/' file.txt
943

(tên tệp) In tên tệp của tệp đầu vào hiện tại (với một dòng mới ở cuối)

sed -i 's/hello/world/' file.txt
944

Thay thế nội dung của không gian mẫu bằng nội dung của không gian giữ

sed -i 's/hello/world/' file.txt
945

Nối một dòng mới vào nội dung của không gian mẫu, rồi nối thêm nội dung của không gian giữ vào nội dung của không gian mẫu

sed -i 's/hello/world/' file.txt
946

(giữ) Thay thế nội dung của không gian giữ bằng nội dung của không gian mẫu

sed -i 's/hello/world/' file.txt
947

Nối một dòng mới vào nội dung của không gian giữ, sau đó nối thêm nội dung của không gian mẫu vào nội dung của không gian giữ

sed -i 's/hello/world/' file.txt
948
sed -i 's/hello/world/' file.txt
932

chèn văn bản trước một dòng

sed -i 's/hello/world/' file.txt
950

chèn văn bản trước một dòng (cú pháp thay thế)

sed -i 's/hello/world/' file.txt
67

In không gian mẫu ở dạng rõ ràng

sed -i 's/hello/world/' file.txt
952

(tiếp theo) Nếu tính năng tự động in không bị tắt, hãy in không gian mẫu, sau đó, bất kể, thay thế không gian mẫu bằng dòng nhập tiếp theo. Nếu không có thêm đầu vào thì

sed -i 's/hello/world/' file.txt
22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào

sed -i 's/hello/world/' file.txt
71

Thêm một dòng mới vào không gian mẫu, sau đó nối dòng đầu vào tiếp theo vào không gian mẫu. Nếu không có thêm đầu vào thì

sed -i 's/hello/world/' file.txt
22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào

sed -i 's/hello/world/' file.txt
37

In không gian mẫu

sed -i 's/hello/world/' file.txt
957

In không gian mẫu, cho đến mẫu đầu tiên

sed -i 's/hello/world/' file.txt
958

(thoát) Thoát

sed -i 's/hello/world/' file.txt
22 mà không xử lý thêm bất kỳ lệnh hoặc đầu vào nào

sed -i 's/hello/world/' file.txt
960

(thoát) Lệnh này giống như lệnh

sed -i 's/hello/world/' file.txt
900, nhưng sẽ không in nội dung của không gian mẫu. Giống như
sed -i 's/hello/world/' file.txt
900, nó cung cấp khả năng trả lại mã thoát cho người gọi

sed -i 's/hello/world/' file.txt
963

Đọc tên tập tin

sed -i 's/hello/world/' file.txt
964

Xếp hàng một dòng tên tệp sẽ được đọc và chèn vào luồng đầu ra ở cuối chu kỳ hiện tại hoặc khi dòng đầu vào tiếp theo được đọc

sed -i 's/hello/world/' file.txt
965

(thay thế) Khớp biểu thức chính quy với nội dung của không gian mẫu. Nếu tìm thấy, hãy thay thế chuỗi phù hợp bằng chuỗi thay thế

sed -i 's/hello/world/' file.txt
966

(thử nghiệm) Chỉ nhánh để gắn nhãn nếu đã có một sự thay thế

sed -i 's/hello/world/' file.txt
967 thành công kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu

sed -i 's/hello/world/' file.txt
968

(thử nghiệm) Chỉ nhánh để gắn nhãn nếu không có thay thế

sed -i 's/hello/world/' file.txt
967 thành công nào kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu

sed -i 's/hello/world/' file.txt
970

(phiên bản) Lệnh này không làm gì, nhưng làm cho

sed -i 's/hello/world/' file.txt
22 không thành công nếu tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt
22 không được hỗ trợ hoặc nếu phiên bản được yêu cầu không có sẵn

sed -i 's/hello/world/' file.txt
973

Viết không gian mẫu vào tên tệp

sed -i 's/hello/world/' file.txt
974

Ghi vào tên tệp đã cho phần không gian mẫu cho đến dòng mới đầu tiên

sed -i 's/hello/world/' file.txt
975

Trao đổi nội dung của không gian giữ và mẫu

sed -i 's/hello/world/' file.txt
976

Chuyển ngữ bất kỳ ký tự nào trong không gian mẫu khớp với bất kỳ ký tự nguồn nào với ký tự tương ứng trong ký tự đích

sed -i 's/hello/world/' file.txt
977

(zap) Lệnh này làm trống nội dung của không gian mẫu

sed -i 's/hello/world/' file.txt
978

Một nhận xét, cho đến dòng mới tiếp theo

sed -i 's/hello/world/' file.txt
979

Nhóm một số lệnh lại với nhau

sed -i 's/hello/world/' file.txt
980

In số dòng đầu vào hiện tại (với một dòng mới ở cuối)

sed -i 's/hello/world/' file.txt
981

Chỉ định vị trí của nhãn cho các lệnh nhánh (______1982,

sed -i 's/hello/world/' file.txt
983,
sed -i 's/hello/world/' file.txt
984)


3. 3 Lệnh sed -i 's/hello/world/' file.txt 967

Lệnh

sed -i 's/hello/world/' file.txt
967 (để thay thế) có lẽ là lệnh quan trọng nhất trong
sed -i 's/hello/world/' file.txt
22 và có rất nhiều tùy chọn khác nhau. Cú pháp của lệnh
sed -i 's/hello/world/' file.txt
967 là ‘s/regexp/replacement/flags’

Khái niệm cơ bản của nó là đơn giản. lệnh

sed -i 's/hello/world/' file.txt
967 cố gắng khớp không gian mẫu với biểu thức chính quy được cung cấp;

Để biết chi tiết về cú pháp biểu thức chính quy, hãy xem Địa chỉ biểu thức chính quy

Phần thay thế có thể chứa các tham chiếu

sed -i 's/hello/world/' file.txt
990 (n là một số từ 1 đến 9), tham chiếu đến phần trùng khớp được chứa giữa
sed -i 's/hello/world/' file.txt
991 thứ n và
sed -i 's/hello/world/' file.txt
992 trùng khớp của nó. Ngoài ra, phần thay thế có thể chứa các ký tự
sed -i 's/hello/world/' file.txt
993 chưa thoát tham chiếu đến toàn bộ phần khớp của không gian mẫu

Các ký tự

sed -i 's/hello/world/' file.txt
994 có thể được thay thế thống nhất bằng bất kỳ ký tự đơn nào khác trong bất kỳ lệnh
sed -i 's/hello/world/' file.txt
967 nào. Ký tự
sed -i 's/hello/world/' file.txt
994 (hoặc bất kỳ ký tự nào khác được sử dụng thay cho nó) chỉ có thể xuất hiện trong biểu thức chính quy hoặc thay thế nếu nó đứng trước ký tự
sed -i 's/hello/world/' file.txt
997

Cuối cùng, với tư cách là tiện ích mở rộng GNU

sed -i 's/hello/world/' file.txt
22, bạn có thể bao gồm một chuỗi đặc biệt được tạo bằng dấu gạch chéo ngược và một trong các chữ cái
sed -i 's/hello/world/' file.txt
999,
sed -i 's/hello/world/' file.txt
67,
sed -i 's/hello/world/' file.txt
101,
sed -i 's/hello/world/' file.txt
102 hoặc
sed -i 's/hello/world/' file.txt
103. Ý nghĩa như sau

sed -i 's/hello/world/' file.txt
104

Biến thay thế thành chữ thường cho đến khi tìm thấy

sed -i 's/hello/world/' file.txt
105 hoặc
sed -i 's/hello/world/' file.txt
106,

sed -i 's/hello/world/' file.txt
107

Biến ký tự tiếp theo thành chữ thường,

sed -i 's/hello/world/' file.txt
105

Chuyển từ thay thế sang chữ hoa cho đến khi tìm thấy

sed -i 's/hello/world/' file.txt
104 hoặc
sed -i 's/hello/world/' file.txt
106,

sed -i 's/hello/world/' file.txt
111

Chuyển ký tự tiếp theo thành chữ hoa,

sed -i 's/hello/world/' file.txt
106

Dừng chuyển đổi trường hợp bắt đầu bởi

sed -i 's/hello/world/' file.txt
104 hoặc
sed -i 's/hello/world/' file.txt
105

Khi cờ

sed -i 's/hello/world/' file.txt
944 đang được sử dụng, chuyển đổi trường hợp không lan truyền từ một lần xuất hiện của biểu thức chính quy sang một lần khác. Ví dụ: khi lệnh sau được thực thi với 'a-b-' trong không gian mẫu

đầu ra là 'axxB'. Khi thay thế '-' đầu tiên, chuỗi '\ u' chỉ ảnh hưởng đến thay thế trống của '\ 1'. Nó không ảnh hưởng đến ký tự

sed -i 's/hello/world/' file.txt
975 được thêm vào không gian mẫu khi thay thế
sed -i 's/hello/world/' file.txt
117 bằng
sed -i 's/hello/world/' file.txt
118

Mặt khác,

sed -i 's/hello/world/' file.txt
107 và
sed -i 's/hello/world/' file.txt
111 ảnh hưởng đến phần còn lại của văn bản thay thế nếu theo sau chúng là một thay thế trống. Với 'a-b-' trong không gian mẫu, lệnh sau

sẽ thay thế '-' bằng 'X' (chữ hoa) và 'b-' bằng 'Bx'. If this behavior is undesirable, you can prevent it by adding a ‘\E’ sequence—after ‘\1’ in this case

Để bao gồm một ký tự

sed -i 's/hello/world/' file.txt
997,
sed -i 's/hello/world/' file.txt
993 hoặc dòng mới trong phần thay thế cuối cùng, hãy đảm bảo đặt trước
sed -i 's/hello/world/' file.txt
997,
sed -i 's/hello/world/' file.txt
993 hoặc dòng mới mong muốn trong phần thay thế bằng một
sed -i 's/hello/world/' file.txt
997

Lệnh

sed -i 's/hello/world/' file.txt
967 có thể được theo sau bởi 0 hoặc nhiều cờ sau

sed -i 's/hello/world/' file.txt
944

Áp dụng thay thế cho tất cả các kết quả phù hợp với biểu thức chính quy, không chỉ lần đầu tiên

sed -i 's/hello/world/' file.txt
128

Chỉ thay thế trận đấu số của regrec

tương tác trong lệnh

sed -i 's/hello/world/' file.txt
967 Lưu ý. tiêu chuẩn POSIX không chỉ định điều gì sẽ xảy ra khi bạn kết hợp công cụ sửa đổi số và số
sed -i 's/hello/world/' file.txt
944 và hiện tại không có ý nghĩa thống nhất rộng rãi trên toàn bộ triển khai
sed -i 's/hello/world/' file.txt
22. Đối với GNU
sed -i 's/hello/world/' file.txt
22, tương tác được xác định là. bỏ qua các kết quả khớp trước số, sau đó khớp và thay thế tất cả các kết quả khớp từ số trở đi

sed -i 's/hello/world/' file.txt
37

Nếu thay thế đã được thực hiện, sau đó in không gian mẫu mới

Ghi chú. khi cả hai tùy chọn

sed -i 's/hello/world/' file.txt
37 và
sed -i 's/hello/world/' file.txt
941 được chỉ định, thứ tự tương đối của hai tùy chọn tạo ra kết quả rất khác nhau. Nói chung,
sed -i 's/hello/world/' file.txt
136 (đánh giá rồi in) là điều bạn muốn, nhưng thao tác ngược lại có thể hữu ích cho việc gỡ lỗi. Vì lý do này, phiên bản hiện tại của GNU
sed -i 's/hello/world/' file.txt
22 diễn giải đặc biệt sự hiện diện của các tùy chọn
sed -i 's/hello/world/' file.txt
37 cả trước và sau
sed -i 's/hello/world/' file.txt
941, in không gian mẫu trước và sau khi đánh giá, trong khi nói chung các cờ cho lệnh
sed -i 's/hello/world/' file.txt
967 chỉ hiển thị tác dụng của chúng một lần. Hành vi này, mặc dù được ghi lại, có thể thay đổi trong các phiên bản sau

sed -i 's/hello/world/' file.txt
973

Nếu thay thế được thực hiện, sau đó ghi kết quả vào tệp được đặt tên. Là một tiện ích mở rộng GNU

sed -i 's/hello/world/' file.txt
22, hai giá trị đặc biệt của tên tệp được hỗ trợ. /dev/stderr, ghi kết quả vào lỗi tiêu chuẩn và /dev/stdout, ghi vào đầu ra tiêu chuẩn. 3

sed -i 's/hello/world/' file.txt
941

Lệnh này cho phép một người chuyển đầu vào từ lệnh shell sang không gian mẫu. Nếu thay thế được thực hiện, lệnh được tìm thấy trong không gian mẫu được thực thi và không gian mẫu được thay thế bằng đầu ra của nó. Một dòng mới ở cuối bị chặn; . Đây là tiện ích mở rộng GNU

sed -i 's/hello/world/' file.txt
22

sed -i 's/hello/world/' file.txt
145
sed -i 's/hello/world/' file.txt
928

Công cụ sửa đổi

sed -i 's/hello/world/' file.txt
145 để so khớp biểu thức chính quy là một tiện ích mở rộng GNU làm cho
sed -i 's/hello/world/' file.txt
22 khớp với biểu thức chính quy theo cách không phân biệt chữ hoa chữ thường

sed -i 's/hello/world/' file.txt
149
sed -i 's/hello/world/' file.txt
150

Công cụ sửa đổi

sed -i 's/hello/world/' file.txt
149 để khớp biểu thức chính quy là một tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt
22 hướng GNU
sed -i 's/hello/world/' file.txt
22 khớp biểu thức chính quy ở chế độ nhiều dòng. Công cụ sửa đổi làm cho
sed -i 's/hello/world/' file.txt
154 và
sed -i 's/hello/world/' file.txt
87 khớp tương ứng (ngoài hành vi bình thường) chuỗi trống sau một dòng mới và chuỗi trống trước một dòng mới. Có các chuỗi ký tự đặc biệt (
sed -i 's/hello/world/' file.txt
156 và
sed -i 's/hello/world/' file.txt
157) luôn khớp với phần đầu hoặc phần cuối của bộ đệm. Ngoài ra, ký tự dấu chấm không khớp với ký tự dòng mới ở chế độ nhiều dòng


3. 4 lệnh thường dùng

Nếu bạn hoàn toàn sử dụng

sed -i 's/hello/world/' file.txt
22, rất có thể bạn sẽ muốn biết các lệnh này

sed -i 's/hello/world/' file.txt
978

[Không cho phép địa chỉ. ]

Nhân vật

sed -i 's/hello/world/' file.txt
978 bắt đầu bình luận;

Nếu bạn lo lắng về tính di động, hãy lưu ý rằng một số triển khai của

sed -i 's/hello/world/' file.txt
22 (không tuân thủ POSIX) chỉ có thể hỗ trợ một nhận xét một dòng và sau đó chỉ khi ký tự đầu tiên của tập lệnh là một
sed -i 's/hello/world/' file.txt
978

Cảnh báo. nếu hai ký tự đầu tiên của tập lệnh

sed -i 's/hello/world/' file.txt
22 là
sed -i 's/hello/world/' file.txt
164, thì tùy chọn -n (no-autoprint) là bắt buộc. Nếu bạn muốn đặt nhận xét trong dòng đầu tiên của tập lệnh và nhận xét đó bắt đầu bằng chữ 'n' và bạn không muốn hành vi này, thì hãy đảm bảo sử dụng chữ 'N' viết hoa hoặc đặt ít nhất một khoảng trắng

sed -i 's/hello/world/' file.txt
165

Thoát

sed -i 's/hello/world/' file.txt
22 mà không cần xử lý thêm bất kỳ lệnh hoặc đầu vào nào

Thí dụ. dừng lại sau khi in dòng thứ hai

Lệnh này chỉ chấp nhận một địa chỉ. Lưu ý rằng không gian mẫu hiện tại được in nếu tính năng in tự động không bị tắt với tùy chọn -n. Khả năng trả lại mã thoát từ tập lệnh

sed -i 's/hello/world/' file.txt
22 là phần mở rộng GNU
sed -i 's/hello/world/' file.txt
22

Xem thêm lệnh GNU

sed -i 's/hello/world/' file.txt
22 tiện ích mở rộng
sed -i 's/hello/world/' file.txt
901 thoát âm thầm mà không in không gian mẫu hiện tại

sed -i 's/hello/world/' file.txt
36

Xóa không gian mẫu;

Thí dụ. xóa dòng đầu vào thứ hai

sed -i 's/hello/world/' file.txt
37

In ra không gian mẫu (đến đầu ra tiêu chuẩn). Lệnh này thường chỉ được sử dụng cùng với tùy chọn dòng lệnh -n

Thí dụ. chỉ in dòng đầu vào thứ hai

sed -i 's/hello/world/' file.txt
952

Nếu tính năng tự động in không bị tắt, hãy in không gian mẫu, sau đó, bất kể, thay thế không gian mẫu bằng dòng nhập tiếp theo. Nếu không có thêm đầu vào thì

sed -i 's/hello/world/' file.txt
22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào

Lệnh này rất hữu ích để bỏ qua các dòng (e. g. xử lý mọi dòng thứ N)

Thí dụ. thực hiện thay thế trên mỗi dòng thứ 3 (i. e. hai lệnh

sed -i 's/hello/world/' file.txt
952 bỏ qua hai dòng)

sed -i 's/hello/world/' file.txt
02

GNU

sed -i 's/hello/world/' file.txt
22 cung cấp cú pháp địa chỉ mở rộng của bước đầu tiên để đạt được kết quả tương tự

sed -i 's/hello/world/' file.txt
03

sed -i 's/hello/world/' file.txt
177

A group of commands may be enclosed between

sed -i 's/hello/world/' file.txt
178 and
sed -i 's/hello/world/' file.txt
179 characters. This is particularly useful when you want a group of commands to be triggered by a single address (or address-range) match

Example. perform substitution then print the second input line

sed -i 's/hello/world/' file.txt
04


3. 5 Less Frequently-Used Commands

Though perhaps less frequently used than those in the previous section, some very small yet useful

sed -i 's/hello/world/' file.txt
22 scripts can be built with these commands

sed -i 's/hello/world/' file.txt
181

Chuyển ngữ bất kỳ ký tự nào trong không gian mẫu khớp với bất kỳ ký tự nguồn nào với ký tự tương ứng trong ký tự đích

Example. transliterate ‘a-j’ into ‘0-9’

sed -i 's/hello/world/' file.txt
05

(The

sed -i 's/hello/world/' file.txt
994 characters may be uniformly replaced by any other single character within any given
sed -i 's/hello/world/' file.txt
183 command. )

Instances of the

sed -i 's/hello/world/' file.txt
994 (or whatever other character is used in its stead),
sed -i 's/hello/world/' file.txt
997, or newlines can appear in the source-chars or dest-chars lists, provide that each instance is escaped by a
sed -i 's/hello/world/' file.txt
997. The source-chars and dest-chars lists must contain the same number of characters (after de-escaping)

See the

sed -i 's/hello/world/' file.txt
187 command from GNU coreutils for similar functionality

sed -i 's/hello/world/' file.txt
933

Appending text after a line. This is a GNU extension to the standard

sed -i 's/hello/world/' file.txt
926 command - see below for details

Example. Add the word ‘hello’ after the second line

sed -i 's/hello/world/' file.txt
06

Leading whitespace after the

sed -i 's/hello/world/' file.txt
926 command is ignored. The text to add is read until the end of the line

sed -i 's/hello/world/' file.txt
931
sed -i 's/hello/world/' file.txt
932

Appending text after a line

Example. Add ‘hello’ after the second line (-. indicates printed output lines)

sed -i 's/hello/world/' file.txt
07

The

sed -i 's/hello/world/' file.txt
926 command queues the lines of text which follow this command (each but the last ending with a
sed -i 's/hello/world/' file.txt
997, which are removed from the output) to be output at the end of the current cycle, or when the next input line is read

As a GNU extension, this command accepts two addresses

Trình tự thoát trong văn bản được xử lý, vì vậy bạn nên sử dụng

sed -i 's/hello/world/' file.txt
195 trong văn bản để in một dấu gạch chéo ngược

The commands resume after the last line without a backslash (

sed -i 's/hello/world/' file.txt
997) - ‘world’ in the following example

sed -i 's/hello/world/' file.txt
08

As a GNU extension, the

sed -i 's/hello/world/' file.txt
926 command and text can be separated into two
sed -i 's/hello/world/' file.txt
198 parameters, enabling easier scripting

sed -i 's/hello/world/' file.txt
09

sed -i 's/hello/world/' file.txt
950

insert text before a line. This is a GNU extension to the standard

sed -i 's/hello/world/' file.txt
928 command - see below for details

Example. Insert the word ‘hello’ before the second line

sed -i 's/hello/world/' file.txt
0

Leading whitespace after the

sed -i 's/hello/world/' file.txt
928 command is ignored. The text to add is read until the end of the line

sed -i 's/hello/world/' file.txt
948
sed -i 's/hello/world/' file.txt
932

Immediately output the lines of text which follow this command

Example. Insert ‘hello’ before the second line (-. indicates printed output lines)

sed -i 's/hello/world/' file.txt
1

As a GNU extension, this command accepts two addresses

Trình tự thoát trong văn bản được xử lý, vì vậy bạn nên sử dụng

sed -i 's/hello/world/' file.txt
195 trong văn bản để in một dấu gạch chéo ngược

The commands resume after the last line without a backslash (

sed -i 's/hello/world/' file.txt
997) - ‘world’ in the following example

sed -i 's/hello/world/' file.txt
2

As a GNU extension, the

sed -i 's/hello/world/' file.txt
928 command and text can be separated into two
sed -i 's/hello/world/' file.txt
198 parameters, enabling easier scripting

sed -i 's/hello/world/' file.txt
3

sed -i 's/hello/world/' file.txt
937

Replaces the line(s) with text. This is a GNU extension to the standard

sed -i 's/hello/world/' file.txt
927 command - see below for details

Thí dụ. Thay thế các dòng thứ 2 đến thứ 9 bằng từ 'xin chào'

sed -i 's/hello/world/' file.txt
4

Khoảng trắng hàng đầu sau lệnh

sed -i 's/hello/world/' file.txt
927 bị bỏ qua. Văn bản cần thêm được đọc cho đến cuối dòng

sed -i 's/hello/world/' file.txt
935
sed -i 's/hello/world/' file.txt
932

Xóa các dòng khớp với địa chỉ hoặc dải địa chỉ và xuất các dòng văn bản tuân theo lệnh này

Thí dụ. Replace 2nd to 4th lines with the words ‘hello’ and ‘world’ (-. cho biết các dòng đầu ra được in)

sed -i 's/hello/world/' file.txt
5

Nếu không có địa chỉ nào được cung cấp, mỗi dòng sẽ được thay thế

Một chu kỳ mới được bắt đầu sau khi lệnh này được thực hiện, vì không gian mẫu sẽ bị xóa. Trong ví dụ sau,

sed -i 's/hello/world/' file.txt
927 bắt đầu một chu kỳ mới và lệnh thay thế không được thực hiện trên văn bản đã thay thế

sed -i 's/hello/world/' file.txt
6

As a GNU extension, the

sed -i 's/hello/world/' file.txt
927 command and text can be separated into two
sed -i 's/hello/world/' file.txt
198 parameters, enabling easier scripting

sed -i 's/hello/world/' file.txt
7

sed -i 's/hello/world/' file.txt
980

Print out the current input line number (with a trailing newline)

sed -i 's/hello/world/' file.txt
8

As a GNU extension, this command accepts two addresses

sed -i 's/hello/world/' file.txt
317

Print the pattern space in an unambiguous form. non-printable characters (and the

sed -i 's/hello/world/' file.txt
997 character) are printed in C-style escaped form; long lines are split, with a trailing
sed -i 's/hello/world/' file.txt
997 character to indicate the split; the end of each line is marked with a
sed -i 's/hello/world/' file.txt
87

n specifies the desired line-wrap length; a length of 0 (zero) means to never wrap long lines. If omitted, the default as specified on the command line is used. Tham số n là phần mở rộng GNU

sed -i 's/hello/world/' file.txt
22

sed -i 's/hello/world/' file.txt
963

Reads file filename. Example

sed -i 's/hello/world/' file.txt
9

Queue the contents of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that if filename cannot be read, it is treated as if it were an empty file, without any error indication

As a GNU

sed -i 's/hello/world/' file.txt
22 extension, the special value /dev/stdin is supported for the file name, which reads the contents of the standard input

As a GNU extension, this command accepts two addresses. The file will then be reread and inserted on each of the addressed lines

sed -i 's/hello/world/' file.txt
973

Write the pattern space to filename. As a GNU

sed -i 's/hello/world/' file.txt
22 extension, two special values of filename are supported. /dev/stderr, which writes the result to the standard error, and /dev/stdout, which writes to the standard output. 4

The file will be created (or truncated) before the first input line is read; all

sed -i 's/hello/world/' file.txt
326 commands (including instances of the
sed -i 's/hello/world/' file.txt
326 flag on successful
sed -i 's/hello/world/' file.txt
967 commands) which refer to the same filename are output without closing and reopening the file

sed -i 's/hello/world/' file.txt
939

If pattern space contains no newline, start a normal new cycle as if the

sed -i 's/hello/world/' file.txt
36 command was issued. Otherwise, delete text in the pattern space up to the first newline, and restart cycle with the resultant pattern space, without reading a new line of input

sed -i 's/hello/world/' file.txt
71

Thêm một dòng mới vào không gian mẫu, sau đó nối dòng đầu vào tiếp theo vào không gian mẫu. Nếu không có thêm đầu vào thì

sed -i 's/hello/world/' file.txt
22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào

When -z is used, a zero byte (the ascii ‘NUL’ character) is added between the lines (instead of a new line)

By default

sed -i 's/hello/world/' file.txt
22 does not terminate if there is no ’next’ input line. This is a GNU extension which can be disabled with --posix. See N command on the last line

sed -i 's/hello/world/' file.txt
957

Print out the portion of the pattern space up to the first newline

sed -i 's/hello/world/' file.txt
946

Replace the contents of the hold space with the contents of the pattern space

sed -i 's/hello/world/' file.txt
947

Nối một dòng mới vào nội dung của không gian giữ, sau đó nối thêm nội dung của không gian mẫu vào nội dung của không gian giữ

sed -i 's/hello/world/' file.txt
944

Thay thế nội dung của không gian mẫu bằng nội dung của không gian giữ

sed -i 's/hello/world/' file.txt
945

Nối một dòng mới vào nội dung của không gian mẫu, rồi nối thêm nội dung của không gian giữ vào nội dung của không gian mẫu

sed -i 's/hello/world/' file.txt
975

Trao đổi nội dung của không gian giữ và mẫu


3. 6 Commands for sed -i 's/hello/world/' file.txt 22 gurus

In most cases, use of these commands indicates that you are probably better off programming in something like

sed -i 's/hello/world/' file.txt
341 or Perl. But occasionally one is committed to sticking with
sed -i 's/hello/world/' file.txt
22, and these commands can enable one to write quite convoluted scripts

sed -i 's/hello/world/' file.txt
981

[Không cho phép địa chỉ. ]

Specify the location of label for branch commands. In all other respects, a no-op

sed -i 's/hello/world/' file.txt
934

Unconditionally branch to label. The label may be omitted, in which case the next cycle is started

sed -i 's/hello/world/' file.txt
966

Branch to label only if there has been a successful

sed -i 's/hello/world/' file.txt
967ubstitution since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started


3. 7 Commands Specific to GNU sed -i 's/hello/world/' file.txt 22

These commands are specific to GNU

sed -i 's/hello/world/' file.txt
22, so you must use them with care and only when you are sure that hindering portability is not evil. They allow you to check for GNU
sed -i 's/hello/world/' file.txt
22 extensions or to do tasks that are required quite often, yet are unsupported by standard
sed -i 's/hello/world/' file.txt
22s

sed -i 's/hello/world/' file.txt
351

Lệnh này cho phép một người chuyển đầu vào từ lệnh shell sang không gian mẫu. Without parameters, the

sed -i 's/hello/world/' file.txt
941 command executes the command that is found in pattern space and replaces the pattern space with the output; a trailing newline is suppressed

If a parameter is specified, instead, the

sed -i 's/hello/world/' file.txt
941 command interprets it as a command and sends its output to the output stream. The command can run across multiple lines, all but the last ending with a back-slash

Trong cả hai trường hợp, kết quả không được xác định nếu lệnh được thực thi chứa ký tự NUL

Lưu ý rằng, không giống như lệnh

sed -i 's/hello/world/' file.txt
354, đầu ra của lệnh sẽ được in ngay lập tức;

sed -i 's/hello/world/' file.txt
943

In ra tên tệp của tệp đầu vào hiện tại (với một dòng mới ở cuối)

sed -i 's/hello/world/' file.txt
357

This command accepts only one address

This command is the same as

sed -i 's/hello/world/' file.txt
900, but will not print the contents of pattern space. Like
sed -i 's/hello/world/' file.txt
900, it provides the ability to return an exit code to the caller

Lệnh này có thể hữu ích vì các cách thay thế duy nhất để thực hiện chức năng có vẻ tầm thường này là sử dụng tùy chọn -n (có thể làm phức tạp tập lệnh của bạn một cách không cần thiết) hoặc sử dụng đoạn mã sau, điều này làm lãng phí thời gian bằng cách đọc toàn bộ tệp mà không có bất kỳ hiệu ứng rõ ràng nào

sed -i 's/hello/world/' file.txt
90

sed -i 's/hello/world/' file.txt
964

Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that if filename cannot be read, or if its end is reached, no line is appended, without any error indication

As with the

sed -i 's/hello/world/' file.txt
354 command, the special value /dev/stdin is supported for the file name, which reads a line from the standard input

sed -i 's/hello/world/' file.txt
968

Branch to label only if there have been no successful

sed -i 's/hello/world/' file.txt
967ubstitutions since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started

sed -i 's/hello/world/' file.txt
364

This command does nothing, but makes

sed -i 's/hello/world/' file.txt
22 fail if GNU
sed -i 's/hello/world/' file.txt
22 extensions are not supported, simply because other versions of
sed -i 's/hello/world/' file.txt
22 do not implement it. In addition, you can specify the version of
sed -i 's/hello/world/' file.txt
22 that your script requires, such as
sed -i 's/hello/world/' file.txt
369. The default is
sed -i 's/hello/world/' file.txt
370 because that is the first version that implemented this command

This command enables all GNU extensions even if

sed -i 's/hello/world/' file.txt
72 is set in the environment

sed -i 's/hello/world/' file.txt
974

Write to the given filename the portion of the pattern space up to the first newline. Everything said under the

sed -i 's/hello/world/' file.txt
326 command about file handling holds here too

sed -i 's/hello/world/' file.txt
977

This command empties the content of pattern space. It is usually the same as ‘s/. *//’, but is more efficient and works in the presence of invalid multibyte sequences in the input stream. POSIX mandates that such sequences are not matched by ‘. ’, so that there is no portable way to clear

sed -i 's/hello/world/' file.txt
22’s buffers in the middle of the script in most multibyte locales (including UTF-8 locales)


3. 8 Multiple commands syntax

There are several methods to specify multiple commands in a

sed -i 's/hello/world/' file.txt
22 program

Using newlines is most natural when running a sed script from a file (using the -f option)

On the command line, all

sed -i 's/hello/world/' file.txt
22 commands may be separated by newlines. Alternatively, you may specify each command as an argument to an -e option

sed -i 's/hello/world/' file.txt
91

A semicolon (‘;’) may be used to separate most simple commands

sed -i 's/hello/world/' file.txt
92

The

sed -i 's/hello/world/' file.txt
178,
sed -i 's/hello/world/' file.txt
179,
sed -i 's/hello/world/' file.txt
982,
sed -i 's/hello/world/' file.txt
983,
sed -i 's/hello/world/' file.txt
984,
sed -i 's/hello/world/' file.txt
383 commands can be separated with a semicolon (this is a non-portable GNU
sed -i 's/hello/world/' file.txt
22 extension)

sed -i 's/hello/world/' file.txt
93

Labels used in

sed -i 's/hello/world/' file.txt
982,
sed -i 's/hello/world/' file.txt
983,
sed -i 's/hello/world/' file.txt
984,
sed -i 's/hello/world/' file.txt
383 commands are read until a semicolon. Leading and trailing whitespace is ignored. In the examples below the label is ‘x’. The first example works with GNU
sed -i 's/hello/world/' file.txt
22. The second is a portable equivalent. For more information about branching and labels see Branching and flow control

sed -i 's/hello/world/' file.txt
94

3. 8. 1 Commands Requiring a newline

The following commands cannot be separated by a semicolon and require a newline

sed -i 's/hello/world/' file.txt
926,
sed -i 's/hello/world/' file.txt
927,
sed -i 's/hello/world/' file.txt
928 (append/change/insert)

All characters following

sed -i 's/hello/world/' file.txt
926,
sed -i 's/hello/world/' file.txt
927,
sed -i 's/hello/world/' file.txt
928 commands are taken as the text to append/change/insert. Using a semicolon leads to undesirable results

sed -i 's/hello/world/' file.txt
95

Separate the commands using -e or a newline

sed -i 's/hello/world/' file.txt
96

Note that specifying the text to add (‘Hello’) immediately after

sed -i 's/hello/world/' file.txt
926,
sed -i 's/hello/world/' file.txt
927,
sed -i 's/hello/world/' file.txt
928 is itself a GNU
sed -i 's/hello/world/' file.txt
22 extension. A portable, POSIX-compliant alternative is

sed -i 's/hello/world/' file.txt
97

sed -i 's/hello/world/' file.txt
978 (comment)

All characters following ‘#’ until the next newline are ignored

sed -i 's/hello/world/' file.txt
98

sed -i 's/hello/world/' file.txt
354,
sed -i 's/hello/world/' file.txt
88,
sed -i 's/hello/world/' file.txt
326,
sed -i 's/hello/world/' file.txt
33 (reading and writing files)

The

sed -i 's/hello/world/' file.txt
354,
sed -i 's/hello/world/' file.txt
88,
sed -i 's/hello/world/' file.txt
326,
sed -i 's/hello/world/' file.txt
33 commands parse the filename until end of the line. If whitespace, comments or semicolons are found, they will be included in the filename, leading to unexpected results

sed -i 's/hello/world/' file.txt
99

Note that

sed -i 's/hello/world/' file.txt
22 silently ignores read/write errors in
sed -i 's/hello/world/' file.txt
354,
sed -i 's/hello/world/' file.txt
88,
sed -i 's/hello/world/' file.txt
326,
sed -i 's/hello/world/' file.txt
33 commands (such as missing files). In the following example,
sed -i 's/hello/world/' file.txt
22 tries to read a file named ‘hello. txt ; N’. The file is missing, and the error is silently ignored

sed -i 's/hello/world/' file.txt
10

sed -i 's/hello/world/' file.txt
941 (command execution)

Any characters following the

sed -i 's/hello/world/' file.txt
941 command until the end of the line will be sent to the shell. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào lệnh trình bao, dẫn đến kết quả không mong muốn

sed -i 's/hello/world/' file.txt
11

sed -i 's/hello/world/' file.txt
617 (substitute with
sed -i 's/hello/world/' file.txt
941 or
sed -i 's/hello/world/' file.txt
326 flags)

In a substitution command, the

sed -i 's/hello/world/' file.txt
326 flag writes the substitution result to a file, and the
sed -i 's/hello/world/' file.txt
941 flag executes the subsitution result as a shell command. As with the
sed -i 's/hello/world/' file.txt
622 commands, these must be terminated with a newline. If whitespace, comments or semicolons are found, they will be included in the shell command or filename, leading to unexpected results

sed -i 's/hello/world/' file.txt
12


4 Addresses. selecting lines


4. 1 Addresses overview

Addresses determine on which line(s) the

sed -i 's/hello/world/' file.txt
22 command will be executed. The following command replaces the word ‘hello’ with ‘world’ only on line 144

sed -i 's/hello/world/' file.txt
13

If no addresses are given, the command is performed on all lines. The following command replaces the word ‘hello’ with ‘world’ on all lines in the input file

sed 's/hello/world/' input.txt > output.txt

Addresses can contain regular expressions to match lines based on content instead of line numbers. The following command replaces the word ‘hello’ with ‘world’ only in lines containing the word ‘apple’

sed -i 's/hello/world/' file.txt
15

An address range is specified with two addresses separated by a comma (

sed -i 's/hello/world/' file.txt
624). Addresses can be numeric, regular expressions, or a mix of both. The following command replaces the word ‘hello’ with ‘world’ only in lines 4 to 17 (inclusive)

sed -i 's/hello/world/' file.txt
16

Appending the

sed -i 's/hello/world/' file.txt
625 character to the end of an address specification (before the command letter) negates the sense of the match. That is, if the
sed -i 's/hello/world/' file.txt
625 character follows an address or an address range, then only lines which do not match the addresses will be selected. The following command replaces the word ‘hello’ with ‘world’ only in lines not containing the word ‘apple’

sed -i 's/hello/world/' file.txt
17

The following command replaces the word ‘hello’ with ‘world’ only in lines 1 to 3 and 18 till the last line of the input file (i. e. excluding lines 4 to 17)

sed -i 's/hello/world/' file.txt
18


4. 2 Chọn dòng theo số

Addresses in a

sed -i 's/hello/world/' file.txt
22 script can be in any of the following forms

sed -i 's/hello/world/' file.txt
128

Specifying a line number will match only that line in the input. (Note that

sed -i 's/hello/world/' file.txt
22 counts lines continuously across all input files unless -i or -s options are specified. )

sed -i 's/hello/world/' file.txt
87

This address matches the last line of the last file of input, or the last line of each file when the -i or -s options are specified

sed -i 's/hello/world/' file.txt
631

This GNU extension matches every stepth line starting with line first. In particular, lines will be selected when there exists a non-negative n such that the current line-number equals first + (n * step). Thus, one would use

sed -i 's/hello/world/' file.txt
632 to select the odd-numbered lines and
sed -i 's/hello/world/' file.txt
633 for even-numbered lines; to pick every third line starting with the second, ‘2~3’ would be used; to pick every fifth line starting with the tenth, use ‘10~5’; and ‘50~0’ is just an obscure way of saying
sed -i 's/hello/world/' file.txt
634

The following commands demonstrate the step address usage

sed -i 's/hello/world/' file.txt
19


4. 3 selecting lines by text matching

GNU

sed -i 's/hello/world/' file.txt
22 supports the following regular expression addresses. The default regular expression is Basic Regular Expression (BRE). If -E or -r options are used, The regular expression should be in Extended Regular Expression (ERE) syntax. See BRE vs ERE

sed -i 's/hello/world/' file.txt
636

This will select any line which matches the regular expression regexp. If regexp itself includes any

sed -i 's/hello/world/' file.txt
994 characters, each must be escaped by a backslash (
sed -i 's/hello/world/' file.txt
997)

The following command prints lines in /etc/passwd which end with ‘bash’5

sed -i 's/hello/world/' file.txt
30

The empty regular expression ‘//’ repeats the last regular expression match (the same holds if the empty regular expression is passed to the

sed -i 's/hello/world/' file.txt
967 command). Note that modifiers to regular expressions are evaluated when the regular expression is compiled, thus it is invalid to specify them together with the empty regular expression

sed -i 's/hello/world/' file.txt
640

(The

sed -i 's/hello/world/' file.txt
641 may be replaced by any other single character. )

This also matches the regular expression regexp, but allows one to use a different delimiter than

sed -i 's/hello/world/' file.txt
994. This is particularly useful if the regexp itself contains a lot of slashes, since it avoids the tedious escaping of every
sed -i 's/hello/world/' file.txt
994. If regexp itself includes any delimiter characters, each must be escaped by a backslash (
sed -i 's/hello/world/' file.txt
997)

The following commands are equivalent. They print lines which start with ‘/home/alice/documents/’

sed -i 's/hello/world/' file.txt
31

sed -i 's/hello/world/' file.txt
645
sed -i 's/hello/world/' file.txt
646

The

sed -i 's/hello/world/' file.txt
145 modifier to regular-expression matching is a GNU extension which causes the regexp to be matched in a case-insensitive manner

In many other programming languages, a lower case

sed -i 's/hello/world/' file.txt
928 is used for case-insensitive regular expression matching. However, in
sed -i 's/hello/world/' file.txt
22 the
sed -i 's/hello/world/' file.txt
928 is used for the insert command (see insert command)

Observe the difference between the following examples

Trong ví dụ này,

sed -i 's/hello/world/' file.txt
651 là địa chỉ. biểu thức chính quy với công cụ sửa đổi
sed -i 's/hello/world/' file.txt
145.
sed -i 's/hello/world/' file.txt
36 là lệnh xóa

sed -i 's/hello/world/' file.txt
32

Đây,

sed -i 's/hello/world/' file.txt
654 là địa chỉ. một biểu thức chính quy.
sed -i 's/hello/world/' file.txt
928 là lệnh chèn.
sed -i 's/hello/world/' file.txt
36 là giá trị cần chèn. Sau đó, một dòng có 'd' được chèn phía trên dòng phù hợp

sed -i 's/hello/world/' file.txt
33

sed -i 's/hello/world/' file.txt
657
sed -i 's/hello/world/' file.txt
658

Công cụ sửa đổi

sed -i 's/hello/world/' file.txt
149 để khớp biểu thức chính quy là một tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt
22 hướng GNU
sed -i 's/hello/world/' file.txt
22 khớp biểu thức chính quy ở chế độ nhiều dòng. Công cụ sửa đổi làm cho
sed -i 's/hello/world/' file.txt
154 và
sed -i 's/hello/world/' file.txt
87 khớp tương ứng (ngoài hành vi bình thường) chuỗi trống sau một dòng mới và chuỗi trống trước một dòng mới. Có các chuỗi ký tự đặc biệt (
sed -i 's/hello/world/' file.txt
156 và
sed -i 's/hello/world/' file.txt
157) luôn khớp với phần đầu hoặc phần cuối của bộ đệm. Ngoài ra, ký tự dấu chấm không khớp với ký tự dòng mới ở chế độ nhiều dòng

Địa chỉ regex hoạt động trên nội dung của không gian mẫu hiện tại. Nếu không gian mẫu bị thay đổi (ví dụ: với lệnh

sed -i 's/hello/world/' file.txt
666), khớp biểu thức chính quy sẽ hoạt động trên văn bản đã thay đổi

Trong ví dụ sau, in tự động bị tắt với -n. Lệnh

sed -i 's/hello/world/' file.txt
667 thay đổi các dòng chứa '2' thành 'X'. Lệnh
sed -i 's/hello/world/' file.txt
668 khớp các dòng có chữ số và in chúng. Bởi vì dòng thứ hai được thay đổi trước biểu thức chính quy
sed -i 's/hello/world/' file.txt
669, nó sẽ không khớp và sẽ không được in

sed -i 's/hello/world/' file.txt
34


4. 4 dãy địa chỉ

Có thể chỉ định phạm vi địa chỉ bằng cách chỉ định hai địa chỉ được phân tách bằng dấu phẩy (

sed -i 's/hello/world/' file.txt
624). Một dải địa chỉ khớp với các dòng bắt đầu từ nơi địa chỉ đầu tiên khớp và tiếp tục cho đến khi địa chỉ thứ hai khớp (bao gồm)

sed -i 's/hello/world/' file.txt
35

Nếu địa chỉ thứ hai là biểu thức chính quy, thì việc kiểm tra kết quả khớp sẽ bắt đầu bằng dòng theo sau dòng khớp với địa chỉ đầu tiên. một phạm vi sẽ luôn kéo dài ít nhất hai dòng (tất nhiên là ngoại trừ nếu luồng đầu vào kết thúc)

sed -i 's/hello/world/' file.txt
36

Nếu địa chỉ thứ hai là một số nhỏ hơn (hoặc bằng) dòng khớp với địa chỉ đầu tiên, thì chỉ có một dòng được khớp

sed -i 's/hello/world/' file.txt
37

GNU

sed -i 's/hello/world/' file.txt
22 cũng hỗ trợ một số biểu mẫu hai địa chỉ đặc biệt;

sed -i 's/hello/world/' file.txt
672

Một số dòng của

sed -i 's/hello/world/' file.txt
673 có thể được sử dụng trong một đặc tả địa chỉ như
sed -i 's/hello/world/' file.txt
672 để
sed -i 's/hello/world/' file.txt
22 cũng sẽ cố gắng khớp với biểu thức chính quy trong dòng đầu vào đầu tiên. Nói cách khác,
sed -i 's/hello/world/' file.txt
672 tương tự như
sed -i 's/hello/world/' file.txt
677, ngoại trừ việc nếu addr2 khớp với dòng đầu tiên của đầu vào, biểu mẫu
sed -i 's/hello/world/' file.txt
672 sẽ coi nó là kết thúc phạm vi, trong khi biểu mẫu
sed -i 's/hello/world/' file.txt
677 sẽ khớp với phần đầu của phạm vi và do đó làm cho phạm vi mở rộng

Lưu ý rằng đây là nơi duy nhất mà địa chỉ

sed -i 's/hello/world/' file.txt
673 có ý nghĩa;

Các ví dụ sau minh họa sự khác biệt giữa bắt đầu bằng địa chỉ 1 và 0

sed -i 's/hello/world/' file.txt
38

sed -i 's/hello/world/' file.txt
682

So khớp addr1 và N dòng sau addr1

sed -i 's/hello/world/' file.txt
39

addr1 có thể là số dòng hoặc biểu thức chính quy

sed -i 's/hello/world/' file.txt
683

So khớp addr1 và các dòng theo sau addr1 cho đến dòng tiếp theo có số dòng đầu vào là bội số của N. Lệnh sau in bắt đầu từ dòng 6, cho đến dòng tiếp theo là bội số của 4 (i. e. dòng 8)

sed -i 's/hello/world/' file.txt
60

addr1 có thể là số dòng hoặc biểu thức chính quy


5 biểu thức chính quy. chọn văn bản


5. 1 Tổng quan về biểu thức chính quy trong sed -i 's/hello/world/' file.txt 22

Để biết cách sử dụng

sed -i 's/hello/world/' file.txt
22, mọi người nên hiểu biểu thức chính quy (viết tắt là regexp). Biểu thức chính quy là một mẫu được khớp với chuỗi chủ đề từ trái sang phải. Hầu hết các nhân vật đều bình thường. chúng đại diện cho chính chúng theo một mẫu và khớp với các ký tự tương ứng. Biểu thức chính quy trong
sed -i 's/hello/world/' file.txt
22 được chỉ định giữa hai dấu gạch chéo

Lệnh sau in các dòng có chứa từ 'hello'

Ví dụ trên tương đương với lệnh

sed -i 's/hello/world/' file.txt
687 này

Sức mạnh của biểu thức chính quy đến từ khả năng bao gồm các lựa chọn thay thế và lặp lại trong mẫu. Chúng được mã hóa trong mẫu bằng cách sử dụng các ký tự đặc biệt, không đại diện cho chính chúng mà thay vào đó được diễn giải theo một cách đặc biệt nào đó

Ký tự

sed -i 's/hello/world/' file.txt
154 (dấu mũ) trong biểu thức chính quy khớp với đầu dòng. Ký tự
sed -i 's/hello/world/' file.txt
689 (dấu chấm) khớp với bất kỳ ký tự đơn nào. Lệnh
sed -i 's/hello/world/' file.txt
22 sau khớp và in các dòng bắt đầu bằng chữ 'b', theo sau là bất kỳ ký tự đơn nào, tiếp theo là chữ 'd'

sed -i 's/hello/world/' file.txt
61

Các phần sau giải thích ý nghĩa và cách sử dụng các ký tự đặc biệt trong biểu thức chính quy


5. 2 Biểu thức chính quy cơ bản (BRE) và mở rộng (ERE)

Biểu thức chính quy cơ bản và mở rộng là hai biến thể về cú pháp của mẫu đã chỉ định. Cú pháp Biểu thức chính quy cơ bản (BRE) là mặc định trong

sed -i 's/hello/world/' file.txt
22 (và tương tự như vậy trong
sed -i 's/hello/world/' file.txt
687). Sử dụng tùy chọn -E do POSIX chỉ định (-r, --regexp-extended) để bật cú pháp Biểu thức chính quy mở rộng (ERE)

Trong GNU

sed -i 's/hello/world/' file.txt
22, sự khác biệt duy nhất giữa biểu thức chính quy cơ bản và mở rộng là ở hành vi của một vài ký tự đặc biệt. ‘?’, ‘+’, dấu ngoặc đơn, dấu ngoặc nhọn (‘{}’) và ‘. ’

Với cú pháp cơ bản (BRE), các ký tự này không có ý nghĩa đặc biệt trừ khi được đặt trước bằng dấu gạch chéo ngược ('\'); . các ký tự này là đặc biệt trừ khi chúng có tiền tố là dấu gạch chéo ngược ('\')

Mẫu mong muốn Cơ bản (BRE) Cú phápMở rộng (ERE) Cú phápChữ ‘+’ (dấu cộng)

sed -i 's/hello/world/' file.txt
62

sed -i 's/hello/world/' file.txt
63

Một hoặc nhiều ký tự 'a' theo sau là 'b' (dấu cộng là siêu ký tự đặc biệt)

sed -i 's/hello/world/' file.txt
64

sed -i 's/hello/world/' file.txt
65


5. 3 Tổng quan về cú pháp biểu thức chính quy cơ bản

Đây là một mô tả ngắn gọn về cú pháp biểu thức chính quy như được sử dụng trong

sed -i 's/hello/world/' file.txt
22

sed -i 's/hello/world/' file.txt
695

Một ký tự thông thường duy nhất khớp với chính nó

sed -i 's/hello/world/' file.txt
60

Matches a sequence of zero or more instances of matches for the preceding regular expression, which must be an ordinary character, a special character preceded by

sed -i 's/hello/world/' file.txt
997, a
sed -i 's/hello/world/' file.txt
689, a grouped regexp (see below), or a bracket expression. Là một phần mở rộng của GNU, một biểu thức chính quy có hậu tố cũng có thể được theo sau bởi
sed -i 's/hello/world/' file.txt
60; . VỊ TRÍ 1003. 1-2001 nói rằng
sed -i 's/hello/world/' file.txt
60 là viết tắt của chính nó khi nó xuất hiện ở đầu biểu thức chính quy hoặc biểu thức con, nhưng nhiều triển khai nonGNU không hỗ trợ điều này và thay vào đó, các tập lệnh di động nên sử dụng
sed -i 's/hello/world/' file.txt
903 trong các ngữ cảnh này

sed -i 's/hello/world/' file.txt
689

Khớp với bất kỳ ký tự nào, bao gồm cả dòng mới

sed -i 's/hello/world/' file.txt
154

So khớp với chuỗi null ở đầu không gian mẫu, i. e. những gì xuất hiện sau dấu mũ phải xuất hiện ở đầu không gian mẫu

Trong hầu hết các tập lệnh, không gian mẫu được khởi tạo cho nội dung của mỗi dòng (xem Cách thức hoạt động của

sed -i 's/hello/world/' file.txt
22). Vì vậy, sẽ là một cách đơn giản hóa hữu ích khi coi
sed -i 's/hello/world/' file.txt
907 chỉ khớp với các dòng trong đó '#include' là điều đầu tiên trên dòng—ví dụ: nếu có khoảng trắng trước đó thì khớp không thành công. Sự đơn giản hóa này có hiệu lực miễn là nội dung ban đầu của không gian mẫu không bị sửa đổi, ví dụ như với lệnh
sed -i 's/hello/world/' file.txt
967

sed -i 's/hello/world/' file.txt
154 chỉ hoạt động như một ký tự đặc biệt ở đầu biểu thức chính quy hoặc biểu thức con (nghĩa là sau
sed -i 's/hello/world/' file.txt
991 hoặc
sed -i 's/hello/world/' file.txt
911). Tuy nhiên, các tập lệnh di động nên tránh
sed -i 's/hello/world/' file.txt
154 ở đầu biểu thức con, vì POSIX cho phép triển khai coi
sed -i 's/hello/world/' file.txt
154 là một ký tự thông thường trong ngữ cảnh đó

sed -i 's/hello/world/' file.txt
87

Nó giống như

sed -i 's/hello/world/' file.txt
154, nhưng đề cập đến phần cuối của không gian mẫu.
sed -i 's/hello/world/' file.txt
87 cũng chỉ hoạt động như một ký tự đặc biệt ở cuối biểu thức chính quy hoặc biểu thức con (nghĩa là trước
sed -i 's/hello/world/' file.txt
992 hoặc
sed -i 's/hello/world/' file.txt
911) và việc sử dụng nó ở cuối biểu thức con là không khả dụng

sed -i 's/hello/world/' file.txt
919
sed -i 's/hello/world/' file.txt
920

Khớp với bất kỳ ký tự đơn nào trong danh sách. ví dụ:

sed -i 's/hello/world/' file.txt
921 khớp với tất cả các nguyên âm. Một danh sách có thể bao gồm các chuỗi như
sed -i 's/hello/world/' file.txt
922, khớp với bất kỳ ký tự nào giữa (bao gồm) char1 và char2. Xem Các lớp ký tự và Biểu thức khung

sed -i 's/hello/world/' file.txt
923

sed -i 's/hello/world/' file.txt
60, nhưng khớp với một hoặc nhiều. Nó là một phần mở rộng của GNU

sed -i 's/hello/world/' file.txt
925

As

sed -i 's/hello/world/' file.txt
60, but only matches zero or one. Nó là một phần mở rộng của GNU

sed -i 's/hello/world/' file.txt
927

sed -i 's/hello/world/' file.txt
60, nhưng khớp chính xác với i chuỗi (i là số nguyên thập phân; để dễ mang theo, hãy giữ nó trong khoảng từ 0 đến 255)

sed -i 's/hello/world/' file.txt
929

Các trận đấu giữa i và j, bao gồm, trình tự

sed -i 's/hello/world/' file.txt
930

So khớp nhiều hơn hoặc bằng i chuỗi

sed -i 's/hello/world/' file.txt
931

Nhóm toàn bộ biểu thức chính quy bên trong, điều này được sử dụng để

  • Áp dụng các toán tử hậu tố, như
    sed -i 's/hello/world/' file.txt
    
    932. this will search for zero or more whole sequences of ‘abcd’, while
    sed -i 's/hello/world/' file.txt
    
    933 would search for ‘abc’ followed by zero or more occurrences of ‘d’. Lưu ý rằng POSIX 1003 yêu cầu hỗ trợ cho
    sed -i 's/hello/world/' file.txt
    
    932. 1-2001, nhưng nhiều triển khai không phải GNU không hỗ trợ nó và do đó nó không thể di động phổ biến
  • Sử dụng tài liệu tham khảo trở lại (xem bên dưới)
sed -i 's/hello/world/' file.txt
935

Khớp với regexp1 hoặc regexp2. Sử dụng dấu ngoặc đơn để sử dụng các biểu thức chính quy thay thế phức tạp. Quá trình so khớp lần lượt thử từng phương án, từ trái sang phải và phương án đầu tiên thành công được sử dụng. Nó là một phần mở rộng của GNU

sed -i 's/hello/world/' file.txt
936

So khớp nối của regexp1 và regexp2. Phép nối liên kết chặt chẽ hơn

sed -i 's/hello/world/' file.txt
911,
sed -i 's/hello/world/' file.txt
154 và
sed -i 's/hello/world/' file.txt
87, nhưng kém chặt chẽ hơn so với các toán tử biểu thức chính quy khác

sed -i 's/hello/world/' file.txt
940

So khớp với biểu thức con được đặt trong ngoặc đơn thứ _________thứ ____ trong biểu thức chính quy. Điều này được gọi là một tài liệu tham khảo trở lại. Các biểu thức con được đánh số hoàn toàn bằng cách đếm số lần xuất hiện của

sed -i 's/hello/world/' file.txt
991 từ trái sang phải

sed -i 's/hello/world/' file.txt
990

Khớp với ký tự xuống dòng

sed -i 's/hello/world/' file.txt
944

So khớp char, trong đó char là một trong số ________ 087, ________ 060, ________ 3689, ________ 4948,

sed -i 's/hello/world/' file.txt
997 hoặc
sed -i 's/hello/world/' file.txt
154. Lưu ý rằng các chuỗi dấu gạch chéo ngược giống như chữ C duy nhất mà bạn có thể cho là được diễn giải là
sed -i 's/hello/world/' file.txt
990 và
sed -i 's/hello/world/' file.txt
195;

Lưu ý rằng trình so khớp cụm từ thông dụng là tham lam, tôi. e. , các trận đấu được thử từ trái sang phải và, nếu có thể có hai hoặc nhiều trận đấu bắt đầu từ cùng một ký tự, nó sẽ chọn trận đấu dài nhất

ví dụ

'abcdef'

Phù hợp với 'abcdef'

'a*b'

Khớp không hoặc nhiều chữ 'a' theo sau bởi một chữ 'b'. Ví dụ: 'b' hoặc 'aaaaab'

'a\?b'

Khớp 'b' hoặc 'ab'

'a\+b\+'

So khớp một hoặc nhiều chữ 'a' theo sau bởi một hoặc nhiều chữ 'b'. 'ab' là từ khớp ngắn nhất có thể, nhưng các ví dụ khác là 'aaaab' hoặc 'abbbbb' hoặc 'aaaaaabbbbbbbb'

‘. *''. \+’

Cả hai đều khớp với tất cả các ký tự trong một chuỗi;

'^ chính. *(. *)’

Điều này khớp với một chuỗi bắt đầu bằng 'main', theo sau là dấu ngoặc đơn mở và đóng. 'n', '(' và ')' không cần phải liền kề

'^#'

Điều này khớp với một chuỗi bắt đầu bằng '#'

‘\\$’

Điều này khớp với một chuỗi kết thúc bằng một dấu gạch chéo ngược. Biểu thức chính quy chứa hai dấu gạch chéo ngược để thoát

‘\$’

Thay vào đó, điều này phù hợp với một chuỗi bao gồm một ký hiệu đô la, bởi vì nó được thoát

‘[a-zA-Z0-9]’

Trong ngôn ngữ C, điều này khớp với bất kỳ chữ cái hoặc chữ số ASCII nào

‘[^ TAB]\+’

(Ở đây TAB là viết tắt của một ký tự tab đơn. ) Điều này khớp với một chuỗi gồm một hoặc nhiều ký tự, không có ký tự nào là khoảng trắng hoặc tab. Thông thường điều này có nghĩa là một từ

‘^\(. *\)\n\1$’

Điều này khớp với một chuỗi bao gồm hai chuỗi con bằng nhau được phân tách bằng một dòng mới

‘. \{9\}A$’

Điều này khớp với chín ký tự theo sau là chữ 'A' ở cuối dòng

‘^. \{15\}A’

Điều này khớp với phần đầu của một chuỗi chứa 16 ký tự, ký tự cuối cùng là 'A'


5. 4 Tổng quan về cú pháp biểu thức chính quy mở rộng

Sự khác biệt duy nhất giữa biểu thức chính quy cơ bản và mở rộng là trong hành vi của một vài ký tự. ‘?’, ‘+’, dấu ngoặc đơn, dấu ngoặc nhọn (‘{}’) và ‘. ’. Mặc dù các biểu thức chính quy cơ bản yêu cầu chúng phải được thoát nếu bạn muốn chúng hoạt động như các ký tự đặc biệt, nhưng khi sử dụng các biểu thức chính quy mở rộng, bạn phải thoát chúng nếu bạn muốn chúng khớp với một ký tự chữ. ‘. ’ is special here because ‘\. ’ is a GNU extension – standard basic regular expressions do not provide its functionality

ví dụ

sed -i 's/hello/world/' file.txt
955

trở thành 'abc\?' khi sử dụng các biểu thức chính quy mở rộng. Nó khớp với chuỗi ký tự 'abc?'

sed -i 's/hello/world/' file.txt
956

trở thành 'c+' khi sử dụng các biểu thức chính quy mở rộng. It matches one or more ‘c’s

sed -i 's/hello/world/' file.txt
957

trở thành 'a{3,}' khi sử dụng các biểu thức chính quy mở rộng. Nó phù hợp với ba hoặc nhiều chữ 'a'

sed -i 's/hello/world/' file.txt
958

trở thành '(abc){2,3}' khi sử dụng các biểu thức chính quy mở rộng. Nó khớp với 'abcabc' hoặc 'abcabcabc'

sed -i 's/hello/world/' file.txt
959

trở thành '(abc*)\1' khi sử dụng các biểu thức chính quy mở rộng. Backreferences vẫn phải được thoát khi sử dụng biểu thức chính quy mở rộng

sed -i 's/hello/world/' file.txt
960

becomes ‘a. b’ khi sử dụng các biểu thức chính quy mở rộng. Nó khớp với 'a' hoặc 'b'


5. 5 lớp ký tự và biểu thức khung

Biểu thức ngoặc là một danh sách các ký tự được bao quanh bởi '[' và ']'. Nó khớp với bất kỳ ký tự đơn nào trong danh sách đó; . Ví dụ: lệnh sau thay thế các từ 'grey' hoặc 'grey' bằng 'blue'

Biểu thức ngoặc có thể được sử dụng trong cả biểu thức chính quy cơ bản và mở rộng (nghĩa là có hoặc không có tùy chọn -E/-r)

Trong một biểu thức ngoặc, một biểu thức phạm vi bao gồm hai ký tự được phân tách bằng dấu gạch nối. Nó khớp với bất kỳ ký tự đơn nào sắp xếp giữa hai ký tự, bao gồm. Trong ngôn ngữ C mặc định, trình tự sắp xếp là thứ tự ký tự gốc;

Cuối cùng, một số lớp ký tự được đặt tên nhất định được xác định trước trong các biểu thức ngoặc, như sau

Các lớp được đặt tên này phải được sử dụng bên trong ngoặc đơn. Correct usage

sed -i 's/hello/world/' file.txt
66

Incorrect usage is rejected by newer

sed -i 's/hello/world/' file.txt
22 versions. Các phiên bản cũ hơn đã chấp nhận nó nhưng coi nó như một biểu thức ngoặc đơn (tương đương với '[dgit. ]’, nghĩa là chỉ có các ký tự d/g/i/t/. )

sed -i 's/hello/world/' file.txt
67

‘[. hàng năm. ]'

Ký tự chữ và số. ‘[. alpha. ]' và '[. chữ số. ]’;

‘[. chữ cái. ]'

Ký tự chữ cái. ‘[. thấp hơn. ]' và '[. phía trên. ]’;

‘[. trống. ]'

Ký tự trống. không gian và tab

‘[. cntrl. ]’

Ký tự điều khiển. In ASCII, these characters have octal codes 000 through 037, and 177 (DEL). Trong các bộ ký tự khác, đây là các ký tự tương đương, nếu có

‘[. chữ số. ]'

chữ số.

sed -i 's/hello/world/' file.txt
962

‘[. đồ thị. ]’

ký tự đồ họa. ‘[. hàng năm. ]' và '[. chấm câu. ]’

‘[. thấp hơn. ]'

Lower-case letters; in the ‘C’ locale and ASCII character encoding, this is

sed -i 's/hello/world/' file.txt
963

‘[. in. ]'

Printable characters. ‘[. hàng năm. ]’, ‘[. punct. ]', và dấu cách

‘[. chấm câu. ]'

Ký tự dấu câu;

‘[. không gian. ]'

Ký tự khoảng trắng. in the ‘C’ locale, this is tab, newline, vertical tab, form feed, carriage return, and space

‘[. upper. ]'

Chữ in hoa. trong ngôn ngữ 'C' và mã hóa ký tự ASCII, đây là

sed -i 's/hello/world/' file.txt
965

‘[. x chữ số. ]'

chữ số thập lục phân.

sed -i 's/hello/world/' file.txt
966

Lưu ý rằng dấu ngoặc trong các tên lớp này là một phần của tên tượng trưng và phải được bao gồm cùng với dấu ngoặc phân định biểu thức dấu ngoặc

Hầu hết các ký tự meta mất ý nghĩa đặc biệt của chúng bên trong các biểu thức ngoặc

‘]’

ends the bracket expression if it’s not the first list item. Vì vậy, nếu bạn muốn đặt ký tự ‘]’ thành một mục trong danh sách, bạn phải đặt ký tự này trước

'-'

đại diện cho phạm vi nếu nó không phải là đầu tiên hoặc cuối cùng trong danh sách hoặc điểm kết thúc của một phạm vi

'^'

đại diện cho các ký tự không có trong danh sách. Nếu bạn muốn biến ký tự '^' thành một mục trong danh sách, hãy đặt ký tự đó ở bất kỳ đâu nhưng trước tiên

LÀM. kết hợp đoạn này (sao chép nguyên văn từ phần BRE)

The characters

sed -i 's/hello/world/' file.txt
87,
sed -i 's/hello/world/' file.txt
60,
sed -i 's/hello/world/' file.txt
689,
sed -i 's/hello/world/' file.txt
948, and
sed -i 's/hello/world/' file.txt
997 are normally not special within list. Ví dụ:
sed -i 's/hello/world/' file.txt
972 khớp với ‘\’ hoặc ‘*’, vì
sed -i 's/hello/world/' file.txt
997 không có gì đặc biệt ở đây. Tuy nhiên, các chuỗi như
sed -i 's/hello/world/' file.txt
974,
sed -i 's/hello/world/' file.txt
975 và
sed -i 's/hello/world/' file.txt
976 là đặc biệt trong danh sách và đại diện cho các ký hiệu đối chiếu, lớp tương đương và lớp ký tự tương ứng, và do đó,
sed -i 's/hello/world/' file.txt
948 là đặc biệt trong danh sách khi nó được theo sau bởi
sed -i 's/hello/world/' file.txt
689,
sed -i 's/hello/world/' file.txt
980 hoặc
sed -i 's/hello/world/' file.txt
383. Also, when not in
sed -i 's/hello/world/' file.txt
72 mode, special escapes like
sed -i 's/hello/world/' file.txt
990 and
sed -i 's/hello/world/' file.txt
953 are recognized within list. Xem Thoát

‘[. ’

represents the open collating symbol

‘. ]'

represents the close collating symbol

'[='

đại diện cho lớp tương đương mở

'=]'

đại diện cho lớp tương đương gần

‘[. ’

represents the open character class symbol, and should be followed by a valid character class name

‘. ]'

đại diện cho ký hiệu lớp ký tự gần


5. 6 phần mở rộng biểu thức chính quy

Các chuỗi sau đây có ý nghĩa đặc biệt bên trong các biểu thức chính quy (được sử dụng trong địa chỉ và lệnh

sed -i 's/hello/world/' file.txt
967)

These can be used in both basic and extended regular expressions (that is, with or without the -E/-r options)

sed -i 's/hello/world/' file.txt
985

Khớp với bất kỳ ký tự "từ" nào. Ký tự “từ” là bất kỳ chữ cái hoặc chữ số hoặc ký tự gạch dưới

sed -i 's/hello/world/' file.txt
68

sed -i 's/hello/world/' file.txt
986

Khớp với bất kỳ ký tự "không phải từ" nào

sed -i 's/hello/world/' file.txt
69

sed -i 's/hello/world/' file.txt
987

Khớp với một ranh giới từ;

sed -i 's/hello/world/' file.txt
90

sed -i 's/hello/world/' file.txt
988

Khớp ở mọi nơi nhưng trên một ranh giới từ;

sed -i 's/hello/world/' file.txt
91

sed -i 's/hello/world/' file.txt
989

So khớp các ký tự khoảng trắng (dấu cách và tab). Newlines embedded in the pattern/hold spaces will also match

sed -i 's/hello/world/' file.txt
92

sed -i 's/hello/world/' file.txt
990

Matches non-whitespace characters

sed -i 's/hello/world/' file.txt
93

sed -i 's/hello/world/' file.txt
991

Matches the beginning of a word

sed -i 's/hello/world/' file.txt
94

sed -i 's/hello/world/' file.txt
992

Khớp với phần cuối của một từ

sed -i 's/hello/world/' file.txt
95

sed -i 's/hello/world/' file.txt
156

Chỉ khớp khi bắt đầu không gian mẫu. Điều này khác với

sed -i 's/hello/world/' file.txt
154 ở chế độ nhiều dòng

Compare the following two examples

sed -i 's/hello/world/' file.txt
96

sed -i 's/hello/world/' file.txt
157

Matches only at the end of pattern space. Điều này khác với

sed -i 's/hello/world/' file.txt
87 ở chế độ nhiều dòng


5. 7 Tham chiếu ngược và Biểu thức con

back-references are regular expression commands which refer to a previous part of the matched regular expression. Tham chiếu ngược được chỉ định bằng dấu gạch chéo ngược và một chữ số (e. g. ‘\1’). The part of the regular expression they refer to is called a subexpression, and is designated with parentheses

Tham chiếu ngược và biểu thức con được sử dụng trong hai trường hợp. trong mẫu tìm kiếm biểu thức chính quy và trong phần thay thế của lệnh

sed -i 's/hello/world/' file.txt
967 (xem Địa chỉ biểu thức chính quy và Lệnh "s")

In a regular expression pattern, back-references are used to match the same content as a previously matched subexpression. In the following example, the subexpression is ‘. ’ - bất kỳ ký tự đơn nào (được bao quanh bởi dấu ngoặc đơn khiến nó trở thành biểu thức con). Tham chiếu ngược '\ 1' yêu cầu khớp cùng nội dung (cùng ký tự) với biểu thức con

Lệnh bên dưới khớp với các từ bắt đầu bằng bất kỳ ký tự nào, theo sau là chữ 'o', theo sau là ký tự giống như ký tự đầu tiên

sed -i 's/hello/world/' file.txt
97

Multiple subexpressions are automatically numbered from left-to-right. Lệnh này tìm kiếm các bảng đối xứng gồm 6 chữ cái (ba chữ cái đầu tiên là 3 biểu thức con, tiếp theo là 3 tham chiếu ngược theo thứ tự ngược lại)

sed -i 's/hello/world/' file.txt
98

Trong lệnh

sed -i 's/hello/world/' file.txt
967, các tham chiếu ngược có thể được sử dụng trong phần thay thế để tham chiếu lại các biểu thức con trong phần biểu thức chính quy

Ví dụ sau sử dụng hai biểu thức con trong biểu thức chính quy để khớp với hai từ được phân tách bằng dấu cách. The back-references in the replacement part prints the words in a different order

sed -i 's/hello/world/' file.txt
99

Khi được sử dụng xen kẽ, nếu nhóm không tham gia trận đấu thì tham chiếu ngược khiến toàn bộ trận đấu thất bại. Ví dụ: 'a (. ). b\1' sẽ không khớp với 'ba'. Khi nhiều biểu thức chính quy được đưa ra với -e hoặc từ một tệp (tệp '-f'), các tham chiếu ngược là cục bộ cho từng biểu thức


5. 8 Escape Sequences - chỉ định các ký tự đặc biệt

Cho đến chương này, chúng ta mới chỉ gặp các lối thoát có dạng '\^', lệnh này yêu cầu

sed -i 's/hello/world/' file.txt
22 không hiểu dấu mũ là một ký tự đặc biệt, mà nên hiểu nó theo nghĩa đen. For example, ‘\*’ matches a single asterisk rather than zero or more backslashes

This chapter introduces another kind of escape6—that is, escapes that are applied to a character or sequence of characters that ordinarily are taken literally, and that

sed -i 's/hello/world/' file.txt
22 replaces with a special character. Điều này cung cấp một cách mã hóa các ký tự không in được trong các mẫu theo cách hiển thị. There is no restriction on the appearance of non-printing characters in a
sed -i 's/hello/world/' file.txt
22 script but when a script is being prepared in the shell or by text editing, it is usually easier to use one of the following escape sequences than the binary character it represents

Danh sách những lần trốn thoát này là

sed 's/hello/world/' input.txt > output.txt
902

Tạo hoặc khớp một ký tự BEL, đó là một "cảnh báo" (ASCII 7)

sed 's/hello/world/' input.txt > output.txt
903

Tạo hoặc khớp với nguồn cấp dữ liệu biểu mẫu (ASCII 12)

sed -i 's/hello/world/' file.txt
990

Tạo hoặc khớp một dòng mới (ASCII 10)

sed 's/hello/world/' input.txt > output.txt
905

Tạo hoặc khớp với trả lại vận chuyển (ASCII 13)

sed -i 's/hello/world/' file.txt
953

Tạo hoặc khớp với tab ngang (ASCII 9)

sed 's/hello/world/' input.txt > output.txt
907

Tạo hoặc khớp với cái gọi là “tab dọc” (ASCII 11)

sed 's/hello/world/' input.txt > output.txt
908

Tạo hoặc khớp với CONTROL-x, trong đó x là ký tự bất kỳ. The precise effect of ‘\cx’ is as follows. nếu x là chữ thường, nó được chuyển thành chữ hoa. Sau đó, bit 6 của ký tự (hex 40) được đảo ngược. Do đó, '\cz' trở thành hex 1A, nhưng '\c{' trở thành hex 3B, trong khi '\c;' trở thành hex 7B

sed 's/hello/world/' input.txt > output.txt
909

Tạo hoặc khớp một ký tự có giá trị ASCII thập phân là xxx

sed 's/hello/world/' input.txt > output.txt
910

Tạo hoặc khớp một ký tự có giá trị ASCII bát phân là xxx

sed 's/hello/world/' input.txt > output.txt
911

Produces or matches a character whose hexadecimal ASCII value is xx

'\b' (xóa lùi) đã bị bỏ qua do xung đột với nghĩa "ranh giới từ" hiện có

5. 8. 1 ưu tiên thoát

GNU

sed -i 's/hello/world/' file.txt
22 xử lý các chuỗi thoát trước khi chuyển văn bản vào đối sánh biểu thức chính quy của lệnh
sed -i 's/hello/world/' file.txt
666 và đối sánh Địa chỉ. Do đó, hai lệnh sau đây là tương đương ('0x5e' là giá trị ASCII thập lục phân của ký tự '^')

sed 's/hello/world/' input.txt > output.txt
90

As are the following (‘0x5b’,‘0x5d’ are the hexadecimal ASCII values of ‘[’,‘]’, respectively)

sed 's/hello/world/' input.txt > output.txt
91

Tuy nhiên, nên tránh các ký tự đặc biệt như vậy do các trường hợp cạnh không mong muốn. For example, the following are not equivalent

sed 's/hello/world/' input.txt > output.txt
92


5. 9 ký tự Multibyte và cân nhắc ngôn ngữ

GNU

sed -i 's/hello/world/' file.txt
22 xử lý các ký tự nhiều byte hợp lệ trong các ngôn ngữ nhiều byte (e. g.
sed 's/hello/world/' input.txt > output.txt
915). 7

Ví dụ sau sử dụng chữ cái Hy Lạp Capital Sigma (Σ, điểm mã Unicode

sed 's/hello/world/' input.txt > output.txt
916). Trong ngôn ngữ
sed 's/hello/world/' input.txt > output.txt
915,
sed -i 's/hello/world/' file.txt
22 xử lý chính xác Sigma dưới dạng một ký tự mặc dù nó là 2 octet (byte)

sed 's/hello/world/' input.txt > output.txt
93

Để buộc

sed -i 's/hello/world/' file.txt
22 xử lý các octet riêng biệt, hãy sử dụng ngôn ngữ
sed 's/hello/world/' input.txt > output.txt
920 (còn được gọi là ngôn ngữ
sed 's/hello/world/' input.txt > output.txt
921)

sed 's/hello/world/' input.txt > output.txt
94

5. 9. 1 Ký tự nhiều byte không hợp lệ

Các biểu thức chính quy của

sed -i 's/hello/world/' file.txt
22 không khớp với các chuỗi nhiều byte không hợp lệ trong một ngôn ngữ nhiều byte

Trong các ví dụ sau, giá trị ascii

sed 's/hello/world/' input.txt > output.txt
923 là một ký tự nhiều byte không hoàn chỉnh (được hiển thị ở đây là �). The regular expression ‘. ’ does not match it

sed 's/hello/world/' input.txt > output.txt
95

Tương tự, biểu thức chính quy 'bắt tất cả' '. *’ không khớp với toàn bộ dòng

sed 's/hello/world/' input.txt > output.txt
96

GNU

sed -i 's/hello/world/' file.txt
22 offers the special
sed -i 's/hello/world/' file.txt
977 command to clear the current pattern space regardless of invalid multibyte characters (i. e. it works like
sed 's/hello/world/' input.txt > output.txt
926 but also removes invalid multibyte characters)

sed 's/hello/world/' input.txt > output.txt
97

Alternatively, force the

sed 's/hello/world/' input.txt > output.txt
920 locale to process each octet separately (every octet is a valid character in the
sed 's/hello/world/' input.txt > output.txt
920 locale)

sed 's/hello/world/' input.txt > output.txt
98

sed -i 's/hello/world/' file.txt
22’s inability to process invalid multibyte characters can be used to detect such invalid sequences in a file. Trong các ví dụ sau,
sed 's/hello/world/' input.txt > output.txt
930 là một chuỗi nhiều byte không hợp lệ, trong khi
sed 's/hello/world/' input.txt > output.txt
931 là một chuỗi nhiều byte hợp lệ (của ký tự Sigma trong tiếng Hy Lạp)

Chương trình

sed -i 's/hello/world/' file.txt
22 sau đây xóa tất cả các ký tự hợp lệ bằng cách sử dụng
sed 's/hello/world/' input.txt > output.txt
933. Bất kỳ nội dung nào còn lại trong không gian mẫu (các ký tự không hợp lệ) đều được thêm vào không gian giữ bằng lệnh
sed -i 's/hello/world/' file.txt
947. Ở dòng cuối cùng (
sed -i 's/hello/world/' file.txt
87), khoảng trống được lấy ra (
sed -i 's/hello/world/' file.txt
975), các dòng mới bị xóa (
sed 's/hello/world/' input.txt > output.txt
937) và mọi octet còn lại được in rõ ràng (
sed -i 's/hello/world/' file.txt
67). Do đó, bất kỳ chuỗi multibyte không hợp lệ nào được in dưới dạng giá trị bát phân

sed 's/hello/world/' input.txt > output.txt
99

Với một vài lệnh nữa,

sed -i 's/hello/world/' file.txt
22 có thể in chính xác số dòng tương ứng với từng ký tự không hợp lệ (dòng 3). Sau đó, các ký tự này có thể bị xóa bằng cách buộc ngôn ngữ
sed 's/hello/world/' input.txt > output.txt
920 và sử dụng các chuỗi thoát bát phân

sed -i 's/hello/world/' file.txt
040

5. 9. 2 Chuyển đổi chữ hoa/chữ thường

Lệnh thay thế của GNU

sed -i 's/hello/world/' file.txt
22 (
sed -i 's/hello/world/' file.txt
967) hỗ trợ chuyển đổi chữ hoa/chữ thường sử dụng mã
sed -i 's/hello/world/' file.txt
105,
sed -i 's/hello/world/' file.txt
104. Những chuyển đổi này hỗ trợ các ký tự nhiều byte

sed -i 's/hello/world/' file.txt
041

Xem Lệnh "s"

5. 9. 3 lớp ký tự biểu thức chính quy nhiều byte

Ở các ngôn ngữ khác, trình tự sắp xếp không được chỉ định và '[a-d]' có thể tương đương với '[abcd]' hoặc '[aBbCcDd]' hoặc có thể không khớp với bất kỳ ký tự nào hoặc bộ ký tự mà nó . Để có được cách diễn giải truyền thống của biểu thức ngoặc, bạn có thể sử dụng ngôn ngữ 'C' bằng cách đặt biến môi trường

sed 's/hello/world/' input.txt > output.txt
945 thành giá trị 'C'

sed -i 's/hello/world/' file.txt
042

Giải thích của họ phụ thuộc vào ngôn ngữ

sed 's/hello/world/' input.txt > output.txt
946; . hàng năm. ]]’ có nghĩa là lớp ký tự gồm số và chữ cái trong ngôn ngữ hiện tại

LÀM. hiển thị ví dụ về đối chiếu

sed -i 's/hello/world/' file.txt
043


6 nâng cao sed -i 's/hello/world/' file.txt 22. chu kỳ và bộ đệm


6. 1 Cách thức hoạt động của sed -i 's/hello/world/' file.txt 22

sed -i 's/hello/world/' file.txt
22 duy trì hai bộ đệm dữ liệu. không gian mẫu đang hoạt động và không gian giữ phụ. Ban đầu cả hai đều rỗng

sed -i 's/hello/world/' file.txt
22 hoạt động bằng cách thực hiện chu trình sau trên mỗi dòng đầu vào. đầu tiên,
sed -i 's/hello/world/' file.txt
22 đọc một dòng từ luồng đầu vào, xóa mọi dòng mới ở cuối và đặt nó vào không gian mẫu. Sau đó, các lệnh được thực thi; . địa chỉ là một loại mã điều kiện và một lệnh chỉ được thực thi nếu điều kiện được xác minh trước khi lệnh được thực thi

Khi đến cuối tập lệnh, trừ khi tùy chọn -n được sử dụng, nội dung của không gian mẫu được in ra luồng đầu ra, thêm lại dòng mới ở cuối nếu nó bị xóa. 8 Sau đó, chu kỳ tiếp theo bắt đầu cho dòng đầu vào tiếp theo

Trừ khi các lệnh đặc biệt (như 'D') được sử dụng, không gian mẫu sẽ bị xóa giữa hai chu kỳ. Mặt khác, không gian giữ giữ dữ liệu của nó giữa các chu kỳ (xem các lệnh 'h', 'H', 'x', 'g', 'G' để di chuyển dữ liệu giữa cả hai bộ đệm)


6. 2 bộ đệm giữ và mẫu

LÀM


6. 3 Kỹ thuật đa dòng - sử dụng D,G,H,N,P để xử lý nhiều dòng

Nhiều dòng có thể được xử lý như một bộ đệm bằng cách sử dụng

sed -i 's/hello/world/' file.txt
939,
sed -i 's/hello/world/' file.txt
945,
sed -i 's/hello/world/' file.txt
947,
sed -i 's/hello/world/' file.txt
71,
sed -i 's/hello/world/' file.txt
957. Chúng tương tự như các lệnh viết thường (
sed -i 's/hello/world/' file.txt
36,
sed -i 's/hello/world/' file.txt
944,
sed -i 's/hello/world/' file.txt
946,
sed -i 's/hello/world/' file.txt
952,
sed -i 's/hello/world/' file.txt
37), ngoại trừ việc các lệnh này nối hoặc trừ dữ liệu trong khi tôn trọng các dòng mới được nhúng - cho phép thêm và xóa các dòng khỏi mẫu và giữ khoảng trắng

Họ hoạt động như sau

sed -i 's/hello/world/' file.txt
939

xóa dòng khỏi không gian mẫu cho đến dòng mới đầu tiên và khởi động lại chu kỳ

sed -i 's/hello/world/' file.txt
945

nối thêm dòng từ không gian giữ vào không gian mẫu, với một dòng mới trước nó

sed -i 's/hello/world/' file.txt
947

nối thêm dòng từ không gian mẫu vào không gian giữ, với một dòng mới trước nó

sed -i 's/hello/world/' file.txt
71

appends line from the input file to the pattern space

sed -i 's/hello/world/' file.txt
957

in dòng từ không gian mẫu cho đến dòng mới đầu tiên

Ví dụ sau minh họa hoạt động của lệnh

sed -i 's/hello/world/' file.txt
71 và
sed -i 's/hello/world/' file.txt
939

sed -i 's/hello/world/' file.txt
044

  1. sed -i 's/hello/world/' file.txt
    
    22 bắt đầu bằng cách đọc dòng đầu tiên vào không gian mẫu (i. e. '1')
  2. Vào đầu mỗi chu kỳ, lệnh
    sed -i 's/hello/world/' file.txt
    
    71 nối thêm một dòng mới và dòng tiếp theo vào không gian mẫu (i. e. ‘1’, ‘\n’, ‘2’ trong chu kỳ đầu tiên)
  3. Lệnh
    sed -i 's/hello/world/' file.txt
    
    67 in rõ ràng nội dung của không gian mẫu
  4. Sau đó, lệnh
    sed -i 's/hello/world/' file.txt
    
    939 sẽ xóa nội dung của không gian mẫu cho đến dòng mới đầu tiên (để lại '2' ở cuối chu kỳ đầu tiên)
  5. Ở chu kỳ tiếp theo, lệnh
    sed -i 's/hello/world/' file.txt
    
    71 nối thêm một dòng mới và dòng đầu vào tiếp theo vào không gian mẫu (e. g. ‘2’, ‘\n’, ‘3’)

Một kỹ thuật phổ biến để xử lý các khối văn bản như đoạn văn (thay vì từng dòng) là sử dụng cấu trúc sau

sed -i 's/hello/world/' file.txt
045

  1. Biểu thức đầu tiên,
    sed 's/hello/world/' input.txt > output.txt
    
    974 hoạt động trên tất cả các dòng không trống và thêm dòng hiện tại (trong không gian mẫu) vào không gian giữ. Trên tất cả các dòng ngoại trừ dòng cuối cùng, không gian mẫu bị xóa và chu kỳ được bắt đầu lại
  2. Các biểu thức khác
    sed -i 's/hello/world/' file.txt
    
    975 và
    sed -i 's/hello/world/' file.txt
    
    967 chỉ được thực hiện trên các dòng trống (i. e. ngăn cách đoạn văn). Lệnh
    sed -i 's/hello/world/' file.txt
    
    975 tìm nạp các dòng tích lũy từ không gian giữ trở lại không gian mẫu. Sau đó, lệnh
    sed -i 's/hello/world/' file.txt
    
    666 sẽ hoạt động trên tất cả văn bản trong đoạn văn (bao gồm cả các dòng mới được nhúng)

Ví dụ sau minh họa kỹ thuật này

sed -i 's/hello/world/' file.txt
046

Để biết thêm ví dụ về chú thích, hãy xem Tìm kiếm văn bản trên nhiều dòng và Điều chỉnh độ dài dòng


6. 4 Phân nhánh và Kiểm soát luồng

Các lệnh rẽ nhánh

sed -i 's/hello/world/' file.txt
982,
sed -i 's/hello/world/' file.txt
983 và
sed -i 's/hello/world/' file.txt
984 cho phép thay đổi luồng chương trình
sed -i 's/hello/world/' file.txt
22

Theo mặc định,

sed -i 's/hello/world/' file.txt
22 đọc một dòng đầu vào vào bộ đệm mẫu, sau đó tiếp tục xử lý tất cả các lệnh theo thứ tự. Các lệnh không có địa chỉ ảnh hưởng đến tất cả các dòng. Các lệnh có địa chỉ chỉ ảnh hưởng đến các dòng phù hợp. Xem tổng quan về Chu kỳ thực thi và địa chỉ

sed -i 's/hello/world/' file.txt
22 does not support a typical
sed 's/hello/world/' input.txt > output.txt
985 construct. Instead, some commands can be used as conditionals or to change the default flow control

sed -i 's/hello/world/' file.txt
36

xóa (xóa) không gian mẫu hiện tại và khởi động lại chu kỳ chương trình mà không xử lý phần còn lại của lệnh và không in không gian mẫu

sed -i 's/hello/world/' file.txt
939

xóa nội dung của không gian mẫu cho đến dòng mới đầu tiên và khởi động lại chu kỳ chương trình mà không xử lý phần còn lại của lệnh và không in không gian mẫu

sed 's/hello/world/' input.txt > output.txt
988
sed 's/hello/world/' input.txt > output.txt
989
sed 's/hello/world/' input.txt > output.txt
990
sed 's/hello/world/' input.txt > output.txt
991

Addresses and regular expressions can be used as an

sed 's/hello/world/' input.txt > output.txt
985 conditional. Nếu [addr] khớp với không gian mẫu hiện tại, hãy thực hiện (các) lệnh. Ví dụ. Lệnh
sed 's/hello/world/' input.txt > output.txt
993 có nghĩa là. nếu mẫu hiện tại khớp với biểu thức chính quy
sed 's/hello/world/' input.txt > output.txt
994 (dòng bắt đầu bằng hàm băm), thì hãy thực hiện lệnh
sed -i 's/hello/world/' file.txt
36. xóa dòng mà không in và khởi động lại chu kỳ chương trình ngay lập tức

sed -i 's/hello/world/' file.txt
982

nhánh vô điều kiện (nghĩa là. always jump to a label, skipping or repeating other commands, without restarting a new cycle). Kết hợp với một địa chỉ, nhánh có thể được thực hiện có điều kiện trên các dòng phù hợp

sed -i 's/hello/world/' file.txt
983

branch conditionally (that is. jump to a label) only if a

sed -i 's/hello/world/' file.txt
666 command has succeeded since the last input line was read or another conditional branch was taken

sed -i 's/hello/world/' file.txt
984

tương tự nhưng ngược lại với lệnh

sed -i 's/hello/world/' file.txt
983. chỉ rẽ nhánh nếu không có sự thay thế thành công nào kể từ khi dòng đầu vào cuối cùng được đọc

Hai chương trình

sed -i 's/hello/world/' file.txt
22 sau đây là tương đương. The first (contrived) example uses the
sed -i 's/hello/world/' file.txt
982 command to skip the
sed -i 's/hello/world/' file.txt
666 command on lines containing ‘1’. Ví dụ thứ hai sử dụng một địa chỉ phủ định (‘. ’) để chỉ thực hiện thay thế trên các dòng mong muốn. The
sed -i 's/hello/world/' file.txt
0404 command is still executed on all lines

sed -i 's/hello/world/' file.txt
047

6. 4. 1 Phân nhánh và Chu kỳ

Các lệnh

sed -i 's/hello/world/' file.txt
982,
sed -i 's/hello/world/' file.txt
983 và
sed -i 's/hello/world/' file.txt
984 có thể được theo sau bởi một nhãn (thường là một chữ cái). Nhãn được xác định bằng dấu hai chấm theo sau bởi một hoặc nhiều chữ cái (e. g. ‘. x’). If the label is omitted the branch commands restart the cycle. Note the difference between branching to a label and restarting the cycle. when a cycle is restarted,
sed -i 's/hello/world/' file.txt
22 first prints the current content of the pattern space, then reads the next input line into the pattern space; Jumping to a label (even if it is at the beginning of the program) does not print the pattern space and does not read the next input line

The following program is a no-op. The

sed -i 's/hello/world/' file.txt
982 command (the only command in the program) does not have a label, and thus simply restarts the cycle. On each cycle, the pattern space is printed and the next input line is read

Ví dụ sau đây là một vòng lặp vô hạn - nó không kết thúc và không in bất cứ thứ gì. The

sed -i 's/hello/world/' file.txt
982 command jumps to the ‘x’ label, and a new cycle is never started

sed -i 's/hello/world/' file.txt
048

Branching is often complemented with the

sed -i 's/hello/world/' file.txt
952 or
sed -i 's/hello/world/' file.txt
71 commands. both commands read the next input line into the pattern space without waiting for the cycle to restart. Before reading the next input line,
sed -i 's/hello/world/' file.txt
952 prints the current pattern space then empties it, while
sed -i 's/hello/world/' file.txt
71 appends a newline and the next input line to the pattern space

Consider the following two examples

sed -i 's/hello/world/' file.txt
049

  • Both examples do not inf-loop, despite never starting a new cycle
  • In the first example, the
    sed -i 's/hello/world/' file.txt
    
    952 commands first prints the content of the pattern space, empties the pattern space then reads the next input line
  • In the second example, the
    sed -i 's/hello/world/' file.txt
    
    71 commands appends the next input line to the pattern space (with a newline). Lines are accumulated in the pattern space until there are no more input lines to read, then the
    sed -i 's/hello/world/' file.txt
    
    71 command terminates the
    sed -i 's/hello/world/' file.txt
    
    22 program. When the program terminates, the end-of-cycle actions are performed, and the entire pattern space is printed
  • The second example requires GNU
    sed -i 's/hello/world/' file.txt
    
    22, because it uses the non-POSIX-standard behavior of
    sed -i 's/hello/world/' file.txt
    
    71. See the “
    sed -i 's/hello/world/' file.txt
    
    71 command on the last line” paragraph in Reporting Bugs
  • To further examine the difference between the two examples, try the following commands

    sed -i 's/hello/world/' file.txt
    
    000

6. 4. 2 Branching example. joining lines

As a real-world example of using branching, consider the case of quoted-printable files, typically used to encode email messages. In these files long lines are split and marked with a soft line break consisting of a single ‘=’ character at the end of the line

sed -i 's/hello/world/' file.txt
001

The following program uses an address match ‘/=$/’ as a conditional. If the current pattern space ends with a ‘=’, it reads the next input line using

sed -i 's/hello/world/' file.txt
71, replaces all ‘=’ characters which are followed by a newline, and unconditionally branches (
sed -i 's/hello/world/' file.txt
982) to the beginning of the program without restarting a new cycle. If the pattern space does not ends with ‘=’, the default action is performed. the pattern space is printed and a new cycle is started

sed -i 's/hello/world/' file.txt
002

Here’s an alternative program with a slightly different approach. On all lines except the last,

sed -i 's/hello/world/' file.txt
71 appends the line to the pattern space. A substitution command then removes soft line breaks (‘=’ at the end of a line, i. e. followed by a newline) by replacing them with an empty string. if the substitution was successful (meaning the pattern space contained a line which should be joined), The conditional branch command
sed -i 's/hello/world/' file.txt
983 jumps to the beginning of the program without completing or restarting the cycle. If the substitution failed (meaning there were no soft line breaks), The
sed -i 's/hello/world/' file.txt
983 command will not branch. Then,
sed -i 's/hello/world/' file.txt
957 will print the pattern space content until the first newline, and
sed -i 's/hello/world/' file.txt
939 will delete the pattern space content until the first new line. (To learn more about
sed -i 's/hello/world/' file.txt
71,
sed -i 's/hello/world/' file.txt
957 and
sed -i 's/hello/world/' file.txt
939 commands see Multiline techniques)

sed -i 's/hello/world/' file.txt
003

For more line-joining examples see Joining lines


7 Some Sample Scripts

Here are some

sed -i 's/hello/world/' file.txt
22 scripts to guide you in the art of mastering
sed -i 's/hello/world/' file.txt
22


7. 1 Joining lines

This section uses

sed -i 's/hello/world/' file.txt
71,
sed -i 's/hello/world/' file.txt
939 and
sed -i 's/hello/world/' file.txt
957 commands to process multiple lines, and the
sed -i 's/hello/world/' file.txt
982 and
sed -i 's/hello/world/' file.txt
983 commands for branching. See Multiline techniques and Branching and flow control

Join specific lines (e. g. if lines 2 and 3 need to be joined)

sed -i 's/hello/world/' file.txt
004

Join backslash-continued lines

sed -i 's/hello/world/' file.txt
005

Join lines that start with whitespace (e. g SMTP headers)

sed -i 's/hello/world/' file.txt
006


7. 2 Centering Lines

This script centers all lines of a file on a 80 columns width. To change that width, the number in

sed -i 's/hello/world/' file.txt
0439 must be replaced, and the number of added spaces also must be changed

Note how the buffer commands are used to separate parts in the regular expressions to be matched—this is a common technique

sed -i 's/hello/world/' file.txt
007
sed -i 's/hello/world/' file.txt
008
sed -i 's/hello/world/' file.txt
009
sed -i 's/hello/world/' file.txt
010
sed -i 's/hello/world/' file.txt
011
sed -i 's/hello/world/' file.txt
012


7. 3 Tăng một số

This script is one of a few that demonstrate how to do arithmetic in

sed -i 's/hello/world/' file.txt
22. This is indeed possible,9 but must be done manually

To increment one number you just add 1 to last digit, replacing it by the following digit. There is one exception. when the digit is a nine the previous digits must be also incremented until you don’t have a nine

This solution by Bruno Haible is very clever and smart because it uses a single buffer; if you don’t have this limitation, the algorithm used in Numbering lines, is faster. It works by replacing trailing nines with an underscore, then using multiple

sed -i 's/hello/world/' file.txt
967 commands to increment the last digit, and then again substituting underscores with zeros

sed -i 's/hello/world/' file.txt
013
sed -i 's/hello/world/' file.txt
014
sed -i 's/hello/world/' file.txt
015
sed -i 's/hello/world/' file.txt
016
sed -i 's/hello/world/' file.txt
017


7. 4 Rename Files to Lower Case

This is a pretty strange use of

sed -i 's/hello/world/' file.txt
22. We transform text, and transform it to be shell commands, then just feed them to shell. Don’t worry, even worse hacks are done when using
sed -i 's/hello/world/' file.txt
22; I have seen a script converting the output of
sed -i 's/hello/world/' file.txt
0444 into a
sed -i 's/hello/world/' file.txt
0445 program

The main body of this is the

sed -i 's/hello/world/' file.txt
22 script, which remaps the name from lower to upper (or vice-versa) and even checks out if the remapped name is the same as the original name. Note how the script is parameterized using shell variables and proper quoting

sed -i 's/hello/world/' file.txt
018
sed -i 's/hello/world/' file.txt
019
sed -i 's/hello/world/' file.txt
020
sed -i 's/hello/world/' file.txt
021
sed -i 's/hello/world/' file.txt
022
sed -i 's/hello/world/' file.txt
023
sed -i 's/hello/world/' file.txt
024
sed -i 's/hello/world/' file.txt
025
sed -i 's/hello/world/' file.txt
026
sed -i 's/hello/world/' file.txt
027
sed -i 's/hello/world/' file.txt
028
sed -i 's/hello/world/' file.txt
029
sed -i 's/hello/world/' file.txt
030
sed -i 's/hello/world/' file.txt
031
sed -i 's/hello/world/' file.txt
032
sed -i 's/hello/world/' file.txt
033
sed -i 's/hello/world/' file.txt
034
sed -i 's/hello/world/' file.txt
035
sed -i 's/hello/world/' file.txt
036
sed -i 's/hello/world/' file.txt
037
sed -i 's/hello/world/' file.txt
038
sed -i 's/hello/world/' file.txt
039
sed -i 's/hello/world/' file.txt
040


7. 5 Print sed -i 's/hello/world/' file.txt 0447 Environment

This script strips the definition of the shell functions from the output of the

sed -i 's/hello/world/' file.txt
0448 Bourne-shell command

sed -i 's/hello/world/' file.txt
041
sed -i 's/hello/world/' file.txt
042
sed -i 's/hello/world/' file.txt
043
sed -i 's/hello/world/' file.txt
044
sed -i 's/hello/world/' file.txt
045
sed -i 's/hello/world/' file.txt
046
sed -i 's/hello/world/' file.txt
047


7. 6 Reverse Characters of Lines

This script can be used to reverse the position of characters in lines. The technique moves two characters at a time, hence it is faster than more intuitive implementations

Note the

sed -i 's/hello/world/' file.txt
0449 command before the definition of the label. Điều này thường cần thiết để đặt lại cờ được kiểm tra bằng lệnh
sed -i 's/hello/world/' file.txt
983

Imaginative readers will find uses for this script. Một ví dụ là đảo ngược đầu ra của

sed -i 's/hello/world/' file.txt
0451. 10

sed -i 's/hello/world/' file.txt
048
sed -i 's/hello/world/' file.txt
049
sed -i 's/hello/world/' file.txt
050
sed -i 's/hello/world/' file.txt
051


7. 7 Text search across multiple lines

Phần này sử dụng lệnh

sed -i 's/hello/world/' file.txt
71 và
sed -i 's/hello/world/' file.txt
939 để tìm kiếm các từ liên tiếp trên nhiều dòng. Xem kỹ thuật Multiline

Những ví dụ này giải quyết việc tìm kiếm các từ xuất hiện gấp đôi trong tài liệu

Finding doubled words in a single line is easy using GNU

sed -i 's/hello/world/' file.txt
687 and similarly with GNU
sed -i 's/hello/world/' file.txt
22

sed -i 's/hello/world/' file.txt
052

  • Cụm từ thông dụng ‘\b\w+\s+’ tìm kiếm ranh giới từ (‘\b’), theo sau là một hoặc nhiều ký tự từ (‘\w+’), tiếp theo là khoảng trắng (‘\s+’). See regexp extensions
  • Adding parentheses around the ‘(\w+)’ expression creates a subexpression. Mẫu biểu thức chính quy '(PATTERN)\s+\1' xác định một biểu thức con (trong ngoặc đơn), theo sau là tham chiếu ngược, được phân tách bằng khoảng trắng. Kết hợp thành công có nghĩa là MẪU được lặp lại hai lần liên tiếp. See Back-references and Subexpressions
  • Biểu thức ranh giới từ (‘\b’) ở cả hai đầu đảm bảo các từ một phần không được khớp (e. g. 'the then' không phải là một kết hợp mong muốn)
  • The -E option enables extended regular expression syntax, alleviating the need to add backslashes before the parenthesis. Xem cú pháp ERE

When the doubled word span two lines the above regular expression will not find them as

sed -i 's/hello/world/' file.txt
687 and
sed -i 's/hello/world/' file.txt
22 operate line-by-line

Bằng cách sử dụng các lệnh

sed -i 's/hello/world/' file.txt
71 và
sed -i 's/hello/world/' file.txt
939,
sed -i 's/hello/world/' file.txt
22 có thể áp dụng các biểu thức chính quy trên nhiều dòng (nghĩa là nhiều dòng được lưu trữ trong không gian mẫu và biểu thức chính quy hoạt động trên đó)

sed -i 's/hello/world/' file.txt
053

  • Lệnh
    sed -i 's/hello/world/' file.txt
    
    71 nối thêm dòng tiếp theo vào không gian mẫu (do đó đảm bảo nó chứa hai dòng liên tiếp trong mỗi chu kỳ)
  • The regular expression uses ‘\s+’ for word separator which matches both spaces and newlines
  • The regular expression matches, the entire pattern space is printed with
    sed -i 's/hello/world/' file.txt
    
    37. Không có dòng nào được in theo mặc định do tùy chọn -n
  • The
    sed -i 's/hello/world/' file.txt
    
    939 removes the first line from the pattern space (up until the first newline), readying it for the next cycle

See the GNU

sed -i 's/hello/world/' file.txt
0464 manual for an alternative solution using
sed -i 's/hello/world/' file.txt
0465 and
sed -i 's/hello/world/' file.txt
0466 at https. //gnu. org/s/coreutils/manual/html_node/Squeezing-and-deleting. html


7. 8 Line length adjustment

This section uses

sed -i 's/hello/world/' file.txt
71 and
sed -i 's/hello/world/' file.txt
939 commands to search for consecutive words spanning multiple lines, and the
sed -i 's/hello/world/' file.txt
982 command for branching. See Multiline techniques and Branching and flow control

This (somewhat contrived) example deal with formatting and wrapping lines of text of the following input file

sed -i 's/hello/world/' file.txt
054

The following sed program wraps lines at 40 characters

sed -i 's/hello/world/' file.txt
055

The wrapped output

sed -i 's/hello/world/' file.txt
056


7. 9 Reverse Lines of Files

This one begins a series of totally useless (yet interesting) scripts emulating various Unix commands. This, in particular, is a

sed -i 's/hello/world/' file.txt
0470 workalike

Note that on implementations other than GNU

sed -i 's/hello/world/' file.txt
22 this script might easily overflow internal buffers

sed -i 's/hello/world/' file.txt
057
sed -i 's/hello/world/' file.txt
058
sed -i 's/hello/world/' file.txt
059
sed -i 's/hello/world/' file.txt
060


7. 10 Numbering Lines

This script replaces ‘cat -n’; in fact it formats its output exactly like GNU

sed -i 's/hello/world/' file.txt
0472 does

Of course this is completely useless and for two reasons. first, because somebody else did it in C, second, because the following Bourne-shell script could be used for the same purpose and would be much faster

sed -i 's/hello/world/' file.txt
061

It uses

sed -i 's/hello/world/' file.txt
22 to print the line number, then groups lines two by two using
sed -i 's/hello/world/' file.txt
71. Of course, this script does not teach as much as the one presented below

The algorithm used for incrementing uses both buffers, so the line is printed as soon as possible and then discarded. The number is split so that changing digits go in a buffer and unchanged ones go in the other; the changed digits are modified in a single step (using a

sed -i 's/hello/world/' file.txt
183 command). The line number for the next line is then composed and stored in the hold space, to be used in the next iteration

sed -i 's/hello/world/' file.txt
062
sed -i 's/hello/world/' file.txt
063
sed -i 's/hello/world/' file.txt
064
sed -i 's/hello/world/' file.txt
065
sed -i 's/hello/world/' file.txt
066
sed -i 's/hello/world/' file.txt
067
sed -i 's/hello/world/' file.txt
068
sed -i 's/hello/world/' file.txt
069
sed -i 's/hello/world/' file.txt
070


7. 11 Numbering Non-blank Lines

Emulating ‘cat -b’ is almost the same as ‘cat -n’—we only have to select which lines are to be numbered and which are not

The part that is common to this script and the previous one is not commented to show how important it is to comment

sed -i 's/hello/world/' file.txt
22 scripts properly

sed -i 's/hello/world/' file.txt
062
sed -i 's/hello/world/' file.txt
072
sed -i 's/hello/world/' file.txt
073


7. 12 Counting Characters

This script shows another way to do arithmetic with

sed -i 's/hello/world/' file.txt
22. In this case we have to add possibly large numbers, so implementing this by successive increments would not be feasible (and possibly even more complicated to contrive than this script)

The approach is to map numbers to letters, kind of an abacus implemented with

sed -i 's/hello/world/' file.txt
22. ‘a’s are units, ‘b’s are tens and so on. we simply add the number of characters on the current line as units, and then propagate the carry to tens, hundreds, and so on

As usual, running totals are kept in hold space

On the last line, we convert the abacus form back to decimal. For the sake of variety, this is done with a loop rather than with some 80

sed -i 's/hello/world/' file.txt
967 commands11. first we convert units, removing ‘a’s from the number; then we rotate letters so that tens become ‘a’s, and so on until no more letters remain

sed -i 's/hello/world/' file.txt
062
sed -i 's/hello/world/' file.txt
075
sed -i 's/hello/world/' file.txt
076
sed -i 's/hello/world/' file.txt
077
sed -i 's/hello/world/' file.txt
078
sed -i 's/hello/world/' file.txt
079
sed -i 's/hello/world/' file.txt
080


7. 13 Counting Words

This script is almost the same as the previous one, once each of the words on the line is converted to a single ‘a’ (in the previous script each letter was changed to an ‘a’)

It is interesting that real

sed -i 's/hello/world/' file.txt
0480 programs have optimized loops for ‘wc -c’, so they are much slower at counting words rather than characters. This script’s bottleneck, instead, is arithmetic, and hence the word-counting one is faster (it has to manage smaller numbers)

Again, the common parts are not commented to show the importance of commenting

sed -i 's/hello/world/' file.txt
22 scripts

sed -i 's/hello/world/' file.txt
062
sed -i 's/hello/world/' file.txt
082
sed -i 's/hello/world/' file.txt
083
sed -i 's/hello/world/' file.txt
084


7. 14 Counting Lines

Không có điều kỳ lạ nào được thực hiện ngay bây giờ, bởi vì

sed -i 's/hello/world/' file.txt
22 cung cấp cho chúng tôi chức năng 'wc -l' miễn phí. Look


7. 15 Printing the First Lines

This script is probably the simplest useful

sed -i 's/hello/world/' file.txt
22 script. It displays the first 10 lines of input; the number of displayed lines is right before the
sed -i 's/hello/world/' file.txt
900 command


7. 16 Printing the Last Lines

Printing the last n lines rather than the first is more complex but indeed possible. n is encoded in the second line, before the bang character

This script is similar to the

sed -i 's/hello/world/' file.txt
0470 script in that it keeps the final output in the hold space and prints it at the end

sed -i 's/hello/world/' file.txt
062
sed -i 's/hello/world/' file.txt
086

Mainly, the scripts keeps a window of 10 lines and slides it by adding a line and deleting the oldest (the substitution command on the second line works like a

sed -i 's/hello/world/' file.txt
939 command but does not restart the loop)

The “sliding window” technique is a very powerful way to write efficient and complex

sed -i 's/hello/world/' file.txt
22 scripts, because commands like
sed -i 's/hello/world/' file.txt
957 would require a lot of work if implemented manually

To introduce the technique, which is fully demonstrated in the rest of this chapter and is based on the

sed -i 's/hello/world/' file.txt
71,
sed -i 's/hello/world/' file.txt
957 and
sed -i 's/hello/world/' file.txt
939 commands, here is an implementation of
sed -i 's/hello/world/' file.txt
0492 using a simple “sliding window. ”

This looks complicated but in fact the working is the same as the last script. after we have kicked in the appropriate number of lines, however, we stop using the hold space to keep inter-line state, and instead use

sed -i 's/hello/world/' file.txt
71 and
sed -i 's/hello/world/' file.txt
939 to slide pattern space by one line

sed -i 's/hello/world/' file.txt
007
sed -i 's/hello/world/' file.txt
088

Note how the first, second and fourth line are inactive after the first ten lines of input. After that, all the script does is. exiting on the last line of input, appending the next input line to pattern space, and removing the first line


7. 17 Make Duplicate Lines Unique

This is an example of the art of using the

sed -i 's/hello/world/' file.txt
71,
sed -i 's/hello/world/' file.txt
957 and
sed -i 's/hello/world/' file.txt
939 commands, probably the most difficult to master

sed -i 's/hello/world/' file.txt
089
sed -i 's/hello/world/' file.txt
090
sed -i 's/hello/world/' file.txt
091
sed -i 's/hello/world/' file.txt
092

As you can see, we maintain a 2-line window using

sed -i 's/hello/world/' file.txt
957 and
sed -i 's/hello/world/' file.txt
939. This technique is often used in advanced
sed -i 's/hello/world/' file.txt
22 scripts


7. 18 Print Duplicated Lines of Input

This script prints only duplicated lines, like ‘uniq -d’

sed -i 's/hello/world/' file.txt
062
sed -i 's/hello/world/' file.txt
094
sed -i 's/hello/world/' file.txt
095
sed -i 's/hello/world/' file.txt
096
sed -i 's/hello/world/' file.txt
097


7. 19 Remove All Duplicated Lines

This script prints only unique lines, like ‘uniq -u’

sed -i 's/hello/world/' file.txt
007
sed -i 's/hello/world/' file.txt
099
sed -i 's/hello/world/' file.txt
00
sed -i 's/hello/world/' file.txt
01
sed -i 's/hello/world/' file.txt
02


7. 20 Squeezing Blank Lines

As a final example, here are three scripts, of increasing complexity and speed, that implement the same function as ‘cat -s’, that is squeezing blank lines

The first leaves a blank line at the beginning and end if there are some already

sed -i 's/hello/world/' file.txt
007
sed -i 's/hello/world/' file.txt
04
sed -i 's/hello/world/' file.txt
05

This one is a bit more complex and removes all empty lines at the beginning. It does leave a single blank line at end if one was there

sed -i 's/hello/world/' file.txt
007
sed -i 's/hello/world/' file.txt
07
sed -i 's/hello/world/' file.txt
08

This removes leading and trailing blank lines. It is also the fastest. Note that loops are completely done with

sed -i 's/hello/world/' file.txt
952 and
sed -i 's/hello/world/' file.txt
982, without relying on
sed -i 's/hello/world/' file.txt
22 to restart the script automatically at the end of a line

sed -i 's/hello/world/' file.txt
062
sed -i 's/hello/world/' file.txt
10
sed -i 's/hello/world/' file.txt
11
sed -i 's/hello/world/' file.txt
12
sed -i 's/hello/world/' file.txt
13
sed -i 's/hello/world/' file.txt
14


8 GNU sed -i 's/hello/world/' file.txt 22’s Limitations and Non-limitations

Đối với những người muốn viết tập lệnh

sed -i 's/hello/world/' file.txt
22 di động, hãy lưu ý rằng một số triển khai đã được biết là giới hạn độ dài dòng (đối với mẫu và khoảng trắng) không quá 4000 byte. The POSIX standard specifies that conforming
sed -i 's/hello/world/' file.txt
22 implementations shall support at least 8192 byte line lengths. GNU
sed -i 's/hello/world/' file.txt
22 has no built-in limit on line length; as long as it can
sed -i 's/hello/world/' file.txt
0008 more (virtual) memory, you can feed or construct lines as long as you like

However, recursion is used to handle subpatterns and indefinite repetition. This means that the available stack space may limit the size of the buffer that can be processed by certain patterns


9 Other Resources for Learning About sed -i 's/hello/world/' file.txt 22

For up to date information about GNU

sed -i 's/hello/world/' file.txt
22 please visit https. //www. gnu. org/software/sed/

Send general questions and suggestions to sed-devel@gnu. org. Visit the mailing list archives for past discussions at https. //lists. gnu. org/archive/html/sed-devel/

The following resources provide information about

sed -i 's/hello/world/' file.txt
22 (both GNU
sed -i 's/hello/world/' file.txt
22 and other variations). Note these not maintained by GNU
sed -i 's/hello/world/' file.txt
22 developers


10 Reporting Bugs

Email bug reports to bug-sed@gnu. org. Also, please include the output of ‘sed --version’ in the body of your report if at all possible

Please do not send a bug report like this

sed -i 's/hello/world/' file.txt
15

If GNU

sed -i 's/hello/world/' file.txt
22 doesn’t configure your favorite package, take a few extra minutes to identify the specific problem and make a stand-alone test case. Unlike other programs such as C compilers, making such test cases for
sed -i 's/hello/world/' file.txt
22 is quite simple

A stand-alone test case includes all the data necessary to perform the test, and the specific invocation of

sed -i 's/hello/world/' file.txt
22 that causes the problem. The smaller a stand-alone test case is, the better. A test case should not involve something as far removed from
sed -i 's/hello/world/' file.txt
22 as “try to configure frobme-1. 3. 4”. Yes, that is in principle enough information to look for the bug, but that is not a very practical prospect

Here are a few commonly reported bugs that are not bugs

sed -i 's/hello/world/' file.txt
71 command on the last line

Most versions of

sed -i 's/hello/world/' file.txt
22 exit without printing anything when the
sed -i 's/hello/world/' file.txt
71 command is issued on the last line of a file. GNU
sed -i 's/hello/world/' file.txt
22 prints pattern space before exiting unless of course the
sed -i 's/hello/world/' file.txt
45 command switch has been specified. This choice is by design

Default behavior (gnu extension, non-POSIX conforming)

To force POSIX-conforming behavior

sed -i 's/hello/world/' file.txt
16

For example, the behavior of

would depend on whether foo has an even or an odd number of lines12. Or, when writing a script to read the next few lines following a pattern match, traditional implementations of

sed -i 's/hello/world/' file.txt
22 would force you to write something like

sed -i 's/hello/world/' file.txt
17

instead of just

sed -i 's/hello/world/' file.txt
18

In any case, the simplest workaround is to use

sed -i 's/hello/world/' file.txt
0024 in scripts that rely on the traditional behavior, or to set the
sed -i 's/hello/world/' file.txt
72 variable to a non-empty value

Regex syntax clashes (problems with backslashes)

sed -i 's/hello/world/' file.txt
22 uses the POSIX basic regular expression syntax. According to the standard, the meaning of some escape sequences is undefined in this syntax; notable in the case of
sed -i 's/hello/world/' file.txt
22 are
sed -i 's/hello/world/' file.txt
911,
sed -i 's/hello/world/' file.txt
923,
sed -i 's/hello/world/' file.txt
925,
sed -i 's/hello/world/' file.txt
156,
sed -i 's/hello/world/' file.txt
157,
sed -i 's/hello/world/' file.txt
991,
sed -i 's/hello/world/' file.txt
992,
sed -i 's/hello/world/' file.txt
987,
sed -i 's/hello/world/' file.txt
988,
sed -i 's/hello/world/' file.txt
985, and
sed -i 's/hello/world/' file.txt
986

Như trong tất cả các chương trình GNU sử dụng các biểu thức chính quy cơ bản POSIX,

sed -i 's/hello/world/' file.txt
22 diễn giải các chuỗi thoát này thành các ký tự đặc biệt. So,
sed -i 's/hello/world/' file.txt
0040 matches one or more occurrences of ‘x’.
sed -i 's/hello/world/' file.txt
0041 matches either ‘abc’ or ‘def’

This syntax may cause problems when running scripts written for other

sed -i 's/hello/world/' file.txt
22s. Some
sed -i 's/hello/world/' file.txt
22 programs have been written with the assumption that
sed -i 's/hello/world/' file.txt
911 and
sed -i 's/hello/world/' file.txt
923 match the literal characters
sed -i 's/hello/world/' file.txt
0046 and
sed -i 's/hello/world/' file.txt
0047. Such scripts must be modified by removing the spurious backslashes if they are to be used with modern implementations of
sed -i 's/hello/world/' file.txt
22, like GNU
sed -i 's/hello/world/' file.txt
22

On the other hand, some scripts use s. abc\. def. g to remove occurrences of either

sed -i 's/hello/world/' file.txt
0050 or
sed -i 's/hello/world/' file.txt
0051. While this worked until
sed -i 's/hello/world/' file.txt
22 4. 0. x, newer versions interpret this as removing the string
sed -i 's/hello/world/' file.txt
0053. This is again undefined behavior according to POSIX, and this interpretation is arguably more robust. older
sed -i 's/hello/world/' file.txt
22s, for example, required that the regex matcher parsed
sed -i 's/hello/world/' file.txt
0055 as
sed -i 's/hello/world/' file.txt
994 in the common case of escaping a slash, which is again undefined behavior; the new behavior avoids this, and this is good because the regex matcher is only partially under our control

In addition, this version of

sed -i 's/hello/world/' file.txt
22 supports several escape characters (some of which are multi-character) to insert non-printable characters in scripts (
sed 's/hello/world/' input.txt > output.txt
902,
sed -i 's/hello/world/' file.txt
0059,
sed -i 's/hello/world/' file.txt
0060,
sed -i 's/hello/world/' file.txt
0061,
sed 's/hello/world/' input.txt > output.txt
905,
sed -i 's/hello/world/' file.txt
953,
sed 's/hello/world/' input.txt > output.txt
907,
sed -i 's/hello/world/' file.txt
0065). These can cause similar problems with scripts written for other
sed -i 's/hello/world/' file.txt
22s

-i clobbers read-only files

In short, ‘sed -i’ will let you delete the contents of a read-only file, and in general the -i option (see Invocation) lets you clobber protected files. This is not a bug, but rather a consequence of how the Unix file system works

The permissions on a file say what can happen to the data in that file, while the permissions on a directory say what can happen to the list of files in that directory. ‘sed -i’ will not ever open for writing a file that is already on disk. Rather, it will work on a temporary file that is finally renamed to the original name. if you rename or delete files, you’re actually modifying the contents of the directory, so the operation depends on the permissions of the directory, not of the file. For this same reason,

sed -i 's/hello/world/' file.txt
22 does not let you use -i on a writable file in a read-only directory, and will break hard or symbolic links when -i is used on such a file

sed -i 's/hello/world/' file.txt
0068 does not work (gives an error)

There is no line 0. 0 is a special address that is only used to treat addresses like

sed -i 's/hello/world/' file.txt
0069 as active when the script starts. if you write
sed -i 's/hello/world/' file.txt
0070 and the first line includes the word ‘abc’, then that match would be ignored because address ranges must span at least two lines (barring the end of the file); but what you probably wanted is to delete every line up to the first one including ‘abc’, and this is obtained with
sed -i 's/hello/world/' file.txt
0071

sed -i 's/hello/world/' file.txt
0072 is case insensitive

You are encountering problems with locales. POSIX mandates that

sed -i 's/hello/world/' file.txt
0072 uses the current locale’s collation order – in C parlance, that means using
sed -i 's/hello/world/' file.txt
0074 instead of
sed -i 's/hello/world/' file.txt
0075. Some locales have a case-insensitive collation order, others don’t

Another problem is that

sed -i 's/hello/world/' file.txt
0072 tries to use collation symbols. This only happens if you are on the GNU system, using GNU libc’s regular expression matcher instead of compiling the one supplied with GNU sed. In a Danish locale, for example, the regular expression
sed -i 's/hello/world/' file.txt
0077 matches the string ‘aa’, because this is a single collating symbol that comes after ‘a’ and before ‘b’; ‘ll’ behaves similarly in Spanish locales, or ‘ij’ in Dutch locales

To work around these problems, which may cause bugs in shell scripts, set the

sed -i 's/hello/world/' file.txt
0078 and
sed 's/hello/world/' input.txt > output.txt
946 environment variables to ‘C’

sed 's/hello/world/' input.txt > output.txt
926 does not clear pattern space

This happens if your input stream includes invalid multibyte sequences. POSIX mandates that such sequences are not matched by ‘. ’, so that ‘s/. *//’ will not clear pattern space as you would expect. In fact, there is no way to clear sed’s buffers in the middle of the script in most multibyte locales (including UTF-8 locales). For this reason, GNU

sed -i 's/hello/world/' file.txt
22 provides a ‘z’ command (for ‘zap’) as an extension

To work around these problems, which may cause bugs in shell scripts, set the

sed -i 's/hello/world/' file.txt
0078 and
sed 's/hello/world/' input.txt > output.txt
946 environment variables to ‘C’


Appendix A GNU Free Documentation License

Version 1. 3, 3 November 2008

sed -i 's/hello/world/' file.txt
19

  1. PREAMBLE

    The purpose of this License is to make a manual, textbook, or other functional and useful document free in the sense of freedom. to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others

    This License is a kind of “copyleft”, which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General Public License, which is a copyleft license designed for free software

    Chúng tôi đã thiết kế Giấy phép này để sử dụng nó cho hướng dẫn sử dụng phần mềm miễn phí, bởi vì phần mềm miễn phí cần tài liệu miễn phí. a free program should come with manuals providing the same freedoms that the software does. But this License is not limited to software manuals; it can be used for any textual work, regardless of subject matter or whether it is published as a printed book. We recommend this License principally for works whose purpose is instruction or reference

  2. APPLICABILITY AND DEFINITIONS

    This License applies to any manual or other work, in any medium, that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License. Such a notice grants a world-wide, royalty-free license, unlimited in duration, to use that work under the conditions stated herein. The “Document”, below, refers to any such manual or work. Any member of the public is a licensee, and is addressed as “you”. You accept the license if you copy, modify or distribute the work in a way requiring permission under copyright law

    A “Modified Version” of the Document means any work containing the Document or a portion of it, either copied verbatim, or with modifications and/or translated into another language

    A “Secondary Section” is a named appendix or a front-matter section of the Document that deals exclusively with the relationship of the publishers or authors of the Document to the Document’s overall subject (or to related matters) and contains nothing that could fall directly within that overall subject. (Thus, if the Document is in part a textbook of mathematics, a Secondary Section may not explain any mathematics. ) The relationship could be a matter of historical connection with the subject or with related matters, or of legal, commercial, philosophical, ethical or political position regarding them

    The “Invariant Sections” are certain Secondary Sections whose titles are designated, as being those of Invariant Sections, in the notice that says that the Document is released under this License. Nếu một phần không phù hợp với định nghĩa ở trên về Thứ yếu thì nó không được phép chỉ định là Bất biến. The Document may contain zero Invariant Sections. If the Document does not identify any Invariant Sections then there are none

    The “Cover Texts” are certain short passages of text that are listed, as Front-Cover Texts or Back-Cover Texts, in the notice that says that the Document is released under this License. A Front-Cover Text may be at most 5 words, and a Back-Cover Text may be at most 25 words

    A “Transparent” copy of the Document means a machine-readable copy, represented in a format whose specification is available to the general public, that is suitable for revising the document straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup, or absence of markup, has been arranged to thwart or discourage subsequent modification by readers is not Transparent. An image format is not Transparent if used for any substantial amount of text. A copy that is not “Transparent” is called “Opaque”

    Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standard-conforming simple HTML, PostScript or PDF designed for human modification. Examples of transparent image formats include PNG, XCF and JPG. Opaque formats include proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machine-generated HTML, PostScript or PDF produced by some word processors for output purposes only

    The “Title Page” means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, “Title Page” means the text near the most prominent appearance of the work’s title, preceding the beginning of the body of the text

    The “publisher” means any person or entity that distributes copies of the Document to the public

    A section “Entitled XYZ” means a named subunit of the Document whose title either is precisely XYZ or contains XYZ in parentheses following text that translates XYZ in another language. (Here XYZ stands for a specific section name mentioned below, such as “Acknowledgements”, “Dedications”, “Endorsements”, or “History”. ) To “Preserve the Title” of such a section when you modify the Document means that it remains a section “Entitled XYZ” according to this definition

    The Document may include Warranty Disclaimers next to the notice which states that this License applies to the Document. These Warranty Disclaimers are considered to be included by reference in this License, but only as regards disclaiming warranties. any other implication that these Warranty Disclaimers may have is void and has no effect on the meaning of this License

  3. VERBATIM COPYING

    You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this License, the copyright notices, and the license notice saying this License applies to the Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to obstruct or control the reading or further copying of the copies you make or distribute. However, you may accept compensation in exchange for copies. If you distribute a large enough number of copies you must also follow the conditions in section 3

    You may also lend copies, under the same conditions stated above, and you may publicly display copies

  4. COPYING IN QUANTITY

    If you publish printed copies (or copies in media that commonly have printed covers) of the Document, numbering more than 100, and the Document’s license notice requires Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all these Cover Texts. Front-Cover Texts on the front cover, and Back-Cover Texts on the back cover. Both covers must also clearly and legibly identify you as the publisher of these copies. The front cover must present the full title with all words of the title equally prominent and visible. You may add other material on the covers in addition. Copying with changes limited to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be treated as verbatim copying in other respects

    If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed (as many as fit reasonably) on the actual cover, and continue the rest onto adjacent pages

    If you publish or distribute Opaque copies of the Document numbering more than 100, you must either include a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a computer-network location from which the general network-using public has access to download using public-standard network protocols a complete Transparent copy of the Document, free of added material. If you use the latter option, you must take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy (directly or through your agents or retailers) of that edition to the public

    It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document

  5. MODIFICATIONS

    Bạn có thể sao chép và phân phối Phiên bản đã sửa đổi của Tài liệu theo các điều kiện của phần 2 và 3 ở trên, miễn là bạn phát hành Phiên bản đã sửa đổi theo đúng Giấy phép này, với Phiên bản đã sửa đổi đóng vai trò của Tài liệu, do đó cấp phép phân phối và sửa đổi . In addition, you must do these things in the Modified Version

    1. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those of previous versions (which should, if there were any, be listed in the History section of the Document). You may use the same title as a previous version if the original publisher of that version gives permission
    2. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modifications in the Modified Version, together with at least five of the principal authors of the Document (all of its principal authors, if it has fewer than five), unless they release you from this requirement
    3. State on the Title page the name of the publisher of the Modified Version, as the publisher
    4. Preserve all the copyright notices of the Document
    5. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices
    6. Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified Version under the terms of this License, in the form shown in the Addendum below
    7. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document’s license notice
    8. Include an unaltered copy of this License
    9. Preserve the section Entitled “History”, Preserve its Title, and add to it an item stating at least the title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no section Entitled “History” in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modified Version as stated in the previous sentence
    10. Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document, and likewise the network locations given in the Document for previous versions it was based on. These may be placed in the “History” section. You may omit a network location for a work that was published at least four years before the Document itself, or if the original publisher of the version it refers to gives permission
    11. For any section Entitled “Acknowledgements” or “Dedications”, Preserve the Title of the section, and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein
    12. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles
    13. Delete any section Entitled “Endorsements”. Such a section may not be included in the Modified Version
    14. Do not retitle any existing section to be Entitled “Endorsements” or to conflict in title with any Invariant Section
    15. Preserve any Warranty Disclaimers

    If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document, you may at your option designate some or all of these sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version’s license notice. These titles must be distinct from any other section titles

    You may add a section Entitled “Endorsements”, provided it contains nothing but endorsements of your Modified Version by various parties—for example, statements of peer review or that the text has been approved by an organization as the authoritative definition of a standard

    You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover Text and one of Back-Cover Text may be added by (or through arrangements made by) any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one

    The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modified Version

  6. COMBINING DOCUMENTS

    You may combine the Document with other documents released under this License, under the terms defined in section 4 above for modified versions, provided that you include in the combination all of the Invariant Sections of all of the original documents, unmodified, and list them all as Invariant Sections of your combined work in its license notice, and that you preserve all their Warranty Disclaimers

    The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents, make the title of each such section unique by adding at the end of it, in parentheses, the name of the original author or publisher of that section if known, or else a unique number. Make the same adjustment to the section titles in the list of Invariant Sections in the license notice of the combined work

    In the combination, you must combine any sections Entitled “History” in the various original documents, forming one section Entitled “History”; likewise combine any sections Entitled “Acknowledgements”, and any sections Entitled “Dedications”. You must delete all sections Entitled “Endorsements. ”

  7. COLLECTIONS OF DOCUMENTS

    You may make a collection consisting of the Document and other documents released under this License, and replace the individual copies of this License in the various documents with a single copy that is included in the collection, provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects

    You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy of this License into the extracted document, and follow this License in all other respects regarding verbatim copying of that document

  8. AGGREGATION WITH INDEPENDENT WORKS

    A compilation of the Document or its derivatives with other separate and independent documents or works, in or on a volume of a storage or distribution medium, is called an “aggregate” if the copyright resulting from the compilation is not used to limit the legal rights of the compilation’s users beyond what the individual works permit. When the Document is included in an aggregate, this License does not apply to the other works in the aggregate which are not themselves derivative works of the Document

    If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document is less than one half of the entire aggregate, the Document’s Cover Texts may be placed on covers that bracket the Document within the aggregate, or the electronic equivalent of covers if the Document is in electronic form. Otherwise they must appear on printed covers that bracket the whole aggregate

  9. TRANSLATION

    Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License, and all the license notices in the Document, and any Warranty Disclaimers, provided that you also include the original English version of this License and the original versions of those notices and disclaimers. In case of a disagreement between the translation and the original version of this License or a notice or disclaimer, the original version will prevail

    If a section in the Document is Entitled “Acknowledgements”, “Dedications”, or “History”, the requirement (section 4) to Preserve its Title (section 1) will typically require changing the actual title

  10. TERMINATION

    You may not copy, modify, sublicense, or distribute the Document except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, or distribute it is void, and will automatically terminate your rights under this License

    However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation

    Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice

    Việc chấm dứt quyền của bạn theo phần này không chấm dứt giấy phép của các bên đã nhận bản sao hoặc quyền từ bạn theo Giấy phép này. If your rights have been terminated and not permanently reinstated, receipt of a copy of some or all of the same material does not give you any rights to use it

  11. FUTURE REVISIONS OF THIS LICENSE

    The Free Software Foundation may publish new, revised versions of the GNU Free Documentation License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. See https. //www. gnu. org/copyleft/

    Each version of the License is given a distinguishing version number. If the Document specifies that a particular numbered version of this License “or any later version” applies to it, you have the option of following the terms and conditions either of that specified version or of any later version that has been published (not as a draft) by the Free Software Foundation. If the Document does not specify a version number of this License, you may choose any version ever published (not as a draft) by the Free Software Foundation. If the Document specifies that a proxy can decide which future versions of this License can be used, that proxy’s public statement of acceptance of a version permanently authorizes you to choose that version for the Document

  12. GIẤY PHÉP

    “Trang web cộng tác nhiều tác giả lớn” (hoặc “Trang web MMC”) có nghĩa là bất kỳ máy chủ World Wide Web nào xuất bản các tác phẩm có bản quyền và cũng cung cấp các phương tiện nổi bật để bất kỳ ai chỉnh sửa các tác phẩm đó. A public wiki that anybody can edit is an example of such a server. A “Massive Multiauthor Collaboration” (or “MMC”) contained in the site means any set of copyrightable works thus published on the MMC site

    “CC-BY-SA” means the Creative Commons Attribution-Share Alike 3. 0 license published by Creative Commons Corporation, a not-for-profit corporation with a principal place of business in San Francisco, California, as well as future copyleft versions of that license published by that same organization

    “Incorporate” means to publish or republish a Document, in whole or in part, as part of another Document

    An MMC is “eligible for relicensing” if it is licensed under this License, and if all works that were first published under this License somewhere other than this MMC, and subsequently incorporated in whole or in part into the MMC, (1) had no cover texts or invariant sections, and (2) were thus incorporated prior to November 1, 2008

    The operator of an MMC Site may republish an MMC contained in the site under CC-BY-SA on the same site at any time before August 1, 2009, provided the MMC is eligible for relicensing

ADDENDUM. How to use this License for your documents

To use this License in a document you have written, include a copy of the License in the document and put the following copyright and license notices just after the title page

sed -i 's/hello/world/' file.txt
20

If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the “with…Texts. ” line with this

sed -i 's/hello/world/' file.txt
21

If you have Invariant Sections without Cover Texts, or some other combination of the three, merge those two alternatives to suit the situation

If your document contains nontrivial examples of program code, we recommend releasing these examples in parallel under your choice of free software license, such as the GNU General Public License, to permit their use in free software


Concept Index

This is a general index of all issues discussed in this manual, with the exception of the

sed -i 's/hello/world/' file.txt
22 commands and command-line options