Dấu ngoặc đơn regex trong python

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; . Một bản sao của giấy phép được bao gồm trong phần có tiêu đề “Giấy phép Tài liệu Miễn phí GNU”

1. Giới thiệu

sed -i 's/hello/world/' file.txt
22 là trình chỉnh sửa luồng. Trình chỉnh sửa luồng được sử dụng để thực hiện các chuyển đổi văn bản cơ bản trên luồng đầu vào [tệp hoặc đầu vào từ đường dẫn]. Mặc dù theo một số cách tương tự như một trình soạn thảo cho phép chỉnh sửa theo kịch bản [chẳng hạn như
sed -i 's/hello/world/' file.txt
24],
sed -i 's/hello/world/' file.txt
22 hoạt động bằng cách chỉ thực hiện một lần chuyển qua [các] đầu vào và do đó hiệu quả hơn. 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 Chạy sed

Chương này trình bày cách chạy

sed -i 's/hello/world/' file.txt
22. Chi tiết về các tập lệnh
sed -i 's/hello/world/' file.txt
22 và các lệnh
sed -i 's/hello/world/' file.txt
22 riêng lẻ sẽ được thảo luận trong chương tiếp theo

2. 1. Sơ lượt

Thông thường

sed -i 's/hello/world/' file.txt
22 được gọi như thế này

Ví dụ: để thay thế tất cả các lần xuất hiện của 'xin chào' thành 'thế giới' trong đầu vào tệp. txt

________số 8

Nếu bạn không chỉ định INPUTFILE hoặc nếu INPUTFILE là -, thì

sed -i 's/hello/world/' file.txt
22 sẽ lọc nội dung của đầu vào tiêu chuẩn. Các lệnh sau đây là tương đương

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

sed -i 's/hello/world/' file.txt
22 ghi đầu ra thành đầu ra tiêu chuẩn. Sử dụng -i để chỉnh sửa tệp tại chỗ thay vì in thành đầu ra tiêu chuẩn. Xem thêm các lệnh
sed -i 's/hello/world/' file.txt
33 và
sed -i 's/hello/world/' file.txt
34 để ghi đầu ra vào các tệp khác. 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

________ 055 ________ 056

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

________ 065 ________ 066

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. ]

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

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

Executes command and sends its output to the output stream. 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'. Nếu hành vi này là không mong muốn, bạn có thể ngăn chặn nó bằng cách thêm một chuỗi '\E'—sau '\1' trong trường hợp này

Để 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

Only replace the numberth match of the regexp

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ác 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 các 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à. ignore matches before the numberth, and then match and replace all matches from the numberth on

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

If the substitution was made, then print the new pattern space

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. process every Nth line]

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

Một nhóm lệnh có thể được đặt giữa các ký tự

sed -i 's/hello/world/' file.txt
178 và
sed -i 's/hello/world/' file.txt
179. Điều này đặc biệt hữu ích khi bạn muốn một nhóm lệnh được kích hoạt bởi một địa chỉ [hoặc dải địa chỉ] phù hợp

Thí dụ. thực hiện thay thế sau đó in dòng đầu vào thứ hai

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

3. 5 lệnh ít sử dụng hơn

Mặc dù có lẽ ít được sử dụng hơn so với các tập lệnh trong phần trước, một số tập lệnh

sed -i 's/hello/world/' file.txt
22 rất nhỏ nhưng hữu ích có thể được tạo bằng các lệnh này

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

Thí dụ. phiên âm 'a-j' thành '0-9'

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

[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
183 đã cho nào. ]

Các trường hợp của

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ó],
sed -i 's/hello/world/' file.txt
997 hoặc các dòng mới có thể xuất hiện trong danh sách ký tự nguồn hoặc ký tự đích, với điều kiện là mỗi trường hợp được thoát bởi một
sed -i 's/hello/world/' file.txt
997. Danh sách ký tự nguồn và ký tự đích phải chứa cùng số lượng ký tự [sau khi thoát]

Xem lệnh

sed -i 's/hello/world/' file.txt
187 từ GNU coreutils để biết chức năng tương tự

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

Nối văn bản sau một dòng. Đây là phần mở rộng GNU cho lệnh

sed -i 's/hello/world/' file.txt
926 tiêu chuẩn - xem bên dưới để biết chi tiết

Example. Thêm từ 'xin chào' sau dòng thứ hai

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

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

sed -i 's/hello/world/' file.txt
926 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
931
sed -i 's/hello/world/' file.txt
932

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

Thí dụ. Thêm 'xin chào' sau dòng thứ hai [-. cho biết các dòng đầu ra được in]

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

Lệnh

sed -i 's/hello/world/' file.txt
926 xếp hàng đợi các dòng văn bản tuân theo lệnh này [mỗi dòng trừ phần cuối cùng bằng
sed -i 's/hello/world/' file.txt
997, được xóa khỏi đầu ra] để xuất ra ở cuối chu kỳ hiện tại hoặc khi dòng đầu vào tiếp theo được đọc

Là một phần mở rộng GNU, lệnh này chấp nhận hai địa chỉ

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

Các lệnh tiếp tục sau dòng cuối cùng mà không có dấu gạch chéo ngược [

sed -i 's/hello/world/' file.txt
997] - ‘world’ trong ví dụ sau

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

Là một tiện ích mở rộng GNU, lệnh và văn bản

sed -i 's/hello/world/' file.txt
926 có thể được tách thành hai tham số
sed -i 's/hello/world/' file.txt
198, cho phép tạo tập lệnh dễ dàng hơn

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

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

chèn văn bản trước một dòng. Đây là phần mở rộng GNU cho lệnh

sed -i 's/hello/world/' file.txt
928 tiêu chuẩn - xem bên dưới để biết chi tiết

Thí dụ. Chèn từ 'xin chào' trước dòng thứ hai

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

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

sed -i 's/hello/world/' file.txt
928 bị bỏ qua. 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

Xuất ngay các dòng văn bản theo lệnh này

Thí dụ. Chèn 'xin chào' trước dòng thứ hai [-. cho biết các dòng đầu ra được in]

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

Là một phần mở rộng GNU, lệnh này chấp nhận hai địa chỉ

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

Các lệnh tiếp tục sau dòng cuối cùng mà không có dấu gạch chéo ngược [

sed -i 's/hello/world/' file.txt
997] - ‘world’ trong ví dụ sau

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

Là một tiện ích mở rộng GNU, lệnh và văn bản

sed -i 's/hello/world/' file.txt
928 có thể được tách thành hai tham số
sed -i 's/hello/world/' file.txt
198, cho phép tạo tập lệnh dễ dàng hơn

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

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

Thay thế [các] dòng bằng văn bản. Đây là phần mở rộng GNU cho lệnh

sed -i 's/hello/world/' file.txt
927 tiêu chuẩn - xem bên dưới để biết chi tiết

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ụ. Thay thế dòng thứ 2 đến thứ 4 bằng các từ 'xin chào' và 'thế giới' [-. 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

Là một tiện ích mở rộng GNU, lệnh và văn bản

sed -i 's/hello/world/' file.txt
927 có thể được tách thành hai tham số
sed -i 's/hello/world/' file.txt
198, cho phép tạo tập lệnh dễ dàng hơn

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

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

In ra 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
8

Là một phần mở rộng GNU, lệnh này chấp nhận hai địa chỉ

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

In không gian mẫu ở dạng rõ ràng. các ký tự không in được [và ký tự

sed -i 's/hello/world/' file.txt
997] được in ở dạng thoát kiểu C;

n chỉ định độ dài ngắt dòng mong muốn; . Nếu bỏ qua, giá trị mặc định như được chỉ định trên dòng lệnh sẽ được sử dụng. 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

Đọc tên tập tin. Thí dụ

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

Xếp hàng nội dung của 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. Lưu ý rằng nếu tên tệp không thể đọc được, nó sẽ được coi như một tệp trống, không có bất kỳ dấu hiệu lỗi nào

Là một tiện ích mở rộng GNU

sed -i 's/hello/world/' file.txt
22, giá trị đặc biệt /dev/stdin được hỗ trợ cho tên tệp, đọc nội dung của đầu vào tiêu chuẩn

Là một phần mở rộng GNU, lệnh này chấp nhận hai địa chỉ. Sau đó, tệp sẽ được đọc lại và chèn vào từng dòng được giải quyết

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

Viết không gian mẫu vào tên tệp. 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. 4

Tệp sẽ được tạo [hoặc cắt bớt] trước khi dòng đầu vào đầu tiên được đọc;

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

Trong hầu hết các trường hợp, việc sử dụng các lệnh này cho thấy rằng bạn có thể lập trình tốt hơn bằng thứ gì đó như

sed -i 's/hello/world/' file.txt
341 hoặc Perl. Nhưng đôi khi một người cam kết gắn bó với
sed -i 's/hello/world/' file.txt
22 và các lệnh này có thể cho phép một người viết các tập lệnh khá phức tạp

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

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. 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

This command allows one to pipe input from a shell command into pattern space. 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

In both cases, the results are undefined if the command to be executed contains a NUL character

Note that, unlike the

sed -i 's/hello/world/' file.txt
354 command, the output of the command will be printed immediately; the
sed -i 's/hello/world/' file.txt
354 command instead delays the output to the end of the current cycle

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

Print out the file name of the current input file [with a trailing newline]

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. 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
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. Giá trị mặc định là
sed -i 's/hello/world/' file.txt
370 vì đó là phiên bản đầu tiên triển khai lệnh này

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

Các nhãn được sử dụng trong các lệnh

sed -i 's/hello/world/' file.txt
982,______1983,
sed -i 's/hello/world/' file.txt
984,
sed -i 's/hello/world/' file.txt
383 được đọc cho đến khi có dấu chấm phẩy. Khoảng trắng ở đầu và cuối bị bỏ qua. Trong các ví dụ bên dưới nhãn là 'x'. Ví dụ đầu tiên hoạt động với GNU
sed -i 's/hello/world/' file.txt
22. Thứ hai là tương đương di động. Để biết thêm thông tin về phân nhánh và nhãn, hãy xem Phân nhánh và kiểm soát luồng

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

3. 8. 1 Lệnh Yêu cầu một dòng mới

Các lệnh sau không thể được phân tách bằng dấu chấm phẩy và yêu cầu một dòng mới

sed -i 's/hello/world/' file.txt
926,______1927,______1928 [nối thêm/thay đổi/chèn vào]

Tất cả các ký tự sau các lệnh

sed -i 's/hello/world/' file.txt
926,______1927,
sed -i 's/hello/world/' file.txt
928 được lấy làm văn bản để nối/thay đổi/chèn. Sử dụng dấu chấm phẩy dẫn đến kết quả không mong muốn

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

Tách các lệnh bằng cách sử dụng -e hoặc một dòng mới

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. Một giải pháp thay thế di động, tuân thủ POSIX là

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

sed -i 's/hello/world/' file.txt
978 [bình luận]

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 [đọc và ghi tệp]

Các lệnh

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 phân tích tên tệp cho đến cuối dòng. 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 tên tệp, dẫn đến kết quả không mong muốn

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

Lưu ý rằng

sed -i 's/hello/world/' file.txt
22 âm thầm bỏ qua các lỗi đọc/ghi trong các lệnh
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 [chẳng hạn như các tệp bị thiếu]. Trong ví dụ sau,
sed -i 's/hello/world/' file.txt
22 cố gắng đọc một tệp có tên 'hello. văn bản ; . Tệp bị thiếu và lỗi được âm thầm bỏ qua

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

sed -i 's/hello/world/' file.txt
941 [thi hành mệnh lệnh]

Bất kỳ ký tự nào sau lệnh

sed -i 's/hello/world/' file.txt
941 cho đến cuối dòng sẽ được gửi đến trình bao. 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 [thay thế bằng cờ
sed -i 's/hello/world/' file.txt
941 hoặc
sed -i 's/hello/world/' file.txt
326]

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. Lệnh sau thay thế từ 'xin chào' bằng 'thế giới' trên tất cả các dòng trong tệp đầu vào

________số 8

Addresses can contain regular expressions to match lines based on content instead of line numbers. Lệnh sau chỉ thay thế từ 'hello' bằng 'world' trong các dòng có chứa từ 'apple'

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

Một dải địa chỉ được chỉ định với hai địa chỉ được phân tách bằng dấu phẩy [

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 Selecting lines by numbers

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. [Lưu ý rằng

sed -i 's/hello/world/' file.txt
22 đếm các dòng liên tục trên tất cả các tệp đầu vào trừ khi các tùy chọn -i hoặc -s được chỉ định. ]

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. Nếu bản thân biểu thức chính quy bao gồm bất kỳ ký tự

sed -i 's/hello/world/' file.txt
994 nào, thì mỗi ký tự đó phải được thoát bằng dấu gạch chéo ngược [
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

[Có thể thay thế

sed -i 's/hello/world/' file.txt
641 bằng bất kỳ ký tự đơn nào khác. ]

Điều này cũng phù hợp với biểu thức chính quy biểu thức chính quy, nhưng cho phép một người sử dụng dấu phân cách khác với

sed -i 's/hello/world/' file.txt
994. Điều này đặc biệt hữu ích nếu bản thân biểu thức chính quy chứa nhiều dấu gạch chéo, vì nó tránh được việc thoát tẻ nhạt của mỗi
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

In this example,

sed -i 's/hello/world/' file.txt
651 is the address. regular expression with
sed -i 's/hello/world/' file.txt
145 modifier.
sed -i 's/hello/world/' file.txt
36 is the delete command

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

Here,

sed -i 's/hello/world/' file.txt
654 is the address. a regular expression.
sed -i 's/hello/world/' file.txt
928 is the insert command.
sed -i 's/hello/world/' file.txt
36 is the value to insert. A line with ‘d’ is then inserted above the matched line

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

Regex addresses operate on the content of the current pattern space. 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

In the following example, automatic printing is disabled with -n. The

sed -i 's/hello/world/' file.txt
667 command changes lines containing ‘2’ to ‘X’. The command
sed -i 's/hello/world/' file.txt
668 matches lines with digits and prints them. Because the second line is changed before the
sed -i 's/hello/world/' file.txt
669 regex, it will not match and will not be printed

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

4. 4 Range Addresses

An address range can be specified by specifying two addresses separated by a comma [

sed -i 's/hello/world/' file.txt
624]. An address range matches lines starting from where the first address matches, and continues until the second address matches [inclusively]

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

If the second address is a regexp, then checking for the ending match will start with the line following the line which matched the first address. a range will always span at least two lines [except of course if the input stream ends]

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

If the second address is a number less than [or equal to] the line matching the first address, then only the one line is matched

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

GNU

sed -i 's/hello/world/' file.txt
22 also supports some special two-address forms; all these are GNU extensions

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

A line number of

sed -i 's/hello/world/' file.txt
673 can be used in an address specification like
sed -i 's/hello/world/' file.txt
672 so that
sed -i 's/hello/world/' file.txt
22 will try to match regexp in the first input line too. In other words,
sed -i 's/hello/world/' file.txt
672 is similar to
sed -i 's/hello/world/' file.txt
677, except that if addr2 matches the very first line of input the
sed -i 's/hello/world/' file.txt
672 form will consider it to end the range, whereas the
sed -i 's/hello/world/' file.txt
677 form will match the beginning of its range and hence make the range span up to the second occurrence of the regular expression

Note that this is the only place where the

sed -i 's/hello/world/' file.txt
673 address makes sense; there is no 0-th line and commands which are given the
sed -i 's/hello/world/' file.txt
673 address in any other way will give an error

The following examples demonstrate the difference between starting with address 1 and 0

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

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

Matches addr1 and the N lines following addr1

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

addr1 can be a line number or a regular expression

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

Matches addr1 and the lines following addr1 until the next line whose input line number is a multiple of 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. line 8]

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

addr1 can be a line number or a regular expression

5 Regular Expressions. selecting text

5. 1 Overview of regular expression in
sed -i 's/hello/world/' file.txt
22

To know how to use

sed -i 's/hello/world/' file.txt
22, people should understand regular expressions [regexp for short]. A regular expression is a pattern that is matched against a subject string from left to right. Most characters are ordinary. they stand for themselves in a pattern, and match the corresponding characters. Regular expressions in
sed -i 's/hello/world/' file.txt
22 are specified between two slashes

The following command prints lines containing the word ‘hello’

The above example is equivalent to this

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

The power of regular expressions comes from the ability to include alternatives and repetitions in the pattern. These are encoded in the pattern by the use of special characters, which do not stand for themselves but instead are interpreted in some special way

The character

sed -i 's/hello/world/' file.txt
154 [caret] in a regular expression matches the beginning of the line. The character
sed -i 's/hello/world/' file.txt
689 [dot] matches any single character. The following
sed -i 's/hello/world/' file.txt
22 command matches and prints lines which start with the letter ‘b’, followed by any single character, followed by the letter ‘d’

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

The following sections explain the meaning and usage of special characters in regular expressions

5. 2 Basic [BRE] and extended [ERE] regular expression

Basic and extended regular expressions are two variations on the syntax of the specified pattern. Basic Regular Expression [BRE] syntax is the default in

sed -i 's/hello/world/' file.txt
22 [and similarly in
sed -i 's/hello/world/' file.txt
687]. Use the POSIX-specified -E option [-r, --regexp-extended] to enable Extended Regular Expression [ERE] syntax

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 ['\']; . these characters are special unless they are prefixed with backslash [‘\’]

Desired patternBasic [BRE] SyntaxExtended [ERE] Syntaxliteral ‘+’ [plus sign]

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

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

One or more ‘a’ characters followed by ‘b’ [plus sign as special meta-character]

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

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

5. 3 Overview of basic regular expression syntax

Here is a brief description of regular expression syntax as used in

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

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

A single ordinary character matches itself

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. As a GNU extension, a postfixed regular expression can also be followed by
sed -i 's/hello/world/' file.txt
60; for example,
sed -i 's/hello/world/' file.txt
900 is equivalent to
sed -i 's/hello/world/' file.txt
901. POSIX 1003. 1-2001 says that
sed -i 's/hello/world/' file.txt
60 stands for itself when it appears at the start of a regular expression or subexpression, but many nonGNU implementations do not support this and portable scripts should instead use
sed -i 's/hello/world/' file.txt
903 in these contexts

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

Matches any character, including newline

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

Matches the null string at beginning of the pattern space, i. e. what appears after the circumflex must appear at the beginning of the pattern space

In most scripts, pattern space is initialized to the content of each line [see How

sed -i 's/hello/world/' file.txt
22 works]. So, it is a useful simplification to think of
sed -i 's/hello/world/' file.txt
907 as matching only lines where ‘#include’ is the first thing on line—if there are spaces before, for example, the match fails. This simplification is valid as long as the original content of pattern space is not modified, for example with an
sed -i 's/hello/world/' file.txt
967 command

sed -i 's/hello/world/' file.txt
154 acts as a special character only at the beginning of the regular expression or subexpression [that is, after
sed -i 's/hello/world/' file.txt
991 or
sed -i 's/hello/world/' file.txt
911]. Portable scripts should avoid
sed -i 's/hello/world/' file.txt
154 at the beginning of a subexpression, though, as POSIX allows implementations that treat
sed -i 's/hello/world/' file.txt
154 as an ordinary character in that context

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

It is the same as

sed -i 's/hello/world/' file.txt
154, but refers to end of pattern space.
sed -i 's/hello/world/' file.txt
87 also acts as a special character only at the end of the regular expression or subexpression [that is, before
sed -i 's/hello/world/' file.txt
992 or
sed -i 's/hello/world/' file.txt
911], and its use at the end of a subexpression is not portable

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. for example,

sed -i 's/hello/world/' file.txt
921 matches all vowels. A list may include sequences like
sed -i 's/hello/world/' file.txt
922, which matches any character between [inclusive] char1 and char2. See Character Classes and Bracket Expressions

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. It is a GNU extension

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

As

sed -i 's/hello/world/' file.txt
60, but only matches zero or one. It is a GNU extension

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

As

sed -i 's/hello/world/' file.txt
60, but matches exactly i sequences [i is a decimal integer; for portability, keep it between 0 and 255 inclusive]

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

Matches between i and j, inclusive, sequences

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

Matches more than or equal to i sequences

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

Groups the inner regexp as a whole, this is used to

  • Apply postfix operators, like
    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’. Note that support for
    sed -i 's/hello/world/' file.txt
    
    932 is required by POSIX 1003. 1-2001, but many non-GNU implementations do not support it and hence it is not universally portable
  • Use back references [see below]
sed -i 's/hello/world/' file.txt
935

Matches either regexp1 or regexp2. Use parentheses to use complex alternative regular expressions. The matching process tries each alternative in turn, from left to right, and the first one that succeeds is used. It is a GNU extension

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

Matches the concatenation of regexp1 and regexp2. Concatenation binds more tightly than

sed -i 's/hello/world/' file.txt
911,
sed -i 's/hello/world/' file.txt
154, and
sed -i 's/hello/world/' file.txt
87, but less tightly than the other regular expression operators

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

Matches the digit-th

sed -i 's/hello/world/' file.txt
941 parenthesized subexpression in the regular expression. Đ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;

Note that the regular expression matcher is greedy, i. e. , matches are attempted from left to right and, if two or more matches are possible starting at the same character, it selects the longest

Examples

‘abcdef’

Matches ‘abcdef’

‘a*b’

Matches zero or more ‘a’s followed by a single ‘b’. For example, ‘b’ or ‘aaaaab’

‘a\?b’

Matches ‘b’ or ‘ab’

‘a\+b\+’

Matches one or more ‘a’s followed by one or more ‘b’s. ‘ab’ is the shortest possible match, but other examples are ‘aaaab’ or ‘abbbbb’ or ‘aaaaaabbbbbbb’

‘. *’‘. \+’

These two both match all the characters in a string; however, the first matches every string [including the empty string], while the second matches only strings containing at least one character

‘^main. *[. *]’

This matches a string starting with ‘main’, followed by an opening and closing parenthesis. The ‘n’, ‘[’ and ‘]’ need not be adjacent

‘^#’

This matches a string beginning with ‘#’

‘\\$’

This matches a string ending with a single backslash. The regexp contains two backslashes for escaping

‘\$’

Instead, this matches a string consisting of a single dollar sign, because it is escaped

‘[a-zA-Z0-9]’

In the C locale, this matches any ASCII letters or digits

‘[^ TAB]\+’

[Here TAB stands for a single tab character. ] This matches a string of one or more characters, none of which is a space or a tab. Usually this means a word

‘^\[. *\]\n\1$’

This matches a string consisting of two equal substrings separated by a newline

‘. \{9\}A$’

This matches nine characters followed by an ‘A’ at the end of a line

‘^. \{15\}A’

This matches the start of a string that contains 16 characters, the last of which is an ‘A’

5. 4 Overview of extended regular expression syntax

The only difference between basic and extended regular expressions is in the behavior of a few characters. ‘?’, ‘+’, parentheses, braces [‘{}’], and ‘. ’. While basic regular expressions require these to be escaped if you want them to behave as special characters, when using extended regular expressions you must escape them if you want them to match a literal character. ‘. ’ is special here because ‘\. ’ is a GNU extension – standard basic regular expressions do not provide its functionality

Examples

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

becomes ‘abc\?’ when using extended regular expressions. It matches the literal string ‘abc?’

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

becomes ‘c+’ when using extended regular expressions. It matches one or more ‘c’s

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

becomes ‘a{3,}’ when using extended regular expressions. It matches three or more ‘a’s

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

becomes ‘[abc]{2,3}’ when using extended regular expressions. It matches either ‘abcabc’ or ‘abcabcabc’

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

becomes ‘[abc*]\1’ when using extended regular expressions. Backreferences must still be escaped when using extended regular expressions

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

becomes ‘a. b’ when using extended regular expressions. Nó khớp với 'a' hoặc 'b'

5. 5 Character Classes and Bracket Expressions

Biểu thức ngoặc là một danh sách các ký tự được bao quanh bởi '[' và ']'. It matches any single character in that list; if the first character of the list is the caret ‘^’, then it matches any character not in the list. For example, the following command replaces the words ‘gray’ or ‘grey’ with ‘blue’

Bracket expressions can be used in both basic and extended regular expressions [that is, with or without the -E/-r options]

Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive. Trong ngôn ngữ C mặc định, trình tự sắp xếp là thứ tự ký tự gốc;

Finally, certain named classes of characters are predefined within bracket expressions, as follows

These named classes must be used inside brackets themselves. Cách sử dụng đúng

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

Việc sử dụng không chính xác bị từ chối bởi các phiên bản

sed -i 's/hello/world/' file.txt
22 mới hơn. Older versions accepted it but treated it as a single bracket expression [which is equivalent to ‘[dgit. ]’, that is, only the characters d/g/i/t/. ]

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

‘[. alnum. ]’

Ký tự chữ và số. ‘[. alpha. ]’ and ‘[. chữ số. ]’; in the ‘C’ locale and ASCII character encoding, this is the same as ‘[0-9A-Za-z]’

‘[. alpha. ]’

Alphabetic characters. ‘[. thấp hơn. ]’ and ‘[. phía trên. ]’; in the ‘C’ locale and ASCII character encoding, this is the same as ‘[A-Za-z]’

‘[. blank. ]’

Blank characters. space and tab

‘[. cntrl. ]’

Control characters. In ASCII, these characters have octal codes 000 through 037, and 177 [DEL]. In other character sets, these are the equivalent characters, if any

‘[. digit. ]'

Digits.

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

‘[. graph. ]’

ký tự đồ họa. ‘[. alnum. ]’ and ‘[. punct. ]’

‘[. lower. ]’

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

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

‘[. print. ]’

Printable characters. ‘[. alnum. ]’, ‘[. punct. ]’, and space

‘[. punct. ]’

Punctuation characters; in the ‘C’ locale and ASCII character encoding, this is

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

‘[. space. ]’

Space characters. in the ‘C’ locale, this is tab, newline, vertical tab, form feed, carriage return, and space

‘[. upper. ]’

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

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

‘[. xdigit. ]’

Hexadecimal digits.

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

Note that the brackets in these class names are part of the symbolic names, and must be included in addition to the brackets delimiting the bracket expression

Most meta-characters lose their special meaning inside bracket expressions

‘]’

ends the bracket expression if it’s not the first list item. So, if you want to make the ‘]’ character a list item, you must put it first

‘-’

represents the range if it’s not first or last in a list or the ending point of a range

‘^’

represents the characters not in the list. If you want to make the ‘^’ character a list item, place it anywhere but first

TODO. incorporate this paragraph [copied verbatim from BRE section]

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. For example,
sed -i 's/hello/world/' file.txt
972 matches either ‘\’ or ‘*’, because the
sed -i 's/hello/world/' file.txt
997 is not special here. However, strings like
sed -i 's/hello/world/' file.txt
974,
sed -i 's/hello/world/' file.txt
975, and
sed -i 's/hello/world/' file.txt
976 are special within list and represent collating symbols, equivalence classes, and character classes, respectively, and
sed -i 's/hello/world/' file.txt
948 is therefore special within list when it is followed by
sed -i 's/hello/world/' file.txt
689,
sed -i 's/hello/world/' file.txt
980, or
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

‘[=’

represents the open equivalence class

‘=]’

represents the close equivalence class

‘[. ’

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

‘. ]’

represents the close character class symbol

5. 6 regular expression extensions

The following sequences have special meaning inside regular expressions [used in addresses and the

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

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

Matches any “word” character. A “word” character is any letter or digit or the underscore character

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

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

Matches any “non-word” character

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

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

Matches a word boundary; that is it matches if the character to the left is a “word” character and the character to the right is a “non-word” character, or vice-versa

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

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

Matches everywhere but on a word boundary; that is it matches if the character to the left and the character to the right are either both “word” characters or both “non-word” characters

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

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

Matches whitespace characters [spaces and tabs]. 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

Matches the end of a word

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

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

Matches only at the start of pattern space. This is different from

sed -i 's/hello/world/' file.txt
154 in multi-line mode

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. This is different from

sed -i 's/hello/world/' file.txt
87 in multi-line mode

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. Back-references are specified with backslash and a single digit [e. g. ‘\1’]. The part of the regular expression they refer to is called a subexpression, and is designated with parentheses

Back-references and subexpressions are used in two cases. in the regular expression search pattern, and in the replacement part of the

sed -i 's/hello/world/' file.txt
967 command [see Regular Expression Addresses and The "s" Command]

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 ‘. ’ - any single character [being surrounded by parentheses makes it a subexpression]. The back-reference ‘\1’ asks to match the same content [same character] as the sub-expression

The command below matches words starting with any character, followed by the letter ‘o’, followed by the same character as the first

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

Multiple subexpressions are automatically numbered from left-to-right. This command searches for 6-letter palindromes [the first three letters are 3 subexpressions, followed by 3 back-references in reverse order]

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

In the

sed -i 's/hello/world/' file.txt
967 command, back-references can be used in the replacement part to refer back to subexpressions in the regexp part

The following example uses two subexpressions in the regular expression to match two space-separated words. The back-references in the replacement part prints the words in a different order

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

When used with alternation, if the group does not participate in the match then the back-reference makes the whole match fail. For example, ‘a[. ]. b\1’ will not match ‘ba’. When multiple regular expressions are given with -e or from a file [‘-f file’], back-references are local to each expression

5. 8 Escape Sequences - specifying special characters

Until this chapter, we have only encountered escapes of the form ‘\^’, which tell

sed -i 's/hello/world/' file.txt
22 not to interpret the circumflex as a special character, but rather to take it literally. 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. This provides a way of encoding non-printable characters in patterns in a visible manner. 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

The list of these escapes is

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

Produces or matches a BEL character, that is an “alert” [ASCII 7]

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

Produces or matches a form feed [ASCII 12]

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

Produces or matches a newline [ASCII 10]

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

Produces or matches a carriage return [ASCII 13]

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

Produces or matches a horizontal tab [ASCII 9]

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

Produces or matches a so called “vertical tab” [ASCII 11]

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

Produces or matches CONTROL-x, where x is any character. The precise effect of ‘\cx’ is as follows. if x is a lower case letter, it is converted to upper case. Then bit 6 of the character [hex 40] is inverted. Thus ‘\cz’ becomes hex 1A, but ‘\c{’ becomes hex 3B, while ‘\c;’ becomes hex 7B

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

Produces or matches a character whose decimal ASCII value is xxx

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

Produces or matches a character whose octal ASCII value is 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 Escaping Precedence

GNU

sed -i 's/hello/world/' file.txt
22 processes escape sequences before passing the text onto the regular-expression matching of the
sed -i 's/hello/world/' file.txt
666 command and Address matching. Thus the follwing two commands are equivalent [‘0x5e’ is the hexadecimal ASCII value of the character ‘^’]

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

However it is recommended to avoid such special characters due to unexpected edge-cases. For example, the following are not equivalent

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

5. 9 Multibyte characters and Locale Considerations

GNU

sed -i 's/hello/world/' file.txt
22 processes valid multibyte characters in multibyte locales [e. g.
sed 's/hello/world/' input.txt > output.txt
915]. 7

The following example uses the Greek letter Capital Sigma [Σ, Unicode code point

sed 's/hello/world/' input.txt > output.txt
916]. In a
sed 's/hello/world/' input.txt > output.txt
915 locale,
sed -i 's/hello/world/' file.txt
22 correctly processes the Sigma as one character despite it being 2 octets [bytes]

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

To force

sed -i 's/hello/world/' file.txt
22 to process octets separately, use the
sed 's/hello/world/' input.txt > output.txt
920 locale [also known as the
sed 's/hello/world/' input.txt > output.txt
921 locale]

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

5. 9. 1 Invalid multibyte characters

sed -i 's/hello/world/' file.txt
22’s regular expressions do not match invalid multibyte sequences in a multibyte locale

In the following examples, the ascii value

sed 's/hello/world/' input.txt > output.txt
923 is an incomplete multibyte character [shown here as �]. The regular expression ‘. ’ does not match it

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

Similarly, the ’catch-all’ regular expression ‘. *’ does not match the entire line

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. In the following examples, the
sed 's/hello/world/' input.txt > output.txt
930 is an invalid multibyte sequence, while
sed 's/hello/world/' input.txt > output.txt
931 is a valid multibyte sequence [of the Greek Sigma character]

The following

sed -i 's/hello/world/' file.txt
22 program removes all valid characters using
sed 's/hello/world/' input.txt > output.txt
933. Any content left in the pattern space [the invalid characters] are added to the hold space using the
sed -i 's/hello/world/' file.txt
947 command. On the last line [
sed -i 's/hello/world/' file.txt
87], the hold space is retrieved [
sed -i 's/hello/world/' file.txt
975], newlines are removed [
sed 's/hello/world/' input.txt > output.txt
937], and any remaining octets are printed unambiguously [
sed -i 's/hello/world/' file.txt
67]. Thus, any invalid multibyte sequences are printed as octal values

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

With a few more commands,

sed -i 's/hello/world/' file.txt
22 can print the exact line number corresponding to each invalid characters [line 3]. These characters can then be removed by forcing the
sed 's/hello/world/' input.txt > output.txt
920 locale and using octal escape sequences

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

5. 9. 2 Upper/Lower case conversion

GNU

sed -i 's/hello/world/' file.txt
22’s substitute command [
sed -i 's/hello/world/' file.txt
967] supports upper/lower case conversions using
sed -i 's/hello/world/' file.txt
105,
sed -i 's/hello/world/' file.txt
104 codes. These conversions support multibyte characters

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

Xem Lệnh "s"

5. 9. 3 Multibyte regexp character classes

In other locales, the sorting sequence is not specified, and ‘[a-d]’ might be equivalent to ‘[abcd]’ or to ‘[aBbCcDd]’, or it might fail to match any character, or the set of characters that it matches might even be erratic. To obtain the traditional interpretation of bracket expressions, you can use the ‘C’ locale by setting the

sed 's/hello/world/' input.txt > output.txt
945 environment variable to the value ‘C’

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

Their interpretation depends on the

sed 's/hello/world/' input.txt > output.txt
946 locale; for example, ‘[[. alnum. ]]’ means the character class of numbers and letters in the current locale

TODO. show example of collation

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

6 Advanced
sed -i 's/hello/world/' file.txt
22. cycles and buffers

6. 1 How
sed -i 's/hello/world/' file.txt
22 Works

sed -i 's/hello/world/' file.txt
22 duy trì hai bộ đệm dữ liệu. the active pattern space, and the auxiliary hold space. 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. first,
sed -i 's/hello/world/' file.txt
22 reads one line from the input stream, removes any trailing newline, and places it in the pattern space. 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

nối thêm dòng từ tệp đầu vào vào không gian mẫu

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

The branching commands

sed -i 's/hello/world/' file.txt
982,
sed -i 's/hello/world/' file.txt
983, and
sed -i 's/hello/world/' file.txt
984 enable changing the flow of
sed -i 's/hello/world/' file.txt
22 programs

By default,

sed -i 's/hello/world/' file.txt
22 reads an input line into the pattern buffer, then continues to processes all commands in order. Commands without addresses affect all lines. Commands with addresses affect only matching lines. See Execution Cycle and Addresses overview

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

delete [clears] the current pattern space, and restart the program cycle without processing the rest of the commands and without printing the pattern space

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

delete the contents of the pattern space up to the first newline, and restart the program cycle without processing the rest of the commands and without printing the pattern space

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. If [addr] matches the current pattern space, execute the command[s]. For example. The command
sed 's/hello/world/' input.txt > output.txt
993 means. if the current pattern matches the regular expression
sed 's/hello/world/' input.txt > output.txt
994 [a line starting with a hash], then execute the
sed -i 's/hello/world/' file.txt
36 command. delete the line without printing it, and restart the program cycle immediately

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

branch unconditionally [that is. always jump to a label, skipping or repeating other commands, without restarting a new cycle]. Combined with an address, the branch can be conditionally executed on matched lines

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

similar but opposite to the

sed -i 's/hello/world/' file.txt
983 command. branch only if there has been no successful substitutions since the last input line was read

The following two

sed -i 's/hello/world/' file.txt
22 programs are equivalent. 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’. The second example uses an address with negation [‘. ’] to perform substitution only on desired lines. 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 Branching and Cycles

The

sed -i 's/hello/world/' file.txt
982,
sed -i 's/hello/world/' file.txt
983 and
sed -i 's/hello/world/' file.txt
984 commands can be followed by a label [typically a single letter]. Labels are defined with a colon followed by one or more letters [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

The following example is an infinite-loop - it doesn’t terminate and doesn’t print anything. 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]. Các dòng được tích lũy trong không gian mẫu cho đến khi không còn dòng đầu vào nào để đọc, sau đó lệnh
    sed -i 's/hello/world/' file.txt
    
    71 kết thúc chương trình
    sed -i 's/hello/world/' file.txt
    
    22. 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

Chương trình sau sử dụng đối sánh địa chỉ '/=$/' làm điều kiện. 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. Nếu thay thế không thành công [có nghĩa là không có ngắt dòng mềm], lệnh
sed -i 's/hello/world/' file.txt
983 sẽ không phân nhánh. Sau đó,
sed -i 's/hello/world/' file.txt
957 sẽ in nội dung không gian mẫu cho đến dòng mới đầu tiên và
sed -i 's/hello/world/' file.txt
939 sẽ xóa nội dung không gian mẫu cho đến dòng mới đầu tiên. [Để tìm hiểu thêm về các lệnh
sed -i 's/hello/world/' file.txt
71,
sed -i 's/hello/world/' file.txt
957 và
sed -i 's/hello/world/' file.txt
939, hãy xem Kỹ thuật đa dòng]

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

Để biết thêm các ví dụ về nối dòng, hãy xem Nối các dòng

7 Một số kịch bản mẫu

Dưới đây là một số kịch bản

sed -i 's/hello/world/' file.txt
22 để hướng dẫn bạn nghệ thuật làm chủ
sed -i 's/hello/world/' file.txt
22

7. 1 Đường nối

Phần này sử dụng các lệnh

sed -i 's/hello/world/' file.txt
71,
sed -i 's/hello/world/' file.txt
939 và
sed -i 's/hello/world/' file.txt
957 để xử lý nhiều dòng và các lệnh
sed -i 's/hello/world/' file.txt
982 và
sed -i 's/hello/world/' file.txt
983 để phân nhánh. See Multiline techniques and Branching and flow control

Tham gia các dòng cụ thể [e. g. nếu dòng 2 và 3 cần được nối]

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

Tham gia các dòng tiếp tục dấu gạch chéo ngược

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

Join lines that start with whitespace [e. g tiêu đề SMTP]

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

7. 2 đường định tâm

Tập lệnh này căn giữa tất cả các dòng của tệp trên chiều rộng 80 cột. Để thay đổi chiều rộng đó, số trong

sed -i 's/hello/world/' file.txt
0439 phải được thay thế và số khoảng trắng đã thêm cũng phải được thay đổi

Lưu ý cách các lệnh đệm được sử dụng để phân tách các phần trong biểu thức chính quy được khớp—đây là một kỹ thuật phổ biến

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. Điều này thực sự có thể,9 nhưng phải được thực hiện thủ công

Để tăng một số, bạn chỉ cần thêm 1 vào chữ số cuối cùng, thay thế nó bằng chữ số tiếp theo. Có một ngoại lệ. khi chữ số là chín, các chữ số trước đó cũng phải được tăng dần cho đến khi bạn không có số chín

Giải pháp này của Bruno Haible rất thông minh và thông minh vì nó sử dụng một bộ đệm duy nhất; . Nó hoạt động bằng cách thay thế các số 9 ở cuối bằng dấu gạch dưới, sau đó sử dụng nhiều lệnh

sed -i 's/hello/world/' file.txt
967 để tăng chữ số cuối cùng, sau đó lại thay thế dấu gạch dưới bằng số không

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 Đổi tên tệp thành chữ thường

Đây là một cách sử dụng khá lạ của

sed -i 's/hello/world/' file.txt
22. Chúng tôi chuyển đổi văn bản và chuyển đổi nó thành các lệnh trình bao, sau đó chỉ cần đưa chúng vào trình bao. Đừng lo lắng, những vụ hack thậm chí còn tồi tệ hơn được thực hiện khi sử dụng
sed -i 's/hello/world/' file.txt
22;

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. Lưu ý cách tập lệnh được tham số hóa bằng cách sử dụng các biến trình bao và trích dẫn thích hợp

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___1

7. 5 Bản in
sed -i 's/hello/world/' file.txt
0447 Môi trường

Tập lệnh này loại bỏ định nghĩa của các hàm trình bao khỏi đầu ra của lệnh

sed -i 's/hello/world/' file.txt
0448 Bourne-shell

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

Tập lệnh này có thể được sử dụng để đảo ngược vị trí của các ký tự trong dòng. The technique moves two characters at a time, hence it is faster than more intuitive implementations

Lưu ý lệnh

sed -i 's/hello/world/' file.txt
0449 trước định nghĩa của nhãn. Đ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

Độc giả giàu trí tưởng tượng sẽ tìm thấy cách sử dụng cho kịch bản này. 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 Tìm kiếm văn bản trên nhiều dòng

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

Dễ dàng tìm các từ kép trong một dòng bằng GNU

sed -i 's/hello/world/' file.txt
687 và tương tự với 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+’]. Xem tiện ích mở rộng regrec
  • Thêm dấu ngoặc đơn quanh biểu thức ‘[\w+]’ sẽ tạo ra một biểu thức con. 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. Xem Back-references và 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’ is not a desired match]
  • Tùy chọn -E cho phép cú pháp biểu thức chính quy mở rộng, giảm bớt nhu cầu thêm dấu gạch chéo ngược trước dấu ngoặc đơn. See ERE syntax

Khi từ được nhân đôi kéo dài hai dòng, biểu thức chính quy ở trên sẽ không tìm thấy chúng vì

sed -i 's/hello/world/' file.txt
687 và
sed -i 's/hello/world/' file.txt
22 hoạt động theo từng dòng

By using

sed -i 's/hello/world/' file.txt
71 and
sed -i 's/hello/world/' file.txt
939 commands,
sed -i 's/hello/world/' file.txt
22 can apply regular expressions on multiple lines [that is, multiple lines are stored in the pattern space, and the regular expression works on it]

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ỳ]
  • Biểu thức chính quy sử dụng '\s+' cho dấu tách từ khớp với cả khoảng trắng và dòng mới
  • Biểu thức chính quy khớp, toàn bộ không gian mẫu được in bằng
    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
  • sed -i 's/hello/world/' file.txt
    
    939 xóa dòng đầu tiên khỏi không gian mẫu [cho đến dòng mới đầu tiên], sẵn sàng cho chu kỳ tiếp theo

Xem hướng dẫn sử dụng GNU

sed -i 's/hello/world/' file.txt
0464 để biết giải pháp thay thế bằng cách sử dụng
sed -i 's/hello/world/' file.txt
0465 và
sed -i 's/hello/world/' file.txt
0466 tại https. //gnu. org/s/coreutils/manual/html_node/Ép-và-xóa. html

7. 8 Điều chỉnh độ dài dòng

Phần này sử dụng các 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 kéo dài trên nhiều dòng và lệnh
sed -i 's/hello/world/' file.txt
982 để phân nhánh. Xem Kỹ thuật đa tuyến và Kiểm soát luồng và phân nhánh

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

Chương trình sed sau kết thúc các dòng ở 40 ký tự

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

đầu ra được bọc

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

7. 9 dòng tệp ngược

Cái này bắt đầu một loạt các kịch bản hoàn toàn vô dụng [nhưng thú vị] mô phỏng các lệnh Unix khác nhau. Điều này, đặc biệt, là một công việc tương tự

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

Lưu ý rằng trên các triển khai khác ngoài GNU

sed -i 's/hello/world/' file.txt
22, tập lệnh này có thể dễ dàng làm tràn bộ đệm bên trong

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 dòng đánh số

Tập lệnh này thay thế 'cat -n';

Tất nhiên điều này là hoàn toàn vô ích và vì hai lý do. thứ nhất, bởi vì ai đó khác đã làm nó trong C, thứ hai, bởi vì tập lệnh Bourne-shell sau đây có thể được sử dụng cho cùng một mục đích và sẽ nhanh hơn nhiều

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

Nó sử dụng

sed -i 's/hello/world/' file.txt
22 để in số dòng, sau đó nhóm hai dòng thành hai bằng cách sử dụng
sed -i 's/hello/world/' file.txt
71. Tất nhiên, bản kinh này không dạy nhiều như bản trình bày dưới đây

Thuật toán được sử dụng để tăng dần sử dụng cả hai bộ đệm, vì vậy dòng được in càng sớm càng tốt và sau đó bị loại bỏ. Số được chia sao cho các chữ số thay đổi đi vào bộ đệm và những chữ số không thay đổi đi vào bộ đệm khác; . Số dòng cho dòng tiếp theo sau đó được soạn và lưu trữ trong không gian giữ, sẽ được sử dụng trong lần lặp tiếp theo

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 Đánh số dòng không trống

Mô phỏng 'cat -b' gần giống như 'cat -n'—chúng ta chỉ phải chọn dòng nào sẽ được đánh số và dòng nào không

Phần chung của tập lệnh này và phần trước đó không được bình luận để cho thấy tầm quan trọng của việc bình luận đúng tập lệnh

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

________ 1062 ________ 1072 ________ 1073

7. 12 số đếm

Kịch bản này cho thấy một cách khác để làm số học với

sed -i 's/hello/world/' file.txt
22. Trong trường hợp này, chúng tôi phải thêm các số lượng lớn có thể, do đó, việc thực hiện điều này theo các bước tăng liên tiếp sẽ không khả thi [và thậm chí có thể phức tạp hơn so với tập lệnh này]

Cách tiếp cận là ánh xạ số thành chữ cái, một loại bàn tính được thực hiện với

sed -i 's/hello/world/' file.txt
22. 'a's là đơn vị, 'b's là hàng chục, v.v. chúng ta chỉ cần thêm số lượng ký tự trên dòng hiện tại dưới dạng đơn vị, sau đó chuyển số ký tự thành hàng chục, hàng trăm, v.v.

As usual, running totals are kept in hold space

On the last line, we convert the abacus form back to decimal. Để đa dạng, điều này được thực hiện bằng một vòng lặp chứ không phải bằng một số 80 lệnh

sed -i 's/hello/world/' file.txt
96711. đầu tiên, chúng tôi chuyển đổi các đơn vị, loại bỏ 'a' khỏi số;

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 Đếm Từ

Tập lệnh này gần giống như tập lệnh trước, khi mỗi từ trên dòng được chuyển thành một chữ 'a' [trong tập lệnh trước, mỗi chữ cái được đổi thành 'a']

Điều thú vị là các chương trình

sed -i 's/hello/world/' file.txt
0480 thực có các vòng lặp được tối ưu hóa cho 'wc -c', vì vậy chúng đếm từ chậm hơn nhiều so với đếm ký tự. Thay vào đó, nút cổ chai của tập lệnh này là số học và do đó, phần đếm từ nhanh hơn [nó phải quản lý các số nhỏ hơn]

Một lần nữa, các phần chung không được bình luận để cho thấy tầm quan trọng của việc bình luận các kịch bản

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

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 dòng đếm

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 In những dòng đầu tiên

Tập lệnh này có lẽ là tập lệnh

sed -i 's/hello/world/' file.txt
22 hữu ích đơn giản nhất. Nó hiển thị 10 dòng đầu vào;

7. 16 In những dòng cuối cùng

In n dòng cuối thay vì in dòng đầu tiên phức tạp hơn nhưng thực sự có thể. n được mã hóa ở dòng thứ hai, trước ký tự bang

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

Về cơ bản, các tập lệnh giữ một cửa sổ gồm 10 dòng và trượt nó bằng cách thêm một dòng và xóa dòng cũ nhất [lệnh thay thế trên dòng thứ hai hoạt động giống như lệnh

sed -i 's/hello/world/' file.txt
939 nhưng không khởi động lại vòng lặp]

Kỹ thuật “cửa sổ trượt” là một cách rất hiệu quả để viết các tập lệnh

sed -i 's/hello/world/' file.txt
22 hiệu quả và phức tạp, bởi vì các lệnh như
sed -i 's/hello/world/' file.txt
957 sẽ yêu cầu nhiều công việc nếu được triển khai thủ công

Để giới thiệu kỹ thuật, được trình bày đầy đủ trong phần còn lại của chương này và dựa trên các lệnh

sed -i 's/hello/world/' file.txt
71,
sed -i 's/hello/world/' file.txt
957 và
sed -i 's/hello/world/' file.txt
939, đây là cách triển khai
sed -i 's/hello/world/' file.txt
0492 bằng cách sử dụng một “cửa sổ trượt” đơn giản. ”

Điều này có vẻ phức tạp nhưng trên thực tế hoạt động giống như tập lệnh cuối cùng. Tuy nhiên, sau khi chúng tôi đã khởi động số lượng dòng thích hợp, chúng tôi ngừng sử dụng không gian giữ để giữ trạng thái giữa các dòng và thay vào đó sử dụng

sed -i 's/hello/world/' file.txt
71 và
sed -i 's/hello/world/' file.txt
939 để trượt không gian mẫu theo một dòng

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

Lưu ý cách dòng đầu tiên, thứ hai và thứ tư không hoạt động sau mười dòng đầu tiên của đầu vào. Sau đó, tất cả những gì kịch bản làm là. exiting on the last line of input, appending the next input line to pattern space, and removing the first line

7. 17. Làm cho các dòng trùng lặp trở nên độc đáo

Đây là một ví dụ về nghệ thuật sử dụng các lệnh

sed -i 's/hello/world/' file.txt
71,
sed -i 's/hello/world/' file.txt
957 và
sed -i 's/hello/world/' file.txt
939, có lẽ là lệnh khó thành thạo nhất

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

Như bạn có thể thấy, chúng tôi duy trì cửa sổ 2 dòng bằng cách sử dụng

sed -i 's/hello/world/' file.txt
957 và
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 In các dòng đầu vào trùng lặp

Tập lệnh này chỉ in các dòng trùng lặp, như '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 Xóa tất cả các dòng trùng lặp

Tập lệnh này chỉ in các dòng duy nhất, như '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

Như một ví dụ cuối cùng, đây là ba tập lệnh, với độ phức tạp và tốc độ tăng dần, thực hiện chức năng tương tự như 'cat -s', đó là ép các dòng trống

Đầu tiên để lại một dòng trống ở đầu và cuối nếu đã có một số

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

Cái này phức tạp hơn một chút và loại bỏ tất cả các dòng trống ở đầu. Nó để lại một dòng trống ở cuối nếu có một dòng ở đó

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

Điều này loại bỏ các dòng trống đầu và cuối. Nó cũng là nhanh nhất. Lưu ý rằng các vòng lặp hoàn toàn được thực hiện với

sed -i 's/hello/world/' file.txt
952 và
sed -i 's/hello/world/' file.txt
982 mà không cần dựa vào
sed -i 's/hello/world/' file.txt
22 để tự động khởi động lại tập lệnh ở cuối dòng

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 Hạn chế và Không hạn chế của GNU
sed -i 's/hello/world/' file.txt
22

Đố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. Tiêu chuẩn POSIX chỉ định rằng việc triển khai
sed -i 's/hello/world/' file.txt
22 phù hợp sẽ hỗ trợ độ dài dòng ít nhất là 8192 byte. GNU
sed -i 's/hello/world/' file.txt
22 không có giới hạn tích hợp về độ dài dòng;

Tuy nhiên, đệ quy được sử dụng để xử lý các mẫu con và lặp lại vô thời hạn. Điều này có nghĩa là không gian ngăn xếp có sẵn có thể giới hạn kích thước của bộ đệm có thể được xử lý theo một số mẫu nhất định

9 Các nguồn khác để tìm hiểu về
sed -i 's/hello/world/' file.txt
22

Để biết thông tin cập nhật về GNU

sed -i 's/hello/world/' file.txt
22, vui lòng truy cập https. //www. gnu. tổ chức/phần mềm/sed/

Gửi các câu hỏi và đề xuất chung tới sed-devel@gnu. tổ chức. Truy cập kho lưu trữ danh sách gửi thư cho các cuộc thảo luận trước đây tại https. // danh sách. gnu. org/archive/html/sed-devel/

Các tài nguyên sau đây cung cấp thông tin về

sed -i 's/hello/world/' file.txt
22 [cả GNU
sed -i 's/hello/world/' file.txt
22 và các biến thể khác]. Note these not maintained by GNU
sed -i 's/hello/world/' file.txt
22 developers

10 lỗi báo cáo

Báo cáo lỗi qua email tới bug-sed@gnu. tổ chức. Ngoài ra, vui lòng bao gồm đầu ra của 'sed --version' trong phần thân báo cáo của bạn nếu có thể

Vui lòng không gửi báo cáo lỗi như thế này

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. Không giống như các chương trình khác như trình biên dịch C, việc tạo các trường hợp thử nghiệm như vậy cho
sed -i 's/hello/world/' file.txt
22 khá đơn giản

Một trường hợp thử nghiệm độc lập bao gồm tất cả dữ liệu cần thiết để thực hiện thử nghiệm và lệnh gọi cụ thể của

sed -i 's/hello/world/' file.txt
22 gây ra sự cố. Trường hợp kiểm thử độc lập càng nhỏ thì càng tốt. Một trường hợp thử nghiệm không nên liên quan đến thứ gì đó khác xa với
sed -i 's/hello/world/' file.txt
22 như “cố định cấu hình frobme-1. 3. 4". Vâng, về nguyên tắc đó là đủ thông tin để tìm lỗi, nhưng đó không phải là một triển vọng thực tế

Dưới đây là một vài lỗi thường được báo cáo không phải là lỗi

Lệnh
sed -i 's/hello/world/' file.txt
71 ở dòng cuối cùng

Hầu hết các phiên bản của

sed -i 's/hello/world/' file.txt
22 thoát mà không in bất kỳ thứ gì khi lệnh
sed -i 's/hello/world/' file.txt
71 được đưa ra ở dòng cuối cùng của tệp. 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. Sự lựa chọn này là do thiết kế

Hành vi mặc định [tiện ích mở rộng gnu, không tuân thủ POSIX]

Để buộc hành vi tuân thủ POSIX

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

Thay vì chỉ

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 sử dụng cú pháp biểu thức chính quy cơ bản POSIX. Theo tiêu chuẩn, ý nghĩa của một số chuỗi thoát không được xác định trong cú pháp này;

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 khớp với 'abc' hoặc 'def'

Cú pháp này có thể gây ra sự cố khi chạy tập lệnh được viết cho các

sed -i 's/hello/world/' file.txt
22 khác. 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. Các tập lệnh như vậy phải được sửa đổi bằng cách xóa các dấu gạch chéo ngược giả nếu chúng được sử dụng với các triển khai hiện đại của
sed -i 's/hello/world/' file.txt
22, như GNU
sed -i 's/hello/world/' file.txt
22

Mặt khác, một số tập lệnh sử dụng s. abc\. chắc chắn. g để loại bỏ các lần xuất hiện của

sed -i 's/hello/world/' file.txt
0050 hoặc
sed -i 's/hello/world/' file.txt
0051. Trong khi điều này làm việc cho đến khi
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. Đây lại là hành vi không xác định theo POSIX và cách giải thích này được cho là mạnh mẽ hơn. 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

Ngoài ra, phiên bản

sed -i 's/hello/world/' file.txt
22 này hỗ trợ một số ký tự thoát [một số ký tự có nhiều ký tự] để chèn các ký tự không in được vào tập lệnh [
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]. Những thứ này có thể gây ra sự cố tương tự với các tập lệnh được viết cho các
sed -i 's/hello/world/' file.txt
22 khác

-i chặn các tệp chỉ đọc

Nói tóm lại, 'sed -i' sẽ cho phép bạn xóa nội dung của tệp chỉ đọc và nói chung, tùy chọn -i [xem Yêu cầu] cho phép bạn ghi đè các tệp được bảo vệ. Đây không phải là lỗi, mà là hậu quả của cách thức hoạt động của hệ thống tệp Unix

Các quyền trên một tệp cho biết điều gì có thể xảy ra với dữ liệu trong tệp đó, trong khi các quyền trên một thư mục cho biết điều gì có thể xảy ra với danh sách các tệp trong thư mục đó. 'sed -i' sẽ không bao giờ mở để ghi tệp đã có trên đĩa. Thay vào đó, nó sẽ hoạt động trên một tệp tạm thời cuối cùng được đổi tên thành tên gốc. nếu bạn đổi tên hoặc xóa tệp, thực ra bạn đang sửa đổi nội dung của thư mục, vì vậy hoạt động phụ thuộc vào quyền của thư mục chứ không phải của tệp. Cũng vì lý do này,

sed -i 's/hello/world/' file.txt
22 không cho phép bạn sử dụng -i trên tệp có thể ghi trong thư mục chỉ đọc và sẽ phá vỡ liên kết cứng hoặc liên kết tượng trưng khi -i được sử dụng trên tệp đó

sed -i 's/hello/world/' file.txt
0068 không hoạt động [có lỗi]

Không có dòng 0. 0 là một địa chỉ đặc biệt chỉ được sử dụng để coi các địa chỉ như

sed -i 's/hello/world/' file.txt
0069 là hoạt động khi tập lệnh bắt đầu. nếu bạn viết
sed -i 's/hello/world/' file.txt
0070 và dòng đầu tiên bao gồm từ 'abc', thì kết quả khớp đó sẽ bị bỏ qua vì phạm vi địa chỉ phải kéo dài ít nhất hai dòng [trừ phần cuối của tệp];

sed -i 's/hello/world/' file.txt
0072 không phân biệt chữ hoa chữ thường

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. Một số ngôn ngữ có thứ tự đối chiếu không phân biệt chữ hoa chữ thường, những ngôn ngữ khác thì không

Một vấn đề khác là

sed -i 's/hello/world/' file.txt
0072 cố gắng sử dụng các ký hiệu đối chiếu. Điều này chỉ xảy ra nếu bạn đang sử dụng hệ thống GNU, sử dụng trình so khớp biểu thức chính quy của GNU libc thay vì biên dịch cái được cung cấp cùng với GNU sed. Ví dụ: trong một ngôn ngữ Đan Mạch, biểu thức chính quy
sed -i 's/hello/world/' file.txt
0077 khớp với chuỗi 'aa', bởi vì đây là một ký hiệu đối chiếu duy nhất xuất hiện sau 'a' và trước 'b';

Để khắc phục những sự cố này, vốn có thể gây ra lỗi trong tập lệnh shell, hãy đặt biến môi trường

sed -i 's/hello/world/' file.txt
0078 và
sed 's/hello/world/' input.txt > output.txt
946 thành 'C'

sed 's/hello/world/' input.txt > output.txt
926 không xóa không gian mẫu

Điều này xảy ra nếu luồng đầu vào của bạn bao gồm các chuỗi nhiều byte không hợp lệ. POSIX yêu cầu các chuỗi như vậy không khớp với '. ’, do đó ‘s/. *//' sẽ không xóa không gian mẫu như bạn mong đợi. 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]. Vì lý do này, GNU

sed -i 's/hello/world/' file.txt
22 cung cấp lệnh 'z' [cho 'zap'] dưới dạng tiện ích mở rộng

Để khắc phục những sự cố này, vốn có thể gây ra lỗi trong tập lệnh shell, hãy đặt biến môi trường

sed -i 's/hello/world/' file.txt
0078 và
sed 's/hello/world/' input.txt > output.txt
946 thành 'C'

Phụ lục A Giấy phép Tài liệu Miễn phí GNU

Phiên bản 1. 3, 3 tháng 11 năm 2008

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

  1. LỜI MỞ ĐẦU

    Mục đích của Giấy phép này là làm cho sách hướng dẫn, sách giáo khoa hoặc tài liệu chức năng và hữu ích khác trở nên miễn phí theo nghĩa tự do. 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. Nó bổ sung cho Giấy phép Công cộng GNU, là giấy phép copyleft được thiết kế cho phần mềm tự do

    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í. một chương trình miễn phí phải đi kèm với sách hướng dẫn cung cấp các quyền tự do giống như phần mềm. Nhưng Giấy phép này không giới hạn ở hướng dẫn sử dụng phần mềm; . Chúng tôi khuyên dùng Giấy phép này chủ yếu cho các tác phẩm có mục đích hướng dẫn hoặc tham khảo

  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. Thông báo như vậy cấp giấy phép toàn cầu, miễn phí bản quyền, không giới hạn thời hạn, để sử dụng tác phẩm đó theo các điều kiện được nêu ở đây. “Tài liệu” bên dưới đề cập đến bất kỳ sổ tay hoặc công việc nào như vậy. Bất kỳ thành viên nào của công chúng đều là người được cấp phép và được gọi là “bạn”. You accept the license if you copy, modify or distribute the work in a way requiring permission under copyright law

    “Phiên bản đã sửa đổi” của Tài liệu có nghĩa là bất kỳ tác phẩm nào chứa Tài liệu hoặc một phần của nó, được sao chép nguyên văn hoặc có sửa đổi và/hoặc được dịch sang ngôn ngữ khác

    “Phần phụ” là một phụ lục được đặt tên hoặc một phần quan trọng nhất của Tài liệu chỉ liên quan đến mối quan hệ của nhà xuất bản hoặc tác giả của Tài liệu với chủ đề chung của Tài liệu [hoặc các vấn đề liên quan] và không chứa nội dung nào có thể ảnh hưởng trực tiếp đến . [Do đó, nếu Tài liệu là một phần của sách giáo khoa toán học, thì Phần phụ không được giải thích bất kỳ kiến ​​thức toán học nào. ] 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

    “Phần bất biến” là một số Phần phụ nhất định có tiêu đề được chỉ định, giống như tiêu đề của Phần bất biến, trong thông báo nói rằng Tài liệu được phát hành theo Giấy phép này. If a section does not fit the above definition of Secondary then it is not allowed to be designated as Invariant. Tài liệu có thể chứa 0 Phần bất biến. Nếu Tài liệu không xác định bất kỳ Phần bất biến nào thì không có phần nào

    “Văn bản bìa” là những đoạn văn bản ngắn nhất định được liệt kê, dưới dạng Văn bản bìa trước hoặc Văn bản bìa sau, trong thông báo nói rằng Tài liệu được phát hành theo Giấy phép này. Văn bản Bìa trước có thể có nhiều nhất là 5 từ và Văn bản Bìa sau có thể có nhiều nhất là 25 từ

    Bản sao "Trong suốt" của Tài liệu có nghĩa là bản sao có thể đọc được bằng máy, được thể hiện ở định dạng có thông số kỹ thuật sẵn có cho công chúng, phù hợp để sửa đổi tài liệu một cách trực tiếp bằng trình soạn thảo văn bản chung hoặc [đối với hình ảnh bao gồm pixel] sơn chung . Một bản sao được tạo ở định dạng tệp Minh bạch khác, có đánh dấu hoặc không có đánh dấu, đã được sắp xếp để ngăn cản hoặc không khuyến khích người đọc sửa đổi sau đó không phải là Minh bạch. Định dạng hình ảnh không trong suốt nếu được sử dụng cho bất kỳ lượng văn bản đáng kể nào. Bản sao không “Trong suốt” được gọi là “Đục”

    Ví dụ về các định dạng phù hợp cho các bản sao Trong suốt bao gồm ASCII đơn giản không có đánh dấu, định dạng đầu vào Texinfo, định dạng đầu vào LaTeX, SGML hoặc XML bằng cách sử dụng DTD có sẵn công khai và HTML, PostScript hoặc PDF đơn giản phù hợp với tiêu chuẩn được thiết kế để con người sửa đổi. Ví dụ về các định dạng hình ảnh trong suốt bao gồm PNG, XCF và JPG. Các định dạng mờ bao gồm các định dạng độc quyền chỉ có thể được đọc và chỉnh sửa bởi các trình xử lý văn bản độc quyền, SGML hoặc XML mà DTD và/hoặc các công cụ xử lý thường không có sẵn và HTML, PostScript hoặc PDF do máy tạo ra bởi một số trình xử lý văn bản cho

    “Trang tiêu đề” có nghĩa là, đối với một cuốn sách in, chính trang tiêu đề đó, cộng với các trang tiếp theo cần thiết để giữ một cách rõ ràng, tài liệu mà Giấy phép này yêu cầu xuất hiện trong trang tiêu đề. Đối với các tác phẩm ở định dạng không có bất kỳ trang tiêu đề nào như vậy, “Trang tiêu đề” có nghĩa là văn bản gần phần nổi bật nhất của tiêu đề tác phẩm, trước phần đầu của phần nội dung văn bản

    “Nhà xuất bản” có nghĩa là bất kỳ cá nhân hoặc tổ chức nào phân phối các bản sao của Tài liệu cho công chúng

    Phần “Có tiêu đề XYZ” có nghĩa là đơn vị con được đặt tên của Tài liệu có tiêu đề chính xác là XYZ hoặc chứa XYZ trong ngoặc đơn theo sau văn bản dịch XYZ sang ngôn ngữ khác. [Ở đây XYZ là viết tắt của tên phần cụ thể được đề cập bên dưới, chẳng hạn như “Lời cảm ơn”, “Sự cống hiến”, “Sự xác nhận” hoặc “Lịch sử”. ] Để “Giữ Tiêu đề” của phần như vậy khi bạn sửa đổi Tài liệu có nghĩa là nó vẫn là phần “Có tiêu đề XYZ” theo định nghĩa này

    The Document may include Warranty Disclaimers next to the notice which states that this License applies to the Document. Những tuyên bố miễn trừ trách nhiệm bảo hành này được coi là được đưa vào bằng cách tham chiếu trong Giấy phép này, nhưng chỉ liên quan đến các tuyên bố từ chối trách nhiệm. bất kỳ ngụ ý nào khác mà các Tuyên bố từ chối trách nhiệm bảo hành này có thể có đều vô hiệu và không ảnh hưởng đến ý nghĩa của Giấy phép này

  3. SAO CHÉP NGUYÊN BẢN

    Bạn có thể sao chép và phân phối Tài liệu bằng bất kỳ phương tiện nào, thương mại hoặc phi thương mại, với điều kiện là Giấy phép này, thông báo bản quyền và thông báo cấp phép cho biết Giấy phép này áp dụng cho Tài liệu được sao chép thành tất cả các bản sao và bạn không thêm bất kỳ điều kiện nào khác . Bạn không được sử dụng các biện pháp kỹ thuật để cản trở hoặc kiểm soát việc đọc hoặc sao chép thêm các bản sao bạn tạo hoặc phân phối. Tuy nhiên, bạn có thể chấp nhận bồi thường để trao đổi với các bản sao. Nếu phát hành số lượng đủ lớn cũng phải tuân theo các điều kiện ở mục 3

    Bạn cũng có thể cho mượn các bản sao, với cùng các điều kiện nêu trên, và bạn có thể trưng bày công khai các bản sao

  4. SAO CHÉP SỐ LƯỢNG

    Nếu bạn xuất bản các bản in [hoặc các bản sao trên phương tiện thường có bìa in] của Tài liệu, đánh số hơn 100 và thông báo cấp phép của Tài liệu yêu cầu Văn bản bìa, thì bạn phải gửi kèm các bản sao trong bìa có tất cả những nội dung này một cách rõ ràng và dễ đọc. . Văn bản bìa trước ở bìa trước và Văn bản bìa sau ở bìa sau. Cả hai trang bìa cũng phải xác định rõ ràng và hợp pháp bạn là nhà xuất bản của những bản sao này. 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. Sao chép với các thay đổi giới hạn ở bìa, miễn là chúng giữ nguyên tiêu đề của Tài liệu và đáp ứng các điều kiện này, có thể được coi là sao chép nguyên văn ở các khía cạnh khác

    Nếu các văn bản cần thiết cho một trong hai trang bìa quá dày để có thể đọc được rõ ràng, bạn nên đặt những văn bản đầu tiên được liệt kê [nhiều nhất có thể] trên trang bìa thực tế và tiếp tục phần còn lại trên các trang liền kề

    Nếu bạn xuất bản hoặc phân phối các bản sao Không minh bạch của Tài liệu được đánh số hơn 100, bạn phải bao gồm một bản sao Trong suốt có thể đọc được bằng máy cùng với mỗi bản sao Không rõ ràng hoặc nêu rõ trong hoặc cùng với mỗi bản sao Không rõ ràng một vị trí mạng máy tính mà từ đó mạng chung- . Nếu bạn sử dụng tùy chọn thứ hai, bạn phải thực hiện các bước thận trọng hợp lý, khi bạn bắt đầu phân phối các bản sao Không trong suốt với số lượng lớn, để đảm bảo rằng bản sao Trong suốt này sẽ vẫn có thể truy cập được như vậy tại địa điểm đã nêu cho đến ít nhất một năm sau lần cuối cùng bạn phân phối một

    Bạn được yêu cầu, nhưng không bắt buộc, liên hệ kỹ với các tác giả của Tài liệu trước khi phân phối lại bất kỳ số lượng lớn bản sao nào, để họ có cơ hội cung cấp cho bạn phiên bản cập nhật của Tài liệu

  5. SỬA ĐỔI

    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 . Ngoài ra, bạn phải làm những điều này trong Phiên bản sửa đổi

    1. Sử dụng trong Trang Tiêu đề [và trên trang bìa, nếu có] một tiêu đề khác biệt với tiêu đề của Tài liệu và với các phiên bản trước đó [nếu có, tiêu đề này phải được liệt kê trong phần Lịch sử của Tài liệu]. Bạn có thể sử dụng tên giống như phiên bản trước nếu nhà xuất bản ban đầu của phiên bản đó cho phép
    2. Liệt kê trên Trang tiêu đề, với tư cách là tác giả, một hoặc nhiều người hoặc tổ chức chịu trách nhiệm về quyền tác giả đối với các sửa đổi trong Phiên bản đã sửa đổi, cùng với ít nhất năm tác giả chính của Tài liệu [tất cả các tác giả chính của nó, nếu nó có ít hơn
    3. Nêu trên trang Tiêu đề tên của nhà xuất bản Phiên bản đã sửa đổi, với tư cách là nhà xuất bản
    4. Giữ nguyên tất cả các thông báo bản quyền của Tài liệu
    5. Thêm một thông báo bản quyền thích hợp cho các sửa đổi của bạn bên cạnh các thông báo bản quyền khác
    6. Bao gồm, ngay sau thông báo bản quyền, thông báo giấy phép cho phép công chúng sử dụng Phiên bản đã sửa đổi theo các điều khoản của Giấy phép này, theo mẫu được hiển thị trong Phụ lục bên dưới
    7. Lưu giữ trong thông báo giấy phép đó danh sách đầy đủ các Phần bất biến và Văn bản bìa bắt buộc được cung cấp trong thông báo giấy phép của Tài liệu
    8. Bao gồm một bản sao không thay đổi của Giấy phép này
    9. Giữ nguyên phần Có tiêu đề “Lịch sử”, giữ nguyên Tiêu đề của nó và thêm vào đó một mục nêu ít nhất tiêu đề, năm, tác giả mới và nhà xuất bản của Phiên bản đã sửa đổi như được đưa ra trên Trang tiêu đề. Nếu không có phần Có tiêu đề “Lịch sử” trong Tài liệu, hãy tạo một mục nêu rõ tiêu đề, năm, tác giả và nhà xuất bản của Tài liệu như được cung cấp trên Trang tiêu đề, sau đó thêm một mục mô tả Phiên bản đã sửa đổi như đã nêu trong câu trước
    10. Giữ nguyên vị trí mạng, nếu có, được cung cấp trong Tài liệu để truy cập công khai vào bản sao Minh bạch của Tài liệu và tương tự như vậy, các vị trí mạng được cung cấp trong Tài liệu cho các phiên bản trước mà nó dựa trên. Chúng có thể được đặt trong phần "Lịch sử". Bạn có thể bỏ qua vị trí mạng đối với một tác phẩm đã được xuất bản trước Tài liệu ít nhất bốn năm hoặc nếu nhà xuất bản ban đầu của phiên bản mà nó đề cập cho phép
    11. Đối với bất kỳ phần nào có tiêu đề “Lời cảm ơn” hoặc “Cống hiến”, Giữ nguyên Tiêu đề của phần đó và giữ nguyên trong phần này tất cả nội dung và giọng điệu của từng lời cảm ơn và/hoặc cống hiến của người đóng góp được đưa ra trong đó
    12. Giữ nguyên tất cả các Phần bất biến của Tài liệu, không thay đổi nội dung và tiêu đề của chúng. Số phần hoặc tương đương không được coi là một phần của tiêu đề phần
    13. Xóa bất kỳ phần nào có tiêu đề “Chứng thực”. Một phần như vậy có thể không được bao gồm trong Phiên bản sửa đổi
    14. Không đặt lại tiêu đề cho bất kỳ phần hiện có nào thành Có tiêu đề là “Chứng thực” hoặc xung đột về tiêu đề với bất kỳ Phần bất biến nào
    15. Giữ nguyên mọi Tuyên bố miễn trừ trách nhiệm bảo hành

    Nếu Phiên bản đã sửa đổi bao gồm các phần quan trọng nhất hoặc các phụ lục đủ điều kiện là Phần phụ và không chứa tài liệu được sao chép từ Tài liệu, bạn có thể tùy chọn chỉ định một số hoặc tất cả các phần này là bất biến. Để thực hiện việc này, hãy thêm tiêu đề của chúng vào danh sách Phần bất biến trong thông báo giấy phép của Phiên bản đã sửa đổi. Các tiêu đề này phải khác biệt với bất kỳ tiêu đề phần nào khác

    Bạn có thể thêm một phần Có tiêu đề "Xác nhận", miễn là nó không chứa gì ngoài xác nhận của Phiên bản đã sửa đổi của bạn bởi các bên khác nhau—ví dụ: tuyên bố đánh giá ngang hàng hoặc văn bản đã được một tổ chức phê duyệt là định nghĩa có thẩm quyền của tiêu chuẩn

    Bạn có thể thêm một đoạn văn dài tối đa năm từ làm Văn bản Bìa trước và một đoạn văn tối đa 25 từ làm Văn bản Bìa sau, vào cuối danh sách Văn bản Bìa trong Phiên bản đã Sửa đổi. Chỉ một đoạn Văn bản Bìa trước và một đoạn Văn bản Bìa sau có thể được thêm vào bởi [hoặc thông qua sự sắp xếp của] bất kỳ thực thể nào. Nếu Tài liệu đã bao gồm một văn bản trang bìa cho cùng một trang bìa, do bạn thêm trước đó hoặc theo thỏa thuận của cùng một thực thể mà bạn đang đại diện, thì bạn không thể thêm văn bản khác;

    [Các] tác giả và [các] nhà xuất bản của Tài liệu không theo Giấy phép này cho phép sử dụng tên của họ để công khai hoặc để khẳng định hoặc ngụ ý chứng thực bất kỳ Phiên bản đã sửa đổi nào

  6. TỔNG HỢP GIẤY TỜ

    Bạn có thể kết hợp Tài liệu với các tài liệu khác được phát hành theo Giấy phép này, theo các điều khoản được xác định trong phần 4 ở trên đối với các phiên bản sửa đổi, với điều kiện là bạn bao gồm trong phần kết hợp tất cả các Phần bất biến của tất cả các tài liệu gốc, chưa sửa đổi và liệt kê tất cả chúng

    Công việc kết hợp chỉ cần chứa một bản sao của Giấy phép này và nhiều Phần bất biến giống hệt nhau có thể được thay thế bằng một bản sao duy nhất. Nếu có nhiều Phần bất biến có cùng tên nhưng nội dung khác nhau, hãy đặt tiêu đề của từng phần đó thành duy nhất bằng cách thêm vào cuối phần đó, trong ngoặc đơn, tên của tác giả gốc hoặc nhà xuất bản của phần đó nếu biết, hoặc nếu không thì thêm . Thực hiện điều chỉnh tương tự đối với tiêu đề phần trong danh sách Phần bất biến trong thông báo giấy phép của tác phẩm kết hợp

    Trong sự kết hợp, bạn phải kết hợp bất kỳ phần nào có tiêu đề “Lịch sử” trong các tài liệu gốc khác nhau, tạo thành một phần có tiêu đề “Lịch sử”; . Bạn phải xóa tất cả các phần có tiêu đề “Chứng thực. ”

  7. SƯU TẦM TÀI LIỆU

    Bạn có thể tạo một bộ sưu tập bao gồm Tài liệu và các tài liệu khác được phát hành theo Giấy phép này và thay thế các bản sao riêng lẻ của Giấy phép này trong các tài liệu khác nhau bằng một bản sao duy nhất được bao gồm trong bộ sưu tập, miễn là bạn tuân theo các quy tắc của Giấy phép này để

    Bạn có thể trích xuất một tài liệu từ bộ sưu tập như vậy và phân phối riêng lẻ theo Giấy phép này, miễn là bạn chèn một bản sao của Giấy phép này vào tài liệu được trích xuất và tuân theo Giấy phép này trong tất cả các khía cạnh khác liên quan đến việc sao chép nguyên văn tài liệu đó

  8. TỔNG HỢP VỚI TÁC PHẨM ĐỘC LẬP

    Việc biên soạn Tài liệu hoặc các sản phẩm phái sinh của nó cùng với các tài liệu hoặc tác phẩm riêng biệt và độc lập khác, trong hoặc trên một khối lượng phương tiện lưu trữ hoặc phân phối, được gọi là “tổng hợp” nếu bản quyền phát sinh từ việc biên soạn không được sử dụng để hạn chế các quyền hợp pháp . Khi Tài liệu được bao gồm trong một tập hợp, Giấy phép này không áp dụng cho các tác phẩm khác trong tập hợp không phải là tác phẩm phái sinh của Tài liệu

    Nếu yêu cầu về Văn bản bìa của phần 3 được áp dụng cho các bản sao này của Tài liệu, thì nếu Tài liệu ít hơn một nửa tổng số, thì Văn bản bìa của Tài liệu có thể được đặt trên các trang bìa bao quanh Tài liệu trong tổng số, hoặc . Nếu không, chúng phải xuất hiện trên bìa in bao quanh toàn bộ tổng hợp

  9. DỊCH

    Bản dịch được coi là một loại sửa đổi, vì vậy bạn có thể phân phối các bản dịch của Tài liệu theo các điều khoản của phần 4. Việc thay thế các Phần không thay đổi bằng các bản dịch cần có sự cho phép đặc biệt từ chủ sở hữu bản quyền của họ, nhưng bạn có thể bao gồm các bản dịch của một số hoặc tất cả các Phần không thay đổi ngoài các phiên bản gốc của các Phần không thay đổi này. Bạn có thể đính kèm bản dịch của Giấy phép này và tất cả các thông báo về giấy phép trong Tài liệu cũng như bất kỳ Tuyên bố miễn trừ trách nhiệm bảo hành nào, với điều kiện là bạn cũng bao gồm phiên bản gốc tiếng Anh của Giấy phép này và các phiên bản gốc của các thông báo và tuyên bố từ chối trách nhiệm đó. Trong trường hợp có sự bất đồng giữa bản dịch và bản gốc của Giấy phép này hoặc thông báo hoặc tuyên bố từ chối trách nhiệm, bản gốc sẽ được ưu tiên áp dụng

    Nếu một phần trong Tài liệu có Tiêu đề là “Lời cảm ơn”, “Cống hiến” hoặc “Lịch sử”, thì yêu cầu [phần 4] để Giữ nguyên Tiêu đề của nó [phần 1] thường sẽ yêu cầu thay đổi tiêu đề thực tế

  10. CHẤM DỨT

    Bạn không được sao chép, sửa đổi, cấp phép lại hoặc phân phối Tài liệu trừ khi được cung cấp rõ ràng theo Giấy phép này. Bất kỳ nỗ lực nào khác để sao chép, sửa đổi, cấp phép lại hoặc phân phối nó đều vô hiệu và sẽ tự động chấm dứt các quyền của bạn theo Giấy phép này

    Tuy nhiên, nếu bạn chấm dứt mọi hành vi vi phạm Giấy phép này, thì giấy phép của bạn từ một chủ sở hữu bản quyền cụ thể sẽ được khôi phục [a] tạm thời, trừ khi và cho đến khi chủ sở hữu bản quyền chấm dứt giấy phép của bạn một cách rõ ràng và cuối cùng, và [b] vĩnh viễn, nếu chủ sở hữu bản quyền không thành công

    Ngoài ra, giấy phép của bạn từ một chủ sở hữu bản quyền cụ thể sẽ được khôi phục vĩnh viễn nếu chủ sở hữu bản quyền thông báo cho bạn về vi phạm bằng một số phương tiện hợp lý, đây là lần đầu tiên bạn nhận được thông báo vi phạm Giấy phép này [đối với bất kỳ tác phẩm nào] từ chủ sở hữu bản quyền đó,

    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. Nếu các quyền của bạn đã bị chấm dứt và không được khôi phục vĩnh viễn, thì việc nhận một bản sao của một số hoặc tất cả cùng một tài liệu sẽ không cấp cho bạn bất kỳ quyền nào để sử dụng tài liệu đó

  11. CÁC SỬA ĐỔI TRONG TƯƠNG LAI CỦA GIẤY PHÉP NÀY

    Tổ chức Phần mềm Tự do thỉnh thoảng có thể xuất bản các phiên bản mới, sửa đổi của Giấy phép Tài liệu Miễn phí GNU. Các phiên bản mới như vậy sẽ tương tự về mặt tinh thần với phiên bản hiện tại, nhưng có thể khác về chi tiết để giải quyết các vấn đề hoặc mối quan tâm mới. Xem https. //www. gnu. tổ chức/copyleft/

    Mỗi phiên bản của Giấy phép được cung cấp một số phiên bản phân biệt. Nếu Tài liệu chỉ định rằng một phiên bản được đánh số cụ thể của Giấy phép này “hoặc bất kỳ phiên bản nào mới hơn” áp dụng cho nó, thì bạn có tùy chọn tuân theo các điều khoản và điều kiện của phiên bản được chỉ định đó hoặc của bất kỳ phiên bản nào sau này đã được xuất bản [không phải là . Nếu Tài liệu không chỉ định số phiên bản của Giấy phép này, bạn có thể chọn bất kỳ phiên bản nào đã từng được Tổ chức Phần mềm Tự do xuất bản [không phải dưới dạng bản nháp]. Nếu Tài liệu chỉ định rằng một người được ủy quyền có thể quyết định phiên bản nào trong tương lai của Giấy phép này có thể được sử dụng, thì tuyên bố công khai của người ủy quyền đó về việc chấp nhận một phiên bản sẽ cho phép bạn vĩnh viễn chọn phiên bản đó cho Tài liệu

  12. GIẤY PHÉP

    “Trang web hợp 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 đó. Một wiki công cộng mà bất kỳ ai cũng có thể chỉnh sửa là một ví dụ về một máy chủ như vậy. “Cộng tác nhiều tác giả lớn” [hoặc “MMC”] có trong trang web có nghĩa là bất kỳ bộ tác phẩm có bản quyền nào được xuất bản trên trang web MMC

    “CC-BY-SA” có nghĩa là Creative Commons Ghi công-Chia sẻ tương tự 3. 0 được phát hành bởi Creative Commons Corporation, một công ty phi lợi nhuận có trụ sở kinh doanh chính tại San Francisco, California, cũng như các phiên bản copyleft trong tương lai của giấy phép đó được phát hành bởi chính tổ chức đó

    “Kết hợp” có nghĩa là xuất bản hoặc xuất bản lại một Tài liệu, toàn bộ hoặc một phần, như một phần của Tài liệu khác

    Một MMC “đủ điều kiện để cấp phép lại” nếu nó được cấp phép theo Giấy phép này và nếu tất cả các tác phẩm được xuất bản lần đầu theo Giấy phép này ở một nơi nào đó ngoài MMC này và sau đó được tích hợp toàn bộ hoặc một phần vào MMC, [1] không có

    Người điều hành một Trang web MMC có thể xuất bản lại một MMC có trong trang web theo CC-BY-SA trên cùng một trang web vào bất kỳ thời điểm nào trước ngày 1 tháng 8 năm 2009, với điều kiện là MMC đủ điều kiện để cấp phép lại

PHỤ LỤC. Cách sử dụng Giấy phép này cho tài liệu của bạn

Để sử dụng Giấy phép này trong tài liệu bạn đã viết, hãy đưa một bản sao của Giấy phép vào tài liệu và đặt thông báo bản quyền và giấy phép sau đây ngay sau trang tiêu đề

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

Nếu bạn có Phần bất biến, Văn bản bìa trước và Văn bản bìa sau, hãy thay thế “bằng…Văn bản. ” phù hợp với điều này

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

Nếu bạn có Phần bất biến không có Văn bản bìa hoặc một số kết hợp khác của cả ba, hãy hợp nhất hai lựa chọn thay thế đó cho phù hợp với tình huống

Nếu tài liệu của bạn chứa các ví dụ không cần thiết về mã chương trình, chúng tôi khuyên bạn nên phát hành song song các ví dụ này theo lựa chọn giấy phép phần mềm tự do của bạn, chẳng hạn như Giấy phép Công cộng GNU, để cho phép sử dụng chúng trong phần mềm miễn phí

Chỉ mục khái niệm

Đây là chỉ mục chung về tất cả các vấn đề được thảo luận trong sách hướng dẫn này, ngoại trừ các lệnh

sed -i 's/hello/world/' file.txt
22 và các tùy chọn dòng lệnh

Chủ Đề