Python regex bất kỳ chữ cái nào

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. Tổng quan

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

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

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

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

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

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

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

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

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

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

If no extension is supplied, the original file is overwritten without making a backup

Because -i takes an optional argument, it should not be followed by other short options

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

This is equivalent to --in-place=E, creating FILEE as backup of FILE

Be cautious of using -n with -i. cái trước tắt tự động in các dòng và cái sau thay đổi tệp tại chỗ mà không cần sao lưu. Được sử dụng bất cẩn [và không có lệnh

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

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

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

Specify the default line-wrap length for the

sed -i 's/hello/world/' file.txt
67 command. Độ 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. In order to simplify writing portable scripts, this option disables all the extensions that this manual documents, including additional commands. 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. If you want to disable only the latter kind of extension, you can set the
sed -i 's/hello/world/' file.txt
72 variable to a non-empty value

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. When such a distinction is made—as is the case for MS-DOS, Windows, Cygwin—text files are composed of lines separated by a carriage return and a line feed character, and

sed -i 's/hello/world/' file.txt
22 does not see the ending CR. 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

Use extended regular expressions rather than basic regular expressions. Extended regexps are those that

sed -i 's/hello/world/' file.txt
82 accepts; they can be clearer because they usually have fewer backslashes. 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

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

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

________ 094 ________ 095 ________ 096

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

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

Nếu bất kỳ tham số dòng lệnh nào vẫn còn sau khi xử lý ở trên, các tham số này được hiểu là tên của tệp đầu vào sẽ được xử lý. Tên tệp '-' đề cập đến luồng đầu vào tiêu chuẩn. The standard input will be processed if no file names are specified

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
sed -i 's/hello/world/' file.txt
22 scripts

3. 1
sed -i 's/hello/world/' file.txt
22 script overview

Một 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 sử dụng các tùy chọn này. Tài liệu này sẽ đề cập đến “tập lệnh”
sed -i 's/hello/world/' file.txt
22; . See Overview

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. If
sed -i 's/hello/world/' file.txt
911 is specified, the command X will be executed only on the matched lines.
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

The following example deletes lines 30 to 35 in the input.

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

The following example prints all input until a line starting with the word ‘foo’ is found. If such line is found,

sed -i 's/hello/world/' file.txt
22 will terminate with exit status 42. If such line was not found [and no other error occurred],
sed -i 's/hello/world/' file.txt
22 will exit with status 0.
sed -i 's/hello/world/' file.txt
920 is a regular-expression address.
sed -i 's/hello/world/' file.txt
900 is the quit command.
sed -i 's/hello/world/' file.txt
922 is the command option

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

Commands within a script or script-file can be separated by semicolons [

sed -i 's/hello/world/' file.txt
923] or newlines [ASCII 10]. Multiple scripts can be specified with -e or -f options

The following examples are all equivalent. They perform two

sed -i 's/hello/world/' file.txt
22 operations. deleting any lines matching the regular expression
sed -i 's/hello/world/' file.txt
920, and replacing all occurrences of the string ‘hello’ with ‘world’

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

Commands

sed -i 's/hello/world/' file.txt
926,
sed -i 's/hello/world/' file.txt
927,
sed -i 's/hello/world/' file.txt
928, due to their syntax, cannot be followed by semicolons working as command separators and thus should be terminated with newlines or be placed at the end of a script or script-file. Commands can also be preceded with optional non-significant whitespace characters. See Multiple commands syntax

3. 2
sed -i 's/hello/world/' file.txt
22 commands summary

The following commands are supported in GNU

sed -i 's/hello/world/' file.txt
22. Some are standard POSIX commands, while other are GNU extensions. Details and examples for each command are in the following sections. [Mnemonics] are shown in parentheses

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

Append text after a line

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

Append text after a line [alternative syntax]

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

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

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

Replace [change] lines with text

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

Replace [change] lines with text [alternative syntax]

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

Delete the pattern space; immediately start next cycle

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

If pattern space contains newlines, 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

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

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

Executes the command that is found in pattern space and replaces the pattern space with the output; a trailing newline is suppressed

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

Executes 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

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

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

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

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

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

Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space

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

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

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

Append a newline to the contents of the hold space, and then append the contents of the pattern space to that of the hold space

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

insert text before a line

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

insert text before a line [alternative syntax]

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

Print the pattern space in an unambiguous form

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

[next] If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then

sed -i 's/hello/world/' file.txt
22 exits without processing any more commands

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

Add a newline to the pattern space, then append the next line of input to the pattern space. If there is no more input then

sed -i 's/hello/world/' file.txt
22 exits without processing any more commands

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

Print the pattern space

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

Print the pattern space, up to the first

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

[quit] Exit

sed -i 's/hello/world/' file.txt
22 without processing any more commands or input

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

[quit] 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

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

Reads file filename

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

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. If found, replace matched string with replacement

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

[test] Branch to label only if there has been a successful

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

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

[test] Branch to label only if there have been no successful

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

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

[version] 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, or if the requested version is not available

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

Write the pattern space to filename

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

Write to the given filename the portion of the pattern space up to the first newline

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

Transliterate any characters in the pattern space which match any of the source-chars with the corresponding character in dest-chars

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

[zap] This command empties the content of pattern space

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

A comment, until the next newline

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

Group several commands together

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

Print the current input line number [with a trailing newline]

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

Specify the location of label for branch commands [

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

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

The

sed -i 's/hello/world/' file.txt
967 command [as in substitute] is probably the most important in
sed -i 's/hello/world/' file.txt
22 and has a lot of different options. The syntax of the
sed -i 's/hello/world/' file.txt
967 command is ‘s/regexp/replacement/flags’

Its basic concept is simple. the

sed -i 's/hello/world/' file.txt
967 command attempts to match the pattern space against the supplied regular expression regexp; if the match is successful, then that portion of the pattern space which was matched is replaced with replacement

For details about regexp syntax see Regular Expression Addresses

The replacement can contain

sed -i 's/hello/world/' file.txt
990 [n being a number from 1 to 9, inclusive] references, which refer to the portion of the match which is contained between the nth
sed -i 's/hello/world/' file.txt
991 and its matching
sed -i 's/hello/world/' file.txt
992. Also, the replacement can contain unescaped
sed -i 's/hello/world/' file.txt
993 characters which reference the whole matched portion of the pattern space

The

sed -i 's/hello/world/' file.txt
994 characters may be uniformly replaced by any other single character within any given
sed -i 's/hello/world/' file.txt
967 command. The
sed -i 's/hello/world/' file.txt
994 character [or whatever other character is used in its stead] can appear in the regexp or replacement only if it is preceded by a
sed -i 's/hello/world/' file.txt
997 character

Finally, as a GNU

sed -i 's/hello/world/' file.txt
22 extension, you can include a special sequence made of a backslash and one of the letters
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, or
sed -i 's/hello/world/' file.txt
103. The meaning is as follows

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

Turn the replacement to lowercase until a

sed -i 's/hello/world/' file.txt
105 or
sed -i 's/hello/world/' file.txt
106 is found,

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

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

Lệnh

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

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

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

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

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

tương tác trong lệnh

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

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

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

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

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

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

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

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

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

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

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

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

Công cụ sửa đổi

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

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

Công cụ sửa đổi

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

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

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

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

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

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

Nhân vật

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

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

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

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

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

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

Thoát

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

Ví 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

Delete the pattern space; immediately start next cycle

Ví 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

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

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

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

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

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

Ví 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

Ví 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

Transliterate any characters in the pattern space which match any of the source-chars with the corresponding character in dest-chars

Ví 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

Ví dụ. 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

Ví 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 nhưng kết thúc 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ỉ

Escape sequences in text are processed, so you should use

sed -i 's/hello/world/' file.txt
195 in text to print a single backslash

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

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

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

Leading whitespace after the

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

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

Immediately output the lines of text which follow this command

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

Escape sequences in text are processed, so you should use

sed -i 's/hello/world/' file.txt
195 in text to print a single backslash

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

Replaces the line[s] with text. Đâ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

Ví dụ. Replace the 2nd to 9th lines with the word ‘hello’

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

Leading whitespace after the

sed -i 's/hello/world/' file.txt
927 command is ignored. 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

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

If no addresses are given, each line is replaced

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

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

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

As a GNU extension, the

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

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

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

Print out the current input line number [with a trailing newline]

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

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 specifies the desired line-wrap length; a length of 0 [zero] means to never wrap long lines. 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

Reads file filename. Ví 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. Note that if filename cannot be read, it is treated as if it were an empty file, without any error indication

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. As a GNU

sed -i 's/hello/world/' file.txt
22 extension, two special values of filename are supported. /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

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. Nếu không, hãy xóa văn bản trong không gian mẫu cho đến dòng mới đầu tiên và khởi động lại chu trình với không gian mẫu kết quả mà không cần đọc dòng đầu vào mới

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

Add a newline to the pattern space, then append the next line of input to the pattern space. If there is no more input then

sed -i 's/hello/world/' file.txt
22 exits without processing any more commands

Khi -z được sử dụng, một byte bằng 0 [ký tự ascii ‘NUL’] được thêm vào giữa các dòng [thay vì một dòng mới]

Theo mặc định,

sed -i 's/hello/world/' file.txt
22 không kết thúc nếu không có dòng đầu vào 'tiếp theo'. Đây là tiện ích mở rộng GNU có thể bị tắt bằng --posix. Xem lệnh N trên dòng cuối cùng

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

In ra phần không gian mẫu cho đến dòng mới đầu tiên

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

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

Append a newline to the contents of the hold space, and then append the contents of the pattern space to that of the hold space

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

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

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

Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space

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

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

3. 6 Lệnh dành cho chuyên gia
sed -i 's/hello/world/' file.txt
22

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

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
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 Lệnh dành riêng cho GNU
sed -i 's/hello/world/' file.txt
22

These commands are specific to GNU

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

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

Lệnh này cho phép một người chuyển đầu vào từ lệnh shell sang không gian mẫu. Không có tham số, lệ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;

Thay vào đó, nếu một tham số được chỉ định, lệnh

sed -i 's/hello/world/' file.txt
941 sẽ diễn giải nó thành một lệnh và gửi đầu ra của nó tới luồng đầu ra. Lệnh có thể chạy trên nhiều dòng, trừ dòng cuối cùng có dấu gạch chéo ngược

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

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

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

This command accepts only one address

This command is the same as

sed -i 's/hello/world/' file.txt
900, but will not print the contents of pattern space. 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

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. Lưu ý rằng nếu tên tệp không thể đọc được hoặc nếu đã đến cuối thì không có dòng nào được thêm vào mà không có bất kỳ dấu hiệu lỗi nào

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

Chỉ phân 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
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. Ngoài ra, bạn có thể chỉ định phiên bản
sed -i 's/hello/world/' file.txt
22 mà tập lệnh của bạn yêu cầu, chẳng hạn như
sed -i 's/hello/world/' file.txt
369. The default is
sed -i 's/hello/world/' file.txt
370 because that is the first version that implemented this command

Lệnh này cho phép tất cả các tiện ích mở rộng GNU ngay cả khi

sed -i 's/hello/world/' file.txt
72 được đặt trong môi trường

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. Mọi thứ được nói dưới lệnh

sed -i 's/hello/world/' file.txt
326 về việc xử lý tệp cũng được giữ ở đây

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

Lệnh này làm trống nội dung của không gian mẫu. It is usually the same as ‘s/. *//', nhưng hiệu quả hơn và hoạt động khi có chuỗi nhiều byte không hợp lệ trong luồng đầu vào. POSIX mandates that such sequences are not matched by ‘. ’, vì vậy không có cách di động nào để xóa bộ đệm của

sed -i 's/hello/world/' file.txt
22 ở giữa tập lệnh ở hầu hết các ngôn ngữ nhiều byte [bao gồm cả ngôn ngữ UTF-8]

3. 8 Multiple commands syntax

Có một số phương pháp để chỉ định nhiều lệnh trong chương trình

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

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

Trên dòng lệnh, tất cả các lệnh

sed -i 's/hello/world/' file.txt
22 có thể được phân tách bằng dòng mới. Alternatively, you may specify each command as an argument to an -e option

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

Dấu chấm phẩy [‘;’] có thể được sử dụng để phân tách hầu hết các lệnh đơn giản

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

The

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

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

Labels used in

sed -i 's/hello/world/' file.txt
982,
sed -i 's/hello/world/' file.txt
983,
sed -i 's/hello/world/' file.txt
984,
sed -i 's/hello/world/' file.txt
383 commands are read until a semicolon. 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. The second is a portable equivalent. For more information about branching and labels see Branching and flow control

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

3. 8. 1 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,
sed -i 's/hello/world/' file.txt
927,
sed -i 's/hello/world/' file.txt
928 [append/change/insert]

All characters following

sed -i 's/hello/world/' file.txt
926,
sed -i 's/hello/world/' file.txt
927,
sed -i 's/hello/world/' file.txt
928 commands are taken as the text to append/change/insert. 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

Separate the commands using -e or a newline

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

Lưu ý rằng việc chỉ định văn bản để thêm ['Xin chào'] ngay sau

sed -i 's/hello/world/' file.txt
926,
sed -i 's/hello/world/' file.txt
927,
sed -i 's/hello/world/' file.txt
928 chính nó là một tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt
22. A portable, POSIX-compliant alternative is

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

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

Tất cả các ký tự theo sau '#' cho đến dòng mới tiếp theo đều bị bỏ qua

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

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

The

sed -i 's/hello/world/' file.txt
354,
sed -i 's/hello/world/' file.txt
88,
sed -i 's/hello/world/' file.txt
326,
sed -i 's/hello/world/' file.txt
33 commands parse the filename until end of the line. 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 ; . The file is missing, and the error is silently ignored

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

sed -i 's/hello/world/' file.txt
941 [command execution]

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. If whitespace, comments or semicolons are found, they will be included in the shell command, leading to unexpected results

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]

Trong một lệnh thay thế, cờ

sed -i 's/hello/world/' file.txt
326 ghi kết quả thay thế vào một tệp và cờ
sed -i 's/hello/world/' file.txt
941 thực thi kết quả thay thế dưới dạng lệnh trình bao. Như với các lệnh
sed -i 's/hello/world/' file.txt
622, chúng phải được kết thúc bằng một dòng mới. 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 shell hoặc tên tệp, dẫn đến kết quả không mong muốn

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

4 địa chỉ. chọn dòng

4. 1 Addresses overview

Địa chỉ xác định [các] dòng lệnh

sed -i 's/hello/world/' file.txt
22 sẽ được thực thi. Lệnh sau thay thế từ 'hello' bằng 'world' chỉ trên dòng 144

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

Nếu không có địa chỉ nào được đưa ra, lệnh được thực hiện trên tất cả các dòng. The following command replaces the word ‘hello’ with ‘world’ on all lines in the input file

________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]. Địa chỉ có thể là số, biểu thức chính quy hoặc kết hợp cả hai. Lệnh sau thay thế từ 'xin chào' bằng 'thế giới' chỉ trong các dòng 4 đến 17 [đã bao gồm]

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

Việc thêm ký tự

sed -i 's/hello/world/' file.txt
625 vào cuối thông số địa chỉ [trước chữ cái lệnh] sẽ phủ nhận ý nghĩa của sự trùng khớp. Nghĩa là, nếu ký tự
sed -i 's/hello/world/' file.txt
625 theo sau một địa chỉ hoặc một dải địa chỉ, thì chỉ những dòng không khớp với địa chỉ mới được chọn. 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. trừ dòng 4 đến 17]

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

4. 2 Selecting lines by numbers

Địa chỉ trong tập lệnh

sed -i 's/hello/world/' file.txt
22 có thể ở bất kỳ dạng nào sau đây

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

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

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

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

Địa chỉ này khớp với dòng cuối cùng của tệp đầu vào cuối cùng hoặc dòng cuối cùng của mỗi tệp khi tùy chọn -i hoặc -s được chỉ định

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

Các lệnh sau minh họa việc sử dụng địa chỉ bước

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

4. 3 dòng chọn bằng cách khớp văn bản

GNU

sed -i 's/hello/world/' file.txt
22 hỗ trợ các địa chỉ biểu thức chính quy sau. Biểu thức chính quy mặc định là Biểu thức chính quy cơ bản [BRE]. If -E or -r options are used, The regular expression should be in Extended Regular Expression [ERE] syntax. Xem BRE so với ERE

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

Điều này sẽ chọn bất kỳ dòng nào khớp với biểu thức chính quy. 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]

Lệnh sau in các dòng trong /etc/passwd kết thúc bằng 'bash'5

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

Biểu thức chính quy trống '//' lặp lại kết quả khớp biểu thức chính quy cuối cùng [tương tự nếu biểu thức chính quy trống được truyền cho lệnh

sed -i 's/hello/world/' file.txt
967]. 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. This is particularly useful if the regexp itself contains a lot of slashes, since it avoids the tedious escaping of every
sed -i 's/hello/world/' file.txt
994. Nếu bản thân biểu thức chính quy bao gồm bất kỳ ký tự phân cách 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 commands are equivalent. Họ in các dòng bắt đầu bằng '/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

Công cụ sửa đổi

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

Trong nhiều ngôn ngữ lập trình khác, chữ thường

sed -i 's/hello/world/' file.txt
928 được sử dụng để đối sánh biểu thức chính quy không phân biệt chữ hoa chữ thường. Tuy nhiên, trong
sed -i 's/hello/world/' file.txt
22,
sed -i 's/hello/world/' file.txt
928 được sử dụng cho lệnh chèn [xem lệnh chèn]

Quan sát sự khác biệt giữa các ví dụ sau

Trong ví dụ này,

sed -i 's/hello/world/' file.txt
651 là địa chỉ. regular expression with
sed -i 's/hello/world/' file.txt
145 modifier.
sed -i 's/hello/world/' file.txt
36 là lệnh xóa

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. If the pattern space is changed [for example with

sed -i 's/hello/world/' file.txt
666 command] the regular expression matching will operate on the changed text

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. The following command prints starting at line 6, until the next line which is a multiple of 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

In GNU

sed -i 's/hello/world/' file.txt
22, the only difference between basic and extended regular expressions is in the behavior of a few special characters. ‘?’, ‘+’, parentheses, braces [‘{}’], and ‘. ’

With basic [BRE] syntax, these characters do not have special meaning unless prefixed with a backslash [‘\’]; While with extended [ERE] syntax it is reversed. 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

Matches any single character in list. 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

As

sed -i 's/hello/world/' file.txt
60, but matches one or more. 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. điều này sẽ tìm kiếm không hoặc nhiều hơn toàn bộ chuỗi 'abcd', trong khi
    sed -i 's/hello/world/' file.txt
    
    933 sẽ tìm kiếm 'abc' theo sau là không hoặc nhiều lần xuất hiện của '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. This is called a back reference. Subexpressions are implicitly numbered by counting occurrences of
sed -i 's/hello/world/' file.txt
991 left-to-right

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

Matches the newline character

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

Matches char, where char is one of

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,
sed -i 's/hello/world/' file.txt
997, or
sed -i 's/hello/world/' file.txt
154. Note that the only C-like backslash sequences that you can portably assume to be interpreted are
sed -i 's/hello/world/' file.txt
990 and
sed -i 's/hello/world/' file.txt
195; in particular
sed -i 's/hello/world/' file.txt
953 is not portable, and matches a ‘t’ under most implementations of
sed -i 's/hello/world/' file.txt
22, rather than a tab character

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. It matches ‘a’ or ‘b’

5. 5 Character Classes and Bracket Expressions

A bracket expression is a list of characters enclosed by ‘[’ and ‘]’. 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. In the default C locale, the sorting sequence is the native character order; for example, ‘[a-d]’ is equivalent to ‘[abcd]’

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

These named classes must be used inside brackets themselves. Correct usage

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

Incorrect usage is rejected by newer

sed -i 's/hello/world/' file.txt
22 versions. 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. ]’

Alphanumeric characters. ‘[. alpha. ]’ and ‘[. digit. ]’; 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. ]’

Graphical characters. ‘[. 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. See Escapes

‘[. ’

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. Điều này khác với

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

Compare the following two examples

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

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

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

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

5. 7 Back-references and Subexpressions

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’ [backspace] was omitted because of the conflict with the existing “word boundary” meaning

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 Chuyển đổi chữ hoa/chữ thường

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

See The "s" Command

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 maintains two data buffers. the active pattern space, and the auxiliary hold space. Both are initially empty

sed -i 's/hello/world/' file.txt
22 operates by performing the following cycle on each line of input. 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. Then commands are executed; each command can have an address associated to it. addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed

When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed. 8 Then the next cycle starts for the next input line

Unless special commands [like ‘D’] are used, the pattern space is deleted between two cycles. The hold space, on the other hand, keeps its data between cycles [see commands ‘h’, ‘H’, ‘x’, ‘g’, ‘G’ to move data between both buffers]

6. 2 Hold and Pattern Buffers

TODO

6. 3 Multiline techniques - using D,G,H,N,P to process multiple lines

Multiple lines can be processed as one buffer using the

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. They are similar to their lowercase counterparts [
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], except that these commands append or subtract data while respecting embedded newlines - allowing adding and removing lines from the pattern and hold spaces

They operate as follows

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

deletes line from the pattern space until the first newline, and restarts the cycle

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

appends line from the hold space to the pattern space, with a newline before it

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

appends line from the pattern space to the hold space, with a newline before it

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

appends line from the input file to the pattern space

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

prints line from the pattern space until the first newline

The following example illustrates the operation of

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
044

  1. sed -i 's/hello/world/' file.txt
    
    22 starts by reading the first line into the pattern space [i. e. ‘1’]
  2. At the beginning of every cycle, the
    sed -i 's/hello/world/' file.txt
    
    71 command appends a newline and the next line to the pattern space [i. e. ‘1’, ‘\n’, ‘2’ in the first cycle]
  3. The
    sed -i 's/hello/world/' file.txt
    
    67 command prints the content of the pattern space unambiguously
  4. The
    sed -i 's/hello/world/' file.txt
    
    939 command then removes the content of pattern space up to the first newline [leaving ‘2’ at the end of the first cycle]
  5. At the next cycle the
    sed -i 's/hello/world/' file.txt
    
    71 command appends a newline and the next input line to the pattern space [e. g. ‘2’, ‘\n’, ‘3’]

A common technique to process blocks of text such as paragraphs [instead of line-by-line] is using the following construct

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

  1. The first expression,
    sed 's/hello/world/' input.txt > output.txt
    
    974 operates on all non-empty lines, and adds the current line [in the pattern space] to the hold space. On all lines except the last, the pattern space is deleted and the cycle is restarted
  2. The other expressions
    sed -i 's/hello/world/' file.txt
    
    975 and
    sed -i 's/hello/world/' file.txt
    
    967 are executed only on empty lines [i. e. paragraph separators]. The
    sed -i 's/hello/world/' file.txt
    
    975 command fetches the accumulated lines from the hold space back to the pattern space. The
    sed -i 's/hello/world/' file.txt
    
    666 command then operates on all the text in the paragraph [including the embedded newlines]

The following example demonstrates this technique

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

For more annotated examples, see Text search across multiple lines and Line length adjustment

6. 4 Branching and Flow Control

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. Trước khi đọc dòng đầu vào tiếp theo,
sed -i 's/hello/world/' file.txt
952 in không gian mẫu hiện tại sau đó làm trống nó, trong khi
sed -i 's/hello/world/' file.txt
71 nối một dòng mới và dòng đầu vào tiếp theo vào không gian mẫu

Xét hai ví dụ sau

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

  • Cả hai ví dụ không inf-loop, mặc dù không bao giờ bắt đầu một chu kỳ mới
  • Trong ví dụ đầu tiên, các lệnh
    sed -i 's/hello/world/' file.txt
    
    952 trước tiên in nội dung của không gian mẫu, làm trống không gian mẫu sau đó đọc dòng đầu vào tiếp theo
  • Trong ví dụ thứ hai, các lệnh
    sed -i 's/hello/world/' file.txt
    
    71 nối thêm dòng đầu vào tiếp theo vào không gian mẫu [với một dòng mới]. 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. Khi chương trình kết thúc, các hành động cuối chu kỳ được thực hiện và toàn bộ không gian mẫu được in
  • Ví dụ thứ hai yêu cầu GNU
    sed -i 's/hello/world/' file.txt
    
    22, vì nó sử dụng hành vi không chuẩn POSIX của
    sed -i 's/hello/world/' file.txt
    
    71. Xem đoạn “lệnh ______071 ở dòng cuối cùng” trong Báo cáo lỗi
  • Để kiểm tra thêm sự khác biệt giữa hai ví dụ, hãy thử các lệnh sau

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

6. 4. 2 Ví dụ phân nhánh. đường nối

Như một ví dụ thực tế về việc sử dụng phân nhánh, hãy xem xét trường hợp các tệp có thể in được trích dẫn, thường được sử dụng để mã hóa thư email. Trong các tệp này, các dòng dài được chia nhỏ và được đánh dấu bằng ngắt dòng mềm bao gồm một ký tự '=' duy nhất ở cuối dòng

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. Nếu không gian mẫu hiện tại kết thúc bằng dấu '=', nó sẽ đọc dòng đầu vào tiếp theo bằng cách sử dụng

sed -i 's/hello/world/' file.txt
71, thay thế tất cả các ký tự '=' được theo sau bởi một dòng mới và phân nhánh vô điều kiện [
sed -i 's/hello/world/' file.txt
982] đến đầu chương trình mà không cần khởi động lại một dòng mới . If the pattern space does not ends with ‘=’, the default action is performed. the pattern space is printed and a new cycle is started

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

Here’s an alternative program with a slightly different approach. On all lines except the last,

sed -i 's/hello/world/' file.txt
71 appends the line to the pattern space. A substitution command then removes soft line breaks [‘=’ at the end of a line, i. e. followed by a newline] by replacing them with an empty string. if the substitution was successful [meaning the pattern space contained a line which should be joined], The conditional branch command
sed -i 's/hello/world/' file.txt
983 jumps to the beginning of the program without completing or restarting the cycle. If the substitution failed [meaning there were no soft line breaks], The
sed -i 's/hello/world/' file.txt
983 command will not branch. Then,
sed -i 's/hello/world/' file.txt
957 will print the pattern space content until the first newline, and
sed -i 's/hello/world/' file.txt
939 will delete the pattern space content until the first new line. [To learn more about
sed -i 's/hello/world/' file.txt
71,
sed -i 's/hello/world/' file.txt
957 and
sed -i 's/hello/world/' file.txt
939 commands see Multiline techniques]

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

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

7 Some Sample Scripts

Here are some

sed -i 's/hello/world/' file.txt
22 scripts to guide you in the art of mastering
sed -i 's/hello/world/' file.txt
22

7. 1 Joining lines

This section uses

sed -i 's/hello/world/' file.txt
71,
sed -i 's/hello/world/' file.txt
939 and
sed -i 's/hello/world/' file.txt
957 commands to process multiple lines, and the
sed -i 's/hello/world/' file.txt
982 and
sed -i 's/hello/world/' file.txt
983 commands for branching. See Multiline techniques and Branching and flow control

Join specific lines [e. g. if lines 2 and 3 need to be joined]

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

Join backslash-continued lines

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

Join lines that start with whitespace [e. g SMTP headers]

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

7. 2 Centering Lines

This script centers all lines of a file on a 80 columns width. To change that width, the number in

sed -i 's/hello/world/' file.txt
0439 must be replaced, and the number of added spaces also must be changed

Note how the buffer commands are used to separate parts in the regular expressions to be matched—this is a common technique

sed -i 's/hello/world/' file.txt
007
sed -i 's/hello/world/' file.txt
008
sed -i 's/hello/world/' file.txt
009
sed -i 's/hello/world/' file.txt
010
sed -i 's/hello/world/' file.txt
011
sed -i 's/hello/world/' file.txt
012

7. 3 Increment a Number

This script is one of a few that demonstrate how to do arithmetic in

sed -i 's/hello/world/' file.txt
22. This is indeed possible,9 but must be done manually

To increment one number you just add 1 to last digit, replacing it by the following digit. There is one exception. when the digit is a nine the previous digits must be also incremented until you don’t have a nine

This solution by Bruno Haible is very clever and smart because it uses a single buffer; if you don’t have this limitation, the algorithm used in Numbering lines, is faster. It works by replacing trailing nines with an underscore, then using multiple

sed -i 's/hello/world/' file.txt
967 commands to increment the last digit, and then again substituting underscores with zeros

sed -i 's/hello/world/' file.txt
013
sed -i 's/hello/world/' file.txt
014
sed -i 's/hello/world/' file.txt
015
sed -i 's/hello/world/' file.txt
016
sed -i 's/hello/world/' file.txt
017

7. 4 Rename Files to Lower Case

This is a pretty strange use of

sed -i 's/hello/world/' file.txt
22. We transform text, and transform it to be shell commands, then just feed them to shell. Don’t worry, even worse hacks are done when using
sed -i 's/hello/world/' file.txt
22; I have seen a script converting the output of
sed -i 's/hello/world/' file.txt
0444 into a
sed -i 's/hello/world/' file.txt
0445 program

The main body of this is the

sed -i 's/hello/world/' file.txt
22 script, which remaps the name from lower to upper [or vice-versa] and even checks out if the remapped name is the same as the original name. Note how the script is parameterized using shell variables and proper quoting

sed -i 's/hello/world/' file.txt
018
sed -i 's/hello/world/' file.txt
019
sed -i 's/hello/world/' file.txt
020
sed -i 's/hello/world/' file.txt
021
sed -i 's/hello/world/' file.txt
022
sed -i 's/hello/world/' file.txt
023
sed -i 's/hello/world/' file.txt
024
sed -i 's/hello/world/' file.txt
025
sed -i 's/hello/world/' file.txt
026
sed -i 's/hello/world/' file.txt
027
sed -i 's/hello/world/' file.txt
028
sed -i 's/hello/world/' file.txt
029
sed -i 's/hello/world/' file.txt
030
sed -i 's/hello/world/' file.txt
031
sed -i 's/hello/world/' file.txt
032
sed -i 's/hello/world/' file.txt
033
sed -i 's/hello/world/' file.txt
034
sed -i 's/hello/world/' file.txt
035
sed -i 's/hello/world/' file.txt
036
sed -i 's/hello/world/' file.txt
037
sed -i 's/hello/world/' file.txt
038
sed -i 's/hello/world/' file.txt
039
sed -i 's/hello/world/' file.txt
040

7. 5 Print
sed -i 's/hello/world/' file.txt
0447 Environment

This script strips the definition of the shell functions from the output of the

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

sed -i 's/hello/world/' file.txt
041
sed -i 's/hello/world/' file.txt
042
sed -i 's/hello/world/' file.txt
043
sed -i 's/hello/world/' file.txt
044
sed -i 's/hello/world/' file.txt
045
sed -i 's/hello/world/' file.txt
046
sed -i 's/hello/world/' file.txt
047

7. 6 Reverse Characters of Lines

This script can be used to reverse the position of characters in lines. The technique moves two characters at a time, hence it is faster than more intuitive implementations

Note the

sed -i 's/hello/world/' file.txt
0449 command before the definition of the label. This is often needed to reset the flag that is tested by the
sed -i 's/hello/world/' file.txt
983 command

Imaginative readers will find uses for this script. An example is reversing the output of

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

sed -i 's/hello/world/' file.txt
048
sed -i 's/hello/world/' file.txt
049
sed -i 's/hello/world/' file.txt
050
sed -i 's/hello/world/' file.txt
051

7. 7 Text search across multiple lines

This section uses

sed -i 's/hello/world/' file.txt
71 and
sed -i 's/hello/world/' file.txt
939 commands to search for consecutive words spanning multiple lines. See Multiline techniques

These examples deal with finding doubled occurrences of words in a document

Finding doubled words in a single line is easy using GNU

sed -i 's/hello/world/' file.txt
687 and similarly with GNU
sed -i 's/hello/world/' file.txt
22

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

  • The regular expression ‘\b\w+\s+’ searches for word-boundary [‘\b’], followed by one-or-more word-characters [‘\w+’], followed by whitespace [‘\s+’]. See regexp extensions
  • Adding parentheses around the ‘[\w+]’ expression creates a subexpression. The regular expression pattern ‘[PATTERN]\s+\1’ defines a subexpression [in the parentheses] followed by a back-reference, separated by whitespace. A successful match means the PATTERN was repeated twice in succession. See Back-references and Subexpressions
  • The word-boundery expression [‘\b’] at both ends ensures partial words are not matched [e. g. 'the then' không phải là một kết hợp mong muốn]
  • The -E option enables extended regular expression syntax, alleviating the need to add backslashes before the parenthesis. See ERE syntax

When the doubled word span two lines the above regular expression will not find them as

sed -i 's/hello/world/' file.txt
687 and
sed -i 's/hello/world/' file.txt
22 operate line-by-line

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

  • The
    sed -i 's/hello/world/' file.txt
    
    71 command appends the next line to the pattern space [thus ensuring it contains two consecutive lines in every cycle]
  • The regular expression uses ‘\s+’ for word separator which matches both spaces and newlines
  • The regular expression matches, the entire pattern space is printed with
    sed -i 's/hello/world/' file.txt
    
    37. No lines are printed by default due to the -n option
  • The
    sed -i 's/hello/world/' file.txt
    
    939 removes the first line from the pattern space [up until the first newline], readying it for the next cycle

See the GNU

sed -i 's/hello/world/' file.txt
0464 manual for an alternative solution using
sed -i 's/hello/world/' file.txt
0465 and
sed -i 's/hello/world/' file.txt
0466 at https. //gnu. org/s/coreutils/manual/html_node/Squeezing-and-deleting. html

7. 8 Line length adjustment

This section uses

sed -i 's/hello/world/' file.txt
71 and
sed -i 's/hello/world/' file.txt
939 commands to search for consecutive words spanning multiple lines, and the
sed -i 's/hello/world/' file.txt
982 command for branching. See Multiline techniques and Branching and flow control

This [somewhat contrived] example deal with formatting and wrapping lines of text of the following input file

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

The following sed program wraps lines at 40 characters

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

The wrapped output

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

7. 9 Reverse Lines of Files

This one begins a series of totally useless [yet interesting] scripts emulating various Unix commands. This, in particular, is a

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

Note that on implementations other than GNU

sed -i 's/hello/world/' file.txt
22 this script might easily overflow internal buffers

sed -i 's/hello/world/' file.txt
057
sed -i 's/hello/world/' file.txt
058
sed -i 's/hello/world/' file.txt
059
sed -i 's/hello/world/' file.txt
060

7. 10 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 mục đích tương tự 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. In this case we have to add possibly large numbers, so implementing this by successive increments would not be feasible [and possibly even more complicated to contrive than this script]

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.

Như thường lệ, tổng số chạy được giữ trong không gian giữ

Ở dòng cuối cùng, chúng tôi chuyển đổi dạng bàn tính trở lại dạng thập phân. Để đ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]

Again, the common parts are not commented to show the importance of commenting

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

sed -i 's/hello/world/' file.txt
062
sed -i 's/hello/world/' file.txt
082
sed -i 's/hello/world/' file.txt
083
sed -i 's/hello/world/' file.txt
084

7. 14 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í. Nhìn

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

Tập lệnh này tương tự như tập lệnh

sed -i 's/hello/world/' file.txt
0470 ở chỗ nó giữ đầu ra cuối cùng trong không gian giữ và in nó ở cuối

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. after we have kicked in the appropriate number of lines, however, we stop using the hold space to keep inter-line state, and instead use

sed -i 's/hello/world/' file.txt
71 and
sed -i 's/hello/world/' file.txt
939 to slide pattern space by one line

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

Note how the first, second and fourth line are inactive after the first ten lines of input. After that, all the script does is. exiting on the last line of input, appending the next input line to pattern space, and removing the first line

7. 17. 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. Kỹ thuật này thường được sử dụng trong các tập lệnh
sed -i 's/hello/world/' file.txt
22 nâng cao

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 Bóp dòng trống

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. //lists. 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]. Lưu ý những điều này không được duy trì bởi các nhà phát triển GNU
sed -i 's/hello/world/' file.txt
22

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

Nếu GNU

sed -i 's/hello/world/' file.txt
22 không định cấu hình gói yêu thích của bạn, hãy dành thêm vài phút để xác định vấn đề cụ thể và tạo một trường hợp thử nghiệm độc lập. 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 in không gian mẫu trước khi thoát, tất nhiên trừ khi công tắc lệnh
sed -i 's/hello/world/' file.txt
45 đã được chỉ định. 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

Ví dụ, hành vi của

sẽ phụ thuộc vào việc foo có số dòng chẵn hay lẻ12. Hoặc, khi viết một tập lệnh để đọc một vài dòng tiếp theo sau khi khớp mẫu, việc triển khai truyền thống của

sed -i 's/hello/world/' file.txt
22 sẽ buộc bạn phải viết một cái gì đó như

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

Thay vì chỉ

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

Trong mọi trường hợp, cách giải quyết đơn giản nhất là sử dụng

sed -i 's/hello/world/' file.txt
0024 trong các tập lệnh dựa trên hành vi truyền thống hoặc đặt biến
sed -i 's/hello/world/' file.txt
72 thành một giá trị không trống

Xung đột cú pháp Regex [các vấn đề với dấu gạch chéo ngược]

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. According to the standard, the meaning of some escape sequences is undefined in this syntax; notable in the case of
sed -i 's/hello/world/' file.txt
22 are
sed -i 's/hello/world/' file.txt
911,
sed -i 's/hello/world/' file.txt
923,
sed -i 's/hello/world/' file.txt
925,
sed -i 's/hello/world/' file.txt
156,
sed -i 's/hello/world/' file.txt
157,
sed -i 's/hello/world/' file.txt
991,
sed -i 's/hello/world/' file.txt
992,
sed -i 's/hello/world/' file.txt
987,
sed -i 's/hello/world/' file.txt
988,
sed -i 's/hello/world/' file.txt
985, and
sed -i 's/hello/world/' file.txt
986

As in all GNU programs that use POSIX basic regular expressions,

sed -i 's/hello/world/' file.txt
22 interprets these escape sequences as special characters. Vì vậy,
sed -i 's/hello/world/' file.txt
0040 khớp với một hoặc nhiều lần xuất hiện của '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. Một số chương trình
sed -i 's/hello/world/' file.txt
22 đã được viết với giả định rằng
sed -i 's/hello/world/' file.txt
911 và
sed -i 's/hello/world/' file.txt
923 khớp với các ký tự chữ
sed -i 's/hello/world/' file.txt
0046 và
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, các phiên bản mới hơn hiểu điều này là loại bỏ chuỗi
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. Ví dụ: các
sed -i 's/hello/world/' file.txt
22 cũ hơn yêu cầu trình so khớp regex phân tích cú pháp
sed -i 's/hello/world/' file.txt
0055 thành
sed -i 's/hello/world/' file.txt
994 trong trường hợp phổ biến là thoát khỏi dấu gạch chéo, đây lại là hành vi không xác định;

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’ will not ever open for writing a file that is already on disk. 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

Bạn đang gặp sự cố với ngôn ngữ. POSIX bắt buộc rằng

sed -i 's/hello/world/' file.txt
0072 sử dụng thứ tự đối chiếu của ngôn ngữ hiện tại - theo cách nói của C, điều đó có nghĩa là sử dụng
sed -i 's/hello/world/' file.txt
0074 thay vì
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 mandates that such sequences are not matched by ‘. ’, do đó ‘s/. *//' sẽ không xóa không gian mẫu như bạn mong đợi. Trên thực tế, không có cách nào để xóa bộ đệm của sed ở giữa tập lệnh ở hầu hết các ngôn ngữ nhiều byte [bao gồm cả ngôn ngữ UTF-8]. 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. để đảm bảo cho mọi người quyền tự do hiệu quả để sao chép và phân phối lại nó, có hoặc không sửa đổi nó, vì mục đích thương mại hoặc phi thương mại. Thứ hai, Giấy phép này bảo vệ tác giả và nhà xuất bản một cách để nhận tín dụng cho tác phẩm của họ, trong khi không bị coi là chịu trách nhiệm đối với các sửa đổi do người khác thực hiện

    Giấy phép này là một loại “copyleft”, có nghĩa là bản thân các sản phẩm phái sinh của tài liệu phải tự do theo nghĩa tương tự. 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

    Giấy phép này áp dụng cho bất kỳ thủ công hoặc tác phẩm nào khác, trong bất kỳ phương tiện nào, có chứa thông báo do người giữ bản quyền đặt cho biết rằng nó có thể được phân phối theo các điều khoản của Giấy phép này. 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”. Bạn chấp nhận giấy phép nếu bạn sao chép, sửa đổi hoặc phân phối tác phẩm theo cách cần có sự cho phép theo luật bản quyền

    “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. ] Mối quan hệ có thể là vấn đề liên quan đến lịch sử với chủ đề hoặc với các vấn đề liên quan, hoặc vị trí pháp lý, thương mại, triết học, đạo đức hoặc chính trị liên quan đến chúng

    “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. Nếu một phần không phù hợp với định nghĩa ở trên về Thứ yếu thì nó không được phép chỉ định là Bất biến. 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. A copy that is not “Transparent” is called “Opaque”

    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

    Tài liệu có thể bao gồm Tuyên bố miễn trừ trách nhiệm bảo hành bên cạnh thông báo nêu rõ rằng Giấy phép này áp dụng cho Tài liệu. 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. COPYING IN QUANTITY

    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. Bìa trước phải trình bày tiêu đề đầy đủ với tất cả các từ của tiêu đề nổi bật và dễ nhìn như nhau. Bạn có thể thêm các tài liệu khác trên bìa ngoài. 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 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. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices
    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

    You may add a section Entitled “Endorsements”, provided it contains nothing but endorsements of your Modified Version by various parties—for example, statements of peer review or that the text has been approved by an organization as the authoritative definition of a standard

    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 đưa vào 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

    If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document is less than one half of the entire aggregate, the Document’s Cover Texts may be placed on covers that bracket the Document within the aggregate, or the electronic equivalent of covers if the Document is in electronic form. 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 bất biến 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 bất biến ngoài các phiên bản gốc của các Phần bất biến 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 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 tầm thường 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ủ Đề