Thay thế chuỗi rỗng python

Quyền được cấp để sao chép, phân phối và/hoặc sửa đổi tài liệu này theo các điều khoản của Giấy phép Tài liệu Tự do GNU, Phiên bản 1. 3 hoặc bất kỳ phiên bản nào mới hơn do Tổ chức Phần mềm Tự do xuất bản; . Một bản sao của giấy phép được bao gồm trong phần có tiêu đề “Giấy phép Tài liệu Miễn phí GNU”

1. Giới thiệu

sed -i 's/hello/world/' file.txt 22 là trình chỉnh sửa luồng. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as sed -i 's/hello/world/' file.txt 24), sed -i 's/hello/world/' file.txt 22 works by making only one pass over the input(s), and is consequently more efficient. But it is sed -i 's/hello/world/' file.txt 22’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors

2 Running sed

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

2. 1 Overview

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

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

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

If you do not specify INPUTFILE, or if INPUTFILE is -, sed -i 's/hello/world/' file.txt 22 filters the contents of the standard input. The following commands are equivalent

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt

sed -i 's/hello/world/' file.txt 22 writes output to standard output. Use -i to edit files in-place instead of printing to standard output. See also the sed -i 's/hello/world/' file.txt 33 and sed -i 's/hello/world/' file.txt 34 commands for writing output to other files. The following command modifies file. txt and does not produce any output

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

By default sed -i 's/hello/world/' file.txt 22 prints all processed input (except input that has been modified/deleted by commands such as sed -i 's/hello/world/' file.txt 36). Use -n to suppress output, and the sed -i 's/hello/world/' file.txt 37 command to print specific lines. The following command prints only line 45 of the input file

sed -i 's/hello/world/' file.txt 22 treats multiple input files as one long stream. The following example prints the first line of the first file (one. txt) and the last line of the last file (three. txt). Use -s to reverse this behavior

sed -n '1p ; $p' one.txt two.txt three.txt

Without -e or -f options, sed -i 's/hello/world/' file.txt 22 uses the first non-option parameter as the script, and the following non-option parameters as input files. If -e or -f options are used to specify a script, all non-option parameters are taken as input files. Options -e and -f can be combined, and can appear multiple times (in which case the final effective script will be concatenation of all the individual scripts)

The following examples are equivalent

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt

2. 2 Command-Line Options

The full format for invoking sed -i 's/hello/world/' file.txt 22 is

sed OPTIONS.. [SCRIPT] [INPUTFILE...]

sed -i 's/hello/world/' file.txt 22 may be invoked with the following command-line options

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

Print out the version of sed -i 's/hello/world/' file.txt 22 that is being run and a copyright notice, then exit

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

Print a usage message briefly summarizing these command-line options and the bug-reporting address, then exit

sed -i 's/hello/world/' file.txt 45sed -i 's/hello/world/' file.txt 46sed -i 's/hello/world/' file.txt 47

By default, sed -i 's/hello/world/' file.txt 22 prints out the pattern space at the end of each cycle through the script (see ). These options disable this automatic printing, and sed -i 's/hello/world/' file.txt 22 only produces output when explicitly told to via the sed -i 's/hello/world/' file.txt 37 command

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

Print the input sed program in canonical form, and annotate program execution

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3

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

Add the commands in script to the set of commands to be run while processing the input

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

Add the commands contained in the file script-file to the set of commands to be run while processing the input

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

This option specifies that files are to be edited in-place. GNU sed -i 's/hello/world/' file.txt 22 does this by creating a temporary file and sending output to this file rather than to the standard output

This option implies -s

When the end of the file is reached, the temporary file is renamed to the output file’s original name. The extension, if supplied, is used to modify the name of the old file before renaming the temporary file, thereby making a backup copy)

This rule is followed. if the extension doesn’t contain a sed -i 's/hello/world/' file.txt 60, then it is appended to the end of the current filename as a suffix; if the extension does contain one or more sed -i 's/hello/world/' file.txt 60 characters, then each asterisk is replaced with the current filename. This allows you to add a prefix to the backup file, instead of (or in addition to) a suffix, or even to place backup copies of the original files into another directory (provided the directory already exists)

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

Same as -E -i with no backup suffix - FILE will be edited in-place without creating a backup

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. the former disables automatic printing of lines and the latter changes the file in-place without a backup. Used carelessly (and without an explicit sed -i 's/hello/world/' file.txt 37 command), the output file will be empty

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE

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

Specify the default line-wrap length for the sed -i 's/hello/world/' file.txt 67 command. A length of 0 (zero) means to never wrap long lines. If not specified, it is taken to be 70

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

GNU sed -i 's/hello/world/' file.txt 22 includes several extensions to POSIX sed. In order to simplify writing portable scripts, this option disables all the extensions that this manual documents, including additional commands. Most of the extensions accept sed -i 's/hello/world/' file.txt 22 programs that are outside the syntax mandated by POSIX, but some of them (such as the behavior of the sed -i 's/hello/world/' file.txt 71 command described in ) actually violate the standard. 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 73sed -i 's/hello/world/' file.txt 74

This option is available on every platform, but is only effective where the operating system makes a distinction between text files and binary files. 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. When this option is specified, sed -i 's/hello/world/' file.txt 22 will open input files in binary mode, thus not requesting this special processing and considering lines to end at a line feed

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

This option is available only on platforms that support symbolic links and has an effect only if option -i is specified. In this case, if the file that is specified on the command line is a symbolic link, sed -i 's/hello/world/' file.txt 22 will follow the link and edit the ultimate destination of the link. The default behavior is to break the symbolic link, so that the link destination will not be modified

sed -i 's/hello/world/' file.txt 79sed -i 's/hello/world/' file.txt 80sed -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. Historically this was a GNU extension, but the -E extension has since been added to the POSIX standard (http. //austingroupbugs. lượt xem trên mạng. php?id=528), so use -E for portability. GNU sed has accepted -E as an undocumented option for years, and *BSD seds have accepted -E for years as well, but scripts that use -E might not port to other older systems. Thấy

sed -i 's/hello/world/' file.txt 83sed -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. This GNU sed -i 's/hello/world/' file.txt 22 extension allows the user to consider them as separate files. 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 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, sed -i 's/hello/world/' file.txt 90 lệnh 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______0_______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. )

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

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

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

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

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

Trạng thái thoát bằng 0 cho biết thành công và giá trị khác không cho biết thất bại. GNU sed -i 's/hello/world/' file.txt 22 trả về các giá trị lỗi trạng thái thoát sau

0

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

1

Lệnh không hợp lệ, cú pháp không hợp lệ, biểu thức chính quy không hợp lệ hoặc lệnh mở rộng GNU sed -i 's/hello/world/' file.txt 22 được sử dụng với --posix

2

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

4

Lỗi I/O hoặc lỗi xử lý nghiêm trọng trong thời gian chạy, GNU sed -i 's/hello/world/' file.txt 22 bị hủy bỏ ngay lập tức

Ngoài ra, các lệnh sed -n '1p ; $p' one.txt two.txt three.txt 00 và sed -n '1p ; $p' one.txt two.txt three.txt 01 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)

$ echo | sed 'Q42' ; echo $? 42

3 sed -i 's/hello/world/' file.txt 22 kịch bản

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

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 “chữ viết” sed -i 's/hello/world/' file.txt 22; . Thấy

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 -n '1p ; $p' one.txt two.txt three.txt 11 là một địa chỉ dòng tùy chọn. Nếu sed -n '1p ; $p' one.txt two.txt three.txt 11 được chỉ định, lệnh X sẽ chỉ được thực hiện trên các dòng phù hợp. sed -n '1p ; $p' one.txt two.txt three.txt 11 có thể là số dòng đơn, biểu thức chính quy hoặc dải dòng (xem phần ). sed -n '1p ; $p' one.txt two.txt three.txt 14 bổ sung được sử dụng cho một số lệnh sed -i 's/hello/world/' file.txt 22

Ví dụ sau xóa dòng 30 đến 35 trong đầu vào. sed -n '1p ; $p' one.txt two.txt three.txt 16 là một dải địa chỉ. sed -i 's/hello/world/' file.txt 36 là lệnh xóa

sed '30,35d' input.txt > output.txt

Ví dụ sau in tất cả đầu vào cho đến khi tìm thấy một dòng bắt đầu bằng từ 'foo'. Nếu dòng đó được tìm thấy, sed -i 's/hello/world/' file.txt 22 sẽ chấm dứt với trạng thái thoát 42. Nếu dòng đó không được tìm thấy (và không có lỗi nào khác xảy ra), sed -i 's/hello/world/' file.txt 22 sẽ thoát với trạng thái 0. sed -n '1p ; $p' one.txt two.txt three.txt 20 là một địa chỉ biểu thức chính quy. sed -n '1p ; $p' one.txt two.txt three.txt 00 là lệnh thoát. sed -n '1p ; $p' one.txt two.txt three.txt 22 là tùy chọn lệnh

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 0

Các lệnh trong tập lệnh hoặc tệp tập lệnh có thể được phân tách bằng dấu chấm phẩy (sed -n '1p ; $p' one.txt two.txt three.txt 23) hoặc dòng mới (ASCII 10). Nhiều tập lệnh có thể được chỉ định với các tùy chọn -e hoặc -f

Các ví dụ sau đây đều tương đương. Họ thực hiện hai hoạt động sed -i 's/hello/world/' file.txt 22. xóa bất kỳ dòng nào khớp với biểu thức chính quy sed -n '1p ; $p' one.txt two.txt three.txt 20 và thay thế tất cả các lần xuất hiện của chuỗi 'hello' bằng 'world'

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 1

Lệnh sed -n '1p ; $p' one.txt two.txt three.txt 26, sed -n '1p ; $p' one.txt two.txt three.txt 27, sed -n '1p ; $p' one.txt two.txt three.txt 28, do cú pháp của chúng, không thể theo sau dấu chấm phẩy hoạt động như dấu phân cách lệnh và do đó nên được kết thúc bằng dòng mới hoặc được đặt ở cuối tập lệnh hoặc tệp tập lệnh. Các lệnh cũng có thể được đặt trước các ký tự khoảng trắng không quan trọng tùy chọn. Thấy

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

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

sed -n '1p ; $p' one.txt two.txt three.txt 31sed -n '1p ; $p' one.txt two.txt three.txt 32

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

sed -n '1p ; $p' one.txt two.txt three.txt 33

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

sed -n '1p ; $p' one.txt two.txt three.txt 34

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 -n '1p ; $p' one.txt two.txt three.txt 35sed -n '1p ; $p' one.txt two.txt three.txt 32

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

sed -n '1p ; $p' one.txt two.txt three.txt 37

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

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

Xóa không gian mẫu;

sed -n '1p ; $p' one.txt two.txt three.txt 39

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

Nếu không gian mẫu không chứa dòng mới, hãy bắt đầu một chu kỳ mới bình thường như thể lệnh sed -i 's/hello/world/' file.txt 36 đã được ban hành

sed -n '1p ; $p' one.txt two.txt three.txt 41

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

sed -n '1p ; $p' one.txt two.txt three.txt 42

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

sed -n '1p ; $p' one.txt two.txt three.txt 43

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

sed -n '1p ; $p' one.txt two.txt three.txt 44

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

sed -n '1p ; $p' one.txt two.txt three.txt 45

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

sed -n '1p ; $p' one.txt two.txt three.txt 46

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

sed -n '1p ; $p' one.txt two.txt three.txt 47

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

sed -n '1p ; $p' one.txt two.txt three.txt 48sed -n '1p ; $p' one.txt two.txt three.txt 32

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

sed -n '1p ; $p' one.txt two.txt three.txt 50

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

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

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

sed -n '1p ; $p' one.txt two.txt three.txt 52

(tiếp theo) Nếu tính năng tự động in không bị tắt, hãy in không gian mẫu, sau đó, bất kể, thay thế không gian mẫu bằng dòng nhập tiếp theo. Nếu không có thêm thông tin đầu vào nào thì sed -i 's/hello/world/' file.txt 22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào

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

Thêm một dòng mới vào không gian mẫu, sau đó nối dòng đầu vào tiếp theo vào không gian mẫu. Nếu không có thêm thông tin đầu vào nào thì sed -i 's/hello/world/' file.txt 22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào

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

In không gian mẫu

sed -n '1p ; $p' one.txt two.txt three.txt 57

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

sed -n '1p ; $p' one.txt two.txt three.txt 58

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

sed -n '1p ; $p' one.txt two.txt three.txt 60

(thoát) Lệnh này giống như lệnh sed -n '1p ; $p' one.txt two.txt three.txt 00, nhưng sẽ không in nội dung của không gian mẫu. Giống như sed -n '1p ; $p' one.txt two.txt three.txt 00, nó cung cấp khả năng trả lại mã thoát cho người gọi

sed -n '1p ; $p' one.txt two.txt three.txt 63

Đọc tên tập tin

sed -n '1p ; $p' one.txt two.txt three.txt 64

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

sed -n '1p ; $p' one.txt two.txt three.txt 65

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

sed -n '1p ; $p' one.txt two.txt three.txt 66

(thử nghiệm) Nhánh để gắn nhãn chỉ khi đã có một sự thay thế thành công sed -n '1p ; $p' one.txt two.txt three.txt 67 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 -n '1p ; $p' one.txt two.txt three.txt 68

(thử nghiệm) Chỉ rẽ nhánh để gắn nhãn nếu không có thay thế sed -n '1p ; $p' one.txt two.txt three.txt 67 nào thành công kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu

sed -n '1p ; $p' one.txt two.txt three.txt 70

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

sed -n '1p ; $p' one.txt two.txt three.txt 73

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

sed -n '1p ; $p' one.txt two.txt three.txt 74

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

sed -n '1p ; $p' one.txt two.txt three.txt 75

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

sed -n '1p ; $p' one.txt two.txt three.txt 76

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

sed -n '1p ; $p' one.txt two.txt three.txt 77

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

sed -n '1p ; $p' one.txt two.txt three.txt 78

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

sed -n '1p ; $p' one.txt two.txt three.txt 79

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

sed -n '1p ; $p' one.txt two.txt three.txt 80

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

sed -n '1p ; $p' one.txt two.txt three.txt 81

Chỉ định vị trí của nhãn cho các lệnh rẽ nhánh (_______19_______82, sed -n '1p ; $p' one.txt two.txt three.txt 83, sed -n '1p ; $p' one.txt two.txt three.txt 84)

3. 3 Lệnh sed -n '1p ; $p' one.txt two.txt three.txt 67

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

Khái niệm cơ bản của nó là đơn giản. lệnh sed -n '1p ; $p' one.txt two.txt three.txt 67 cố gắng khớp không gian mẫu với biểu thức chính quy được cung cấp;

Để biết chi tiết về cú pháp regrec xem

Phần thay thế có thể chứa các tham chiếu sed -n '1p ; $p' one.txt two.txt three.txt 90 (n là một số từ 1 đến 9), tham chiếu tới phần trùng khớp được chứa giữa sed -n '1p ; $p' one.txt two.txt three.txt 91 thứ n và kết quả trùng khớp của nó sed -n '1p ; $p' one.txt two.txt three.txt 92. Ngoài ra, phần thay thế có thể chứa _______19_______93 ký tự chưa thoát tham chiếu toàn bộ phần khớp của không gian mẫu

Các ký tự sed -n '1p ; $p' one.txt two.txt three.txt 94 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 -n '1p ; $p' one.txt two.txt three.txt 67 nào. Ký tự sed -n '1p ; $p' one.txt two.txt three.txt 94 (hoặc bất kỳ ký tự nào khác được sử dụng thay cho nó) chỉ có thể xuất hiện trong biểu thức chính quy hoặc thay thế nếu nó đứng trước ký tự sed -n '1p ; $p' one.txt two.txt three.txt 97

Cuối cùng, với tư cách là tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt 22, bạn có thể bao gồm một chuỗi đặc biệt được tạo thành từ dấu gạch chéo ngược và một trong các chữ cái sed -n '1p ; $p' one.txt two.txt three.txt 99, sed -i 's/hello/world/' file.txt 67, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 01, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 02 hoặc sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 03. Ý nghĩa như sau

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 04

Chuyển từ thay thế sang chữ thường cho đến khi tìm thấy sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 05 hoặc sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 06,

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 07

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

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 05

Chuyển từ thay thế sang chữ hoa cho đến khi tìm thấy sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 04 hoặc sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 06,

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 11

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

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 06

Dừng chuyển đổi trường hợp bắt đầu bởi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 04 hoặc sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 05

Khi cờ sed -n '1p ; $p' one.txt two.txt three.txt 44 đang được sử dụng, chuyển đổi kiểu chữ không lan truyền từ lần xuất hiện này sang lần xuất hiện khác của biểu thức chính quy. 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 -n '1p ; $p' one.txt two.txt three.txt 75 được thêm vào không gian mẫu khi thay thế sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 17 bằng sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 18

Mặt khác, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 07 và sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 11 ả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 từ thay thế trống. Với 'a-b-' trong không gian mẫu, lệnh sau

sẽ thay thế '-' bằng 'X' (chữ hoa) và 'b-' bằng 'Bx'. Nếu hành vi này là không mong muốn, bạn có thể ngăn chặn nó bằng cách thêm một chuỗi '\E'—sau '\1' trong trường hợp này

Để bao gồm một ký tự sed -n '1p ; $p' one.txt two.txt three.txt 97, sed -n '1p ; $p' one.txt two.txt three.txt 93 hoặc dòng mới trong thay thế cuối cùng, hãy đảm bảo đặt trước sed -n '1p ; $p' one.txt two.txt three.txt 97 mong muốn, sed -n '1p ; $p' one.txt two.txt three.txt 93 hoặc dòng mới trong thay thế bằng một ____19_______97

Lệnh sed -n '1p ; $p' one.txt two.txt three.txt 67 có thể được theo sau bởi 0 hoặc nhiều cờ sau

sed -n '1p ; $p' one.txt two.txt three.txt 44

Á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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 28

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

tương tác trong lệnh sed -n '1p ; $p' one.txt two.txt three.txt 67 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 -n '1p ; $p' one.txt two.txt three.txt 44 và hiện tại không có ý nghĩa thống nhất rộng rãi trong việc triển khai sed -i 's/hello/world/' file.txt 22. Đối với GNU sed -i 's/hello/world/' file.txt 22, tương tác được định nghĩa 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 -n '1p ; $p' one.txt two.txt three.txt 41 đượ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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 36 (đá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 một cách đặ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 -n '1p ; $p' one.txt two.txt three.txt 41, 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 -n '1p ; $p' one.txt two.txt three.txt 67 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 -n '1p ; $p' one.txt two.txt three.txt 73

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

sed -n '1p ; $p' one.txt two.txt three.txt 41

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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 45_______19_______28

Công cụ sửa đổi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 45 cho đối sánh biểu thức chính quy là một tiện ích mở rộng GNU làm cho _________22 đối sánh biểu thức chính quy theo cách không phân biệt chữ hoa chữ thường

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 49sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 50

Công cụ sửa đổi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 49 để 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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 54 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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 56 và sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 57) 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 -n '1p ; $p' one.txt two.txt three.txt 78

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

Ký tự sed -n '1p ; $p' one.txt two.txt three.txt 78 bắt đầu nhận xét;

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 -n '1p ; $p' one.txt two.txt three.txt 78

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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 64, 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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 65

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

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

Lệnh này chỉ chấp nhận một địa chỉ. Lưu ý rằng không gian mẫu hiện tại được in nếu tính năng in tự động không bị tắt với tùy chọn -n. Khả năng trả lại mã thoát từ tập lệnh sed -i 's/hello/world/' file.txt 22 là một tiện ích 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 -n '1p ; $p' one.txt two.txt three.txt 01 thoát âm thầm mà không in không gian mẫu hiện tại

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

Xóa không gian mẫu;

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

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

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

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

sed -n '1p ; $p' one.txt two.txt three.txt 52

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 thông tin đầu vào nào thì sed -i 's/hello/world/' file.txt 22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào

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

Thí dụ. thực hiện thay thế trên mỗi dòng thứ 3 (i. e. hai lệnh sed -n '1p ; $p' one.txt two.txt three.txt 52 bỏ qua hai dòng)

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 2

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 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 3

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 77

Một nhóm lệnh có thể được đặt trong khoảng từ sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 78 đến sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 79 ký tự. Điều này đặc biệt hữu ích khi bạn muốn một nhóm lệnh được kích hoạt bởi một địa chỉ (hoặc dải địa chỉ) phù hợp

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

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 4

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 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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 81

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

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

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 5

(Các ký tự sed -n '1p ; $p' one.txt two.txt three.txt 94 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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 83 nào. )

Các trường hợp của sed -n '1p ; $p' one.txt two.txt three.txt 94 (hoặc bất kỳ ký tự nào khác được sử dụng thay cho nó), sed -n '1p ; $p' one.txt two.txt three.txt 97 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 ____19_______97. 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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 87 từ GNU coreutils để biết chức năng tương tự

sed -n '1p ; $p' one.txt two.txt three.txt 33

Nối văn bản sau một dòng. Đây là phần mở rộng GNU cho lệnh sed -n '1p ; $p' one.txt two.txt three.txt 26 tiêu chuẩn - xem bên dưới để biết chi tiết

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

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 6

Khoảng trắng phía trước sau lệnh sed -n '1p ; $p' one.txt two.txt three.txt 26 bị bỏ qua. Văn bản cần thêm được đọc cho đến cuối dòng

sed -n '1p ; $p' one.txt two.txt three.txt 31sed -n '1p ; $p' one.txt two.txt three.txt 32

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

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

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 7

Lệnh sed -n '1p ; $p' one.txt two.txt three.txt 26 xếp hàng các dòng văn bản tuân theo lệnh này (mỗi dòng trừ kết thúc cuối cùng bằng ____19_______97, được xóa khỏi đầu ra) để xuất ra ở cuối chu kỳ hiện tại hoặc khi dòng đầu vào tiếp theo được đọc

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

Trình tự thoát trong văn bản được xử lý, vì vậy bạn nên sử dụng sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 95 trong văn bản để in một dấu gạch chéo ngược

Các lệnh tiếp tục sau dòng cuối cùng mà không có dấu gạch chéo ngược (sed -n '1p ; $p' one.txt two.txt three.txt 97) - ‘world’ trong ví dụ sau

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 8

Là một phần mở rộng của GNU, lệnh và văn bản sed -n '1p ; $p' one.txt two.txt three.txt 26 có thể được tách thành hai tham số sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 98, cho phép tạo tập lệnh dễ dàng hơn

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 9

sed -n '1p ; $p' one.txt two.txt three.txt 50

chèn văn bản trước một dòng. Đây là phần mở rộng GNU cho lệnh sed -n '1p ; $p' one.txt two.txt three.txt 28 tiêu chuẩn - xem bên dưới để biết chi tiết

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

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

Khoảng trắng phía trước sau lệnh sed -n '1p ; $p' one.txt two.txt three.txt 28 bị bỏ qua. Văn bản cần thêm được đọc cho đến cuối dòng

sed -n '1p ; $p' one.txt two.txt three.txt 48sed -n '1p ; $p' one.txt two.txt three.txt 32

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

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

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

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

Trình tự thoát trong văn bản được xử lý, vì vậy bạn nên sử dụng sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 95 trong văn bản để in một dấu gạch chéo ngược

Các lệnh tiếp tục sau dòng cuối cùng mà không có dấu gạch chéo ngược (sed -n '1p ; $p' one.txt two.txt three.txt 97) - ‘world’ trong ví dụ sau

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

Là một phần mở rộng của GNU, lệnh và văn bản sed -n '1p ; $p' one.txt two.txt three.txt 28 có thể được tách thành hai tham số sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 98, cho phép tạo tập lệnh dễ dàng hơn

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

sed -n '1p ; $p' one.txt two.txt three.txt 37

Thay thế (các) dòng bằng văn bản. Đây là phần mở rộng GNU cho lệnh sed -n '1p ; $p' one.txt two.txt three.txt 27 tiêu chuẩn - xem bên dưới để biết chi tiết

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

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

Khoảng trắng phía trước sau lệnh sed -n '1p ; $p' one.txt two.txt three.txt 27 bị bỏ qua. Văn bản cần thêm được đọc cho đến cuối dòng

sed -n '1p ; $p' one.txt two.txt three.txt 35sed -n '1p ; $p' one.txt two.txt three.txt 32

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

Thí dụ. Thay thế dòng thứ 2 đến thứ 4 bằng các từ 'xin chào' và 'thế giới' (-. cho biết các dòng đầu ra được in)

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

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

Một chu kỳ mới được bắt đầu sau khi lệnh này được thực hiện, vì không gian mẫu sẽ bị xóa. Trong ví dụ sau, sed -n '1p ; $p' one.txt two.txt three.txt 27 bắt đầu một chu kỳ mới và lệnh thay thế không được thực hiện trên văn bản đã thay thế

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

Là một phần mở rộng của GNU, lệnh và văn bản sed -n '1p ; $p' one.txt two.txt three.txt 27 có thể được tách thành hai tham số sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 98, cho phép tạo tập lệnh dễ dàng hơn

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

sed -n '1p ; $p' one.txt two.txt three.txt 80

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

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

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

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 17

In không gian mẫu ở dạng rõ ràng. các ký tự không in được (và ký tự sed -n '1p ; $p' one.txt two.txt three.txt 97) được in ở dạng thoát kiểu C;

n chỉ định độ dài ngắt dòng mong muốn; . Nếu bỏ qua, giá trị mặc định như được chỉ định trên dòng lệnh sẽ được sử dụng. Tham số n là phần mở rộng GNU sed -i 's/hello/world/' file.txt 22

sed -n '1p ; $p' one.txt two.txt three.txt 63

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

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

Xếp hàng nội dung của tên tệp sẽ được đọc và chèn vào luồng đầu ra ở cuối chu kỳ hiện tại hoặc khi dòng đầu vào tiếp theo được đọc. Lưu ý rằng nếu tên tệp không thể đọc được, nó sẽ được coi như một tệp trống, không có bất kỳ dấu hiệu lỗi nào

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

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

sed -n '1p ; $p' one.txt two.txt three.txt 73

Viết không gian mẫu vào tên tệp. Là một tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt 22, hai giá trị đặc biệt của tên tệp được hỗ trợ. /dev/stderr, ghi kết quả vào lỗi tiêu chuẩn và /dev/stdout, ghi vào đầu ra tiêu chuẩn

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 -n '1p ; $p' one.txt two.txt three.txt 39

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

Thêm một dòng mới vào không gian mẫu, sau đó nối dòng đầu vào tiếp theo vào không gian mẫu. Nếu không có thêm thông tin đầu vào nà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

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. Thấy

sed -n '1p ; $p' one.txt two.txt three.txt 57

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

sed -n '1p ; $p' one.txt two.txt three.txt 46

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

sed -n '1p ; $p' one.txt two.txt three.txt 47

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

sed -n '1p ; $p' one.txt two.txt three.txt 44

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

sed -n '1p ; $p' one.txt two.txt three.txt 45

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

sed -n '1p ; $p' one.txt two.txt three.txt 75

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

3. 6 mệnh lệnh cho sed -i 's/hello/world/' file.txt 22 bậc thầy

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 trong những thứ như sed OPTIONS.. [SCRIPT] [INPUTFILE...] 41 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 -n '1p ; $p' one.txt two.txt three.txt 81

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

Chỉ định vị trí của nhãn cho các lệnh rẽ nhánh. Trong tất cả các khía cạnh khác, một no-op

sed -n '1p ; $p' one.txt two.txt three.txt 34

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 -n '1p ; $p' one.txt two.txt three.txt 66

Chỉ nhánh để gắn nhãn nếu đã có một sự thay thế sed -n '1p ; $p' one.txt two.txt three.txt 67 thành công kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện đã được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu

3. 7 Lệnh dành riêng cho GNU sed -i 's/hello/world/' file.txt 22

Các lệnh này dành riêng cho GNU sed -i 's/hello/world/' file.txt 22, vì vậy bạn phải cẩn thận khi sử dụng chúng và chỉ khi bạn chắc chắn rằng việc cản trở tính di động không phải là điều xấu. Chúng cho phép bạn kiểm tra các tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt 22 hoặc thực hiện các tác vụ được yêu cầu khá thường xuyên, nhưng không được hỗ trợ bởi các sed -i 's/hello/world/' file.txt 22 tiêu chuẩn

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 51

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 -n '1p ; $p' one.txt two.txt three.txt 41 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 -n '1p ; $p' one.txt two.txt three.txt 41 diễn giải nó thành một lệnh và gửi đầu ra của nó đến 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

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

Lưu ý rằng, không giống như lệnh sed OPTIONS.. [SCRIPT] [INPUTFILE...] 54, đầu ra của lệnh sẽ được in ngay lập tức;

sed -n '1p ; $p' one.txt two.txt three.txt 43

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 OPTIONS.. [SCRIPT] [INPUTFILE...] 57

Lệnh này chỉ chấp nhận một địa chỉ

Lệnh này giống như lệnh sed -n '1p ; $p' one.txt two.txt three.txt 00, nhưng sẽ không in nội dung của không gian mẫu. Giống như sed -n '1p ; $p' one.txt two.txt three.txt 00, 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 -n '1p ; $p' one.txt two.txt three.txt 0

sed -n '1p ; $p' one.txt two.txt three.txt 64

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

Như với lệnh sed OPTIONS.. [SCRIPT] [INPUTFILE...] 54, giá trị đặc biệt /dev/stdin được hỗ trợ cho tên tệp, đọc một dòng từ đầu vào tiêu chuẩn

sed -n '1p ; $p' one.txt two.txt three.txt 68

Chỉ phân nhánh để gắn nhãn nếu không có thay thế sed -n '1p ; $p' one.txt two.txt three.txt 67 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 OPTIONS.. [SCRIPT] [INPUTFILE...] 64

Lệnh này không làm gì, nhưng làm cho sed -i 's/hello/world/' file.txt 22 không thành công nếu các tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt 22 không được hỗ trợ, đơn giản vì các phiên bản khác của sed -i 's/hello/world/' file.txt 22 không triển khai lệnh này. Ngoài ra, bạn có thể chỉ định phiên bản của 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 OPTIONS.. [SCRIPT] [INPUTFILE...] 69. Giá trị mặc định là sed OPTIONS.. [SCRIPT] [INPUTFILE...] 70 vì đó là phiên bản đầu tiên triển khai lệnh này

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 -n '1p ; $p' one.txt two.txt three.txt 74

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 theo lệnh sed OPTIONS.. [SCRIPT] [INPUTFILE...] 26 về xử lý tệp cũng được giữ ở đây

sed -n '1p ; $p' one.txt two.txt three.txt 77

Lệnh này làm trống nội dung của không gian mẫu. Nó thường giống như ‘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 yêu cầu các chuỗi như vậy không khớp với '. ’, do đó 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 Cú pháp nhiều lệnh

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

Sử dụng dòng mới là cách tự nhiên nhất khi chạy tập lệnh sed từ tệp (sử dụng tùy chọn -f)

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. Ngoài ra, bạn có thể chỉ định từng lệnh làm đối số cho tùy chọn -e

sed -n '1p ; $p' one.txt two.txt three.txt 1

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 -n '1p ; $p' one.txt two.txt three.txt 2

Các lệnh sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 78,sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 79,sed -n '1p ; $p' one.txt two.txt three.txt 82,_______19_______83,sed -n '1p ; $p' one.txt two.txt three.txt 84,sed OPTIONS.. [SCRIPT] [INPUTFILE...] 83 có thể được phân tách bằng dấu chấm phẩy (đây là tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt 22 không di động)

sed -n '1p ; $p' one.txt two.txt three.txt 3

Các nhãn được sử dụng trong các lệnh sed -n '1p ; $p' one.txt two.txt three.txt 82,sed -n '1p ; $p' one.txt two.txt three.txt 83,sed -n '1p ; $p' one.txt two.txt three.txt 84,sed OPTIONS.. [SCRIPT] [INPUTFILE...] 83 được đọc cho đến khi có dấu chấm phẩy. Khoảng trắng ở đầu và cuối bị bỏ qua. Trong các ví dụ bên dưới nhãn là 'x'. Ví dụ đầu tiên hoạt động với GNU sed -i 's/hello/world/' file.txt 22. Thứ hai là tương đương di động. Để biết thêm thông tin về phân nhánh và nhãn, hãy xem

sed -n '1p ; $p' one.txt two.txt three.txt 4

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 -n '1p ; $p' one.txt two.txt three.txt 26,______19_______27,sed -n '1p ; $p' one.txt two.txt three.txt 28 (nối thêm/thay đổi/chèn)

Tất cả các ký tự theo sau lệnh sed -n '1p ; $p' one.txt two.txt three.txt 26,sed -n '1p ; $p' one.txt two.txt three.txt 27,sed -n '1p ; $p' one.txt two.txt three.txt 28 được lấy làm văn bản để thêm/thay đổi/chèn. Sử dụng dấu chấm phẩy dẫn đến kết quả không mong muốn

sed -n '1p ; $p' one.txt two.txt three.txt 5

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

sed -n '1p ; $p' one.txt two.txt three.txt 6

Lưu ý rằng việc chỉ định văn bản để thêm ('Xin chào') ngay sau sed -n '1p ; $p' one.txt two.txt three.txt 26,sed -n '1p ; $p' one.txt two.txt three.txt 27,sed -n '1p ; $p' one.txt two.txt three.txt 28 chính nó là một tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt 22. Một giải pháp thay thế di động, tuân thủ POSIX là

sed -n '1p ; $p' one.txt two.txt three.txt 7

sed -n '1p ; $p' one.txt two.txt three.txt 78 (bình luận)

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

sed -n '1p ; $p' one.txt two.txt three.txt 8

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 54,______0_______88,sed OPTIONS.. [SCRIPT] [INPUTFILE...] 26,sed -i 's/hello/world/' file.txt 33 (đọc và ghi tệp)

Các lệnh sed OPTIONS.. [SCRIPT] [INPUTFILE...] 54,sed -i 's/hello/world/' file.txt 88,sed OPTIONS.. [SCRIPT] [INPUTFILE...] 26,sed -i 's/hello/world/' file.txt 33 phân tích cú pháp tên tệp cho đến cuối dòng. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào tên tệp, dẫn đến kết quả không mong muốn

sed -n '1p ; $p' one.txt two.txt three.txt 9

Lưu ý rằng sed -i 's/hello/world/' file.txt 22 âm thầm bỏ qua lỗi đọc/ghi trong các lệnh sed OPTIONS.. [SCRIPT] [INPUTFILE...] 54,sed -i 's/hello/world/' file.txt 88,sed OPTIONS.. [SCRIPT] [INPUTFILE...] 26,sed -i 's/hello/world/' file.txt 33 (chẳng hạn như các tệp bị thiếu). Trong ví dụ sau, sed -i 's/hello/world/' file.txt 22 cố gắng đọc một tệp có tên 'hello. văn bản ; . Tệp bị thiếu và lỗi được âm thầm bỏ qua

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 0

sed -n '1p ; $p' one.txt two.txt three.txt 41 (thực thi lệnh)

Bất kỳ ký tự nào theo sau lệnh sed -n '1p ; $p' one.txt two.txt three.txt 41 cho đến cuối dòng sẽ được gửi tới trình bao. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào lệnh trình bao, dẫn đến kết quả không mong muốn

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 1

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 17 (thay thế bằng cờ sed -n '1p ; $p' one.txt two.txt three.txt 41 hoặc sed OPTIONS.. [SCRIPT] [INPUTFILE...] 26)

Trong một lệnh thay thế, cờ sed OPTIONS.. [SCRIPT] [INPUTFILE...] 26 ghi kết quả thay thế vào một tệp và cờ sed -n '1p ; $p' one.txt two.txt three.txt 41 thực thi kết quả thay thế dưới dạng lệnh trình bao. Như với các lệnh $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 22, 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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 2

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

4. 1 Tổng quan về địa chỉ

Các đị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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 3

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

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

Địa chỉ có thể chứa các biểu thức chính quy để khớp các dòng dựa trên nội dung thay vì số dòng. Lệnh sau chỉ thay thế từ 'hello' bằng 'world' trong các dòng có chứa từ 'apple'

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 5

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 ($ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 24). Đị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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 6

Việc thêm ký tự $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 25 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ự $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 25 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. Lệnh sau chỉ thay thế từ 'xin chào' bằng 'thế giới' trong các dòng không chứa từ 'quả táo'

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 7

Lệnh sau chỉ thay thế từ 'hello' bằng 'world' trong các dòng từ 1 đến 3 và 18 cho đến dòng cuối cùng của tệp đầu vào (i. e. trừ dòng 4 đến 17)

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 8

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

Đị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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 28

Chỉ định một số dòng sẽ chỉ khớp với dòng đó trong đầu vào. (Lưu ý rằng sed -i 's/hello/world/' file.txt 22 đếm các dòng liên tục trên tất cả các tệp đầu vào trừ khi các tùy chọn -i hoặc -s được chỉ định. )

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

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

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 31

Tiện ích mở rộng GNU này khớp với mọi dòng bước bắt đầu bằng dòng đầu tiên. Cụ thể, các dòng sẽ được chọn khi tồn tại một n không âm sao cho số dòng hiện tại bằng đầu tiên + (n * bước). Do đó, người ta sẽ sử dụng $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 32 để chọn các dòng đánh số lẻ và $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 33 cho các dòng đánh số chẵn;

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

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 9

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à. Nếu các tùy chọn -E hoặc -r được sử dụng, Biểu thức chính quy phải theo cú pháp. Thấy

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 36

Đ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 chính biểu thức chính quy bao gồm bất kỳ ký tự sed -n '1p ; $p' one.txt two.txt three.txt 94 nào, thì mỗi ký tự phải được thoát bằng dấu gạch chéo ngược (sed -n '1p ; $p' one.txt two.txt three.txt 97)

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

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 0

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 -n '1p ; $p' one.txt two.txt three.txt 67). Lưu ý rằng các công cụ sửa đổi cho biểu thức chính quy được đánh giá khi biểu thức chính quy được biên dịch, do đó, việc chỉ định chúng cùng với biểu thức chính quy trống là không hợp lệ

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 40

(Có thể thay thế $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 41 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 một dấu phân cách khác với sed -n '1p ; $p' one.txt two.txt three.txt 94. Điều này đặc biệt hữu ích nếu bản thân biểu thức chính quy chứa nhiều dấu gạch chéo, vì nó tránh được việc thoát tẻ nhạt của mỗi sed -n '1p ; $p' one.txt two.txt three.txt 94. 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 -n '1p ; $p' one.txt two.txt three.txt 97)

Các lệnh sau đây là tương đương. Họ in các dòng bắt đầu bằng '/home/alice/documents/'

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 1

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 45_______35_______46

Công cụ sửa đổi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 45 để khớp biểu thức chính quy là một tiện ích mở rộng GNU làm cho 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 -n '1p ; $p' one.txt two.txt three.txt 28 đượ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 -n '1p ; $p' one.txt two.txt three.txt 28 được sử dụng cho lệnh chèn (xem phần )

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

Trong ví dụ này, $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 51 là địa chỉ. biểu thức chính quy với công cụ sửa đổi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 45. sed -i 's/hello/world/' file.txt 36 là lệnh xóa

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 2

Đây, $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 54 là địa chỉ. một biểu thức chính quy. sed -n '1p ; $p' one.txt two.txt three.txt 28 là lệnh chèn. sed -i 's/hello/world/' file.txt 36 là giá trị cần chèn. Sau đó, một dòng có 'd' được chèn phía trên dòng phù hợp

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 3

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 57____35_______58

Công cụ sửa đổi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 49 để 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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 54 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 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 56 và sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 57) luôn khớp với phần đầu hoặc phần cuối của bộ đệm. Ngoài ra, ký tự dấu chấm không khớp với ký tự dòng mới ở chế độ nhiều dòng

Địa chỉ regex hoạt động trên nội dung của không gian mẫu hiện tại. Nếu không gian mẫu bị thay đổi (ví dụ: với lệnh $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 66), khớp biểu thức chính quy sẽ hoạt động trên văn bản đã thay đổi

Trong ví dụ sau, in tự động bị tắt với -n. Lệnh $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 67 thay đổi các dòng chứa '2' thành 'X'. Lệnh $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 68 khớp các dòng có chữ số và in chúng. Bởi vì dòng thứ hai được thay đổi trước biểu thức chính quy $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 69, nó sẽ không khớp và sẽ không được in

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 4

4. 4 dãy địa chỉ

Có thể chỉ định phạm vi địa chỉ bằng cách chỉ định hai địa chỉ được phân tách bằng dấu phẩy ($ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 24). Một dải địa chỉ khớp với các dòng bắt đầu từ nơi địa chỉ đầu tiên khớp và tiếp tục cho đến khi địa chỉ thứ hai khớp (bao gồm)

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 5

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

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 6

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

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 7

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

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 72

Một số dòng của $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 73 có thể được sử dụng trong một đặc tả địa chỉ như $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 72 để sed -i 's/hello/world/' file.txt 22 cũng sẽ cố khớp với biểu thức chính quy trong dòng đầu vào đầu tiên. Nói cách khác, $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 72 tương tự như $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 77, ngoại trừ việc nếu addr2 khớp với dòng đầu tiên của đầu vào, biểu mẫu $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 72 sẽ coi nó là kết thúc phạm vi, trong khi biểu mẫu $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 77 sẽ khớp với phần đầu của phạm vi và do đó làm cho phạm vi mở rộng

Lưu ý rằng đây là nơi duy nhất mà địa chỉ $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 73 có ý nghĩa;

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

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 8

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 82

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

sed OPTIONS.. [SCRIPT] [INPUTFILE...] 9

addr1 can be a line number or a regular expression

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 83

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

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 0

addr1 can be a line number or a regular expression

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

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

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

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

Ví dụ trên tương đương với lệnh $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 87 này

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

Ký tự sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 54 (dấu mũ) trong biểu thức chính quy khớp với đầu dòng. Ký tự $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 89 (dấu chấm) khớp với bất kỳ ký tự đơn nào. Lệnh sed -i 's/hello/world/' file.txt 22 sau khớp và in các dòng bắt đầu bằng chữ 'b', theo sau là bất kỳ ký tự đơn nào, theo sau là chữ 'd'

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 1

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

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

Biểu thức chính quy cơ bản và mở rộng là hai biến thể về cú pháp của mẫu đã chỉ định. Cú pháp Biểu thức chính quy cơ bản (BRE) là mặc định trong sed -i 's/hello/world/' file.txt 22 (và tương tự như vậy trong $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 87). Sử dụng tùy chọn -E do POSIX chỉ định (-r, --regexp-extended) để bật cú pháp Biểu thức chính quy mở rộng (ERE)

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

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

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

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 2

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 3

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

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 4

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 5

5. 3 Overview of basic regular expression syntax

Dưới đây là mô tả ngắn gọn về cú pháp biểu thức chính quy như được sử dụng trong sed -i 's/hello/world/' file.txt 22

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 95

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

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

Matches a sequence of zero or more instances of matches for the preceding regular expression, which must be an ordinary character, a special character preceded by sed -n '1p ; $p' one.txt two.txt three.txt 97, a $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 89, 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, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 00 is equivalent to # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 01. 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 # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 03 in these contexts

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 89

Matches any character, including newline

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 54

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 ). So, it is a useful simplification to think of # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 07 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 -n '1p ; $p' one.txt two.txt three.txt 67 command

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 54 acts as a special character only at the beginning of the regular expression or subexpression (that is, after sed -n '1p ; $p' one.txt two.txt three.txt 91 or # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 11). Portable scripts should avoid sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 54 at the beginning of a subexpression, though, as POSIX allows implementations that treat sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 54 as an ordinary character in that context

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

It is the same as sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 54, 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 -n '1p ; $p' one.txt two.txt three.txt 92 or # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 11), and its use at the end of a subexpression is not portable

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 19# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 20

Matches any single character in list. for example, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 21 matches all vowels. A list may include sequences like # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 22, which matches any character between (inclusive) char1 and char2. See

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 23

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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 25

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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 27

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)

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 29

Matches between i and j, inclusive, sequences

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 30

Matches more than or equal to i sequences

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 31

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

  • Apply postfix operators, like # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 32. this will search for zero or more whole sequences of ‘abcd’, while # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 33 would search for ‘abc’ followed by zero or more occurrences of ‘d’. Note that support for # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 32 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)
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 35

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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 36

Matches the concatenation of regexp1 and regexp2. Concatenation binds more tightly than # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 11, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 54, and sed -i 's/hello/world/' file.txt 87, but less tightly than the other regular expression operators

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 40

Matches the digit-th # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 41 parenthesized subexpression in the regular expression. This is called a back reference. Subexpressions are implicitly numbered by counting occurrences of sed -n '1p ; $p' one.txt two.txt three.txt 91 left-to-right

sed -n '1p ; $p' one.txt two.txt three.txt 90

Matches the newline character

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 44

Matches char, where char is one of sed -i 's/hello/world/' file.txt 87, sed -i 's/hello/world/' file.txt 60, $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 89, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 48, sed -n '1p ; $p' one.txt two.txt three.txt 97, or sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 54. Note that the only C-like backslash sequences that you can portably assume to be interpreted are sed -n '1p ; $p' one.txt two.txt three.txt 90 and sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 95; in particular # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 53 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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 55

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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 56

trở thành 'c+' khi sử dụng các biểu thức chính quy mở rộng. Nó khớp với một hoặc nhiều chữ 'c'

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 57

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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 58

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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 59

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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 60

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

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 6

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

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 7

‘[. 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. ‘[. lower. ]’ and ‘[. upper. ]’; 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. # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 62

‘[. graph. ]’

Graphical characters. ‘[. alnum. ]’ and ‘[. punct. ]’

‘[. lower. ]’

Lower-case letters; in the ‘C’ locale and ASCII character encoding, this is # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 63

‘[. print. ]’

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

‘[. punct. ]’

Punctuation characters; in the ‘C’ locale and ASCII character encoding, this is # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 64

‘[. 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 # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 65

‘[. xdigit. ]'

Hexadecimal digits. # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 66

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

‘]’

kết thúc biểu thức ngoặc nếu nó không phải là mục danh sách đầu tiên. Vì vậy, nếu bạn muốn đặt ký tự ‘]’ thành một mục trong danh sách, bạn phải đặt ký tự này trước

'-'

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

'^'

đại diện cho các ký tự không có trong danh sách. If you want to make the ‘^’ character a list item, place it anywhere but first

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

Các ký tự sed -i 's/hello/world/' file.txt 87, sed -i 's/hello/world/' file.txt 60, $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 89, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 48, và sed -n '1p ; $p' one.txt two.txt three.txt 97 thường không có gì đặc biệt trong danh sách. Ví dụ: # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 72 khớp với ‘\’ hoặc ‘*’, vì sed -n '1p ; $p' one.txt two.txt three.txt 97 không có gì đặc biệt ở đây. Tuy nhiên, các chuỗi như # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 74, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 75 và # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 76 là đặc biệt trong danh sách và đại diện cho các ký hiệu đối chiếu, lớp tương đương và lớp ký tự tương ứng, và do đó, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 48 đặc biệt trong danh sách khi nó được theo sau bởi $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 89, sed -n '1p ; $p' one.txt two.txt three.txt 80 hoặc sed OPTIONS.. [SCRIPT] [INPUTFILE...] 83. Ngoài ra, khi không ở chế độ sed -i 's/hello/world/' file.txt 72, các lần thoát đặc biệt như sed -n '1p ; $p' one.txt two.txt three.txt 90 và # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 53 được công nhận trong danh sách. Thấy

‘[. ’

đại diện cho biểu tượng đối chiếu mở

‘. ]'

đại diện cho biểu tượng đối chiếu gần

‘[=’

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 and the sed -n '1p ; $p' one.txt two.txt three.txt 67 command)

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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 85

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

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 8

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 86

Matches any “non-word” character

$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 9

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 87

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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 0

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 88

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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 1

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 89

Matches whitespace characters (spaces and tabs). Newlines embedded in the pattern/hold spaces will also match

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 2

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 90

Matches non-whitespace characters

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 3

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 91

Matches the beginning of a word

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 4

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 92

Matches the end of a word

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 5

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 56

Matches only at the start of pattern space. This is different from sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 54 in multi-line mode

Compare the following two examples

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 6

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 57

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 -n '1p ; $p' one.txt two.txt three.txt 67 command (see and )

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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 7

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)

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 8

In the sed -n '1p ; $p' one.txt two.txt three.txt 67 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

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 9

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 escape—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

$ echo | sed 'Q42' ; echo $? 42 02

Produces or matches a BEL character, that is an “alert” (ASCII 7)

$ echo | sed 'Q42' ; echo $? 42 03

Produces or matches a form feed (ASCII 12)

sed -n '1p ; $p' one.txt two.txt three.txt 90

Produces or matches a newline (ASCII 10)

$ echo | sed 'Q42' ; echo $? 42 05

Produces or matches a carriage return (ASCII 13)

# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 53

Produces or matches a horizontal tab (ASCII 9)

$ echo | sed 'Q42' ; echo $? 42 07

Produces or matches a so called “vertical tab” (ASCII 11)

$ echo | sed 'Q42' ; echo $? 42 08

Produces or matches CONTROL-x, where x is any character. Tác dụng chính xác của ‘\cx’ như sau. nếu x là chữ thường, nó được chuyển thành chữ hoa. Sau đó, bit 6 của ký tự (hex 40) được đảo ngược. Do đó, '\cz' trở thành hex 1A, nhưng '\c{' trở thành hex 3B, trong khi '\c;' trở thành hex 7B

$ echo | sed 'Q42' ; echo $? 42 09

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

$ echo | sed 'Q42' ; echo $? 42 10

Produces or matches a character whose octal ASCII value is xxx

$ echo | sed 'Q42' ; echo $? 42 11

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 $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 66 command and Address matching. Thus the follwing two commands are equivalent (‘0x5e’ is the hexadecimal ASCII value of the character ‘^’)

$ echo | sed 'Q42' ; echo $? 42 0

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

$ echo | sed 'Q42' ; echo $? 42 1

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

$ echo | sed 'Q42' ; echo $? 42 2

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. $ echo | sed 'Q42' ; echo $? 42 15).

The following example uses the Greek letter Capital Sigma (Σ, Unicode code point $ echo | sed 'Q42' ; echo $? 42 16). In a $ echo | sed 'Q42' ; echo $? 42 15 locale, sed -i 's/hello/world/' file.txt 22 correctly processes the Sigma as one character despite it being 2 octets (bytes)

$ echo | sed 'Q42' ; echo $? 42 3

To force sed -i 's/hello/world/' file.txt 22 to process octets separately, use the $ echo | sed 'Q42' ; echo $? 42 20 locale (also known as the $ echo | sed 'Q42' ; echo $? 42 21 locale)

$ echo | sed 'Q42' ; echo $? 42 4

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 $ echo | sed 'Q42' ; echo $? 42 23 is an incomplete multibyte character (shown here as �). The regular expression ‘. ’ does not match it

$ echo | sed 'Q42' ; echo $? 42 5

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

$ echo | sed 'Q42' ; echo $? 42 6

GNU sed -i 's/hello/world/' file.txt 22 offers the special sed -n '1p ; $p' one.txt two.txt three.txt 77 command to clear the current pattern space regardless of invalid multibyte characters (i. e. it works like $ echo | sed 'Q42' ; echo $? 42 26 but also removes invalid multibyte characters)

$ echo | sed 'Q42' ; echo $? 42 7

Alternatively, force the $ echo | sed 'Q42' ; echo $? 42 20 locale to process each octet separately (every octet is a valid character in the $ echo | sed 'Q42' ; echo $? 42 20 locale)

$ echo | sed 'Q42' ; echo $? 42 8

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 $ echo | sed 'Q42' ; echo $? 42 30 is an invalid multibyte sequence, while $ echo | sed 'Q42' ; echo $? 42 31 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 $ echo | sed 'Q42' ; echo $? 42 33. Any content left in the pattern space (the invalid characters) are added to the hold space using the sed -n '1p ; $p' one.txt two.txt three.txt 47 command. On the last line (sed -i 's/hello/world/' file.txt 87), the hold space is retrieved (sed -n '1p ; $p' one.txt two.txt three.txt 75), newlines are removed ($ echo | sed 'Q42' ; echo $? 42 37), 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

$ echo | sed 'Q42' ; echo $? 42 9

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 $ echo | sed 'Q42' ; echo $? 42 20 locale and using octal escape sequences

sed '30,35d' input.txt > output.txt 0

5. 9. 2 Upper/Lower case conversion

GNU sed -i 's/hello/world/' file.txt 22’s substitute command (sed -n '1p ; $p' one.txt two.txt three.txt 67) supports upper/lower case conversions using sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 05,sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 04 codes. These conversions support multibyte characters

sed '30,35d' input.txt > output.txt 1

See

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 $ echo | sed 'Q42' ; echo $? 42 45 environment variable to the value ‘C’

sed '30,35d' input.txt > output.txt 2

Their interpretation depends on the $ echo | sed 'Q42' ; echo $? 42 46 locale; for example, ‘[[. alnum. ]]’ means the character class of numbers and letters in the current locale

TODO. show example of collation

sed '30,35d' input.txt > output.txt 3

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. 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 -n '1p ; $p' one.txt two.txt three.txt 39,sed -n '1p ; $p' one.txt two.txt three.txt 45,sed -n '1p ; $p' one.txt two.txt three.txt 47,sed -i 's/hello/world/' file.txt 71,sed -n '1p ; $p' one.txt two.txt three.txt 57. They are similar to their lowercase counterparts (sed -i 's/hello/world/' file.txt 36,sed -n '1p ; $p' one.txt two.txt three.txt 44, sed -n '1p ; $p' one.txt two.txt three.txt 46,sed -n '1p ; $p' one.txt two.txt three.txt 52,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 -n '1p ; $p' one.txt two.txt three.txt 39

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

sed -n '1p ; $p' one.txt two.txt three.txt 45

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

sed -n '1p ; $p' one.txt two.txt three.txt 47

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 -n '1p ; $p' one.txt two.txt three.txt 57

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 -n '1p ; $p' one.txt two.txt three.txt 39 commands

sed '30,35d' input.txt > output.txt 4

  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 -n '1p ; $p' one.txt two.txt three.txt 39 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 '30,35d' input.txt > output.txt 5

  1. The first expression, $ echo | sed 'Q42' ; echo $? 42 74 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 -n '1p ; $p' one.txt two.txt three.txt 75 and sed -n '1p ; $p' one.txt two.txt three.txt 67 are executed only on empty lines (i. e. paragraph separators). The sed -n '1p ; $p' one.txt two.txt three.txt 75 command fetches the accumulated lines from the hold space back to the pattern space. The $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 66 command then operates on all the text in the paragraph (including the embedded newlines)

The following example demonstrates this technique

sed '30,35d' input.txt > output.txt 6

For more annotated examples, see and

6. 4 Branching and Flow Control

Các lệnh rẽ nhánh sed -n '1p ; $p' one.txt two.txt three.txt 82, sed -n '1p ; $p' one.txt two.txt three.txt 83 và sed -n '1p ; $p' one.txt two.txt three.txt 84 cho phép thay đổi luồng của chương trình sed -i 's/hello/world/' file.txt 22

Theo mặc định, sed -i 's/hello/world/' file.txt 22 đọc một dòng đầu vào vào bộ đệm mẫu, sau đó tiếp tục xử lý tất cả các lệnh theo thứ tự. Commands without addresses affect all lines. Các lệnh có địa chỉ chỉ ảnh hưởng đến các dòng phù hợp. See and

sed -i 's/hello/world/' file.txt 22 does not support a typical $ echo | sed 'Q42' ; echo $? 42 85 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 -n '1p ; $p' one.txt two.txt three.txt 39

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

$ echo | sed 'Q42' ; echo $? 42 88$ echo | sed 'Q42' ; echo $? 42 89$ echo | sed 'Q42' ; echo $? 42 90$ echo | sed 'Q42' ; echo $? 42 91

Addresses and regular expressions can be used as an $ echo | sed 'Q42' ; echo $? 42 85 conditional. If [addr] matches the current pattern space, execute the command(s). For example. The command $ echo | sed 'Q42' ; echo $? 42 93 means. if the current pattern matches the regular expression $ echo | sed 'Q42' ; echo $? 42 94 (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 -n '1p ; $p' one.txt two.txt three.txt 82

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 -n '1p ; $p' one.txt two.txt three.txt 83

branch conditionally (that is. jump to a label) only if a $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 66 command has succeeded since the last input line was read or another conditional branch was taken

sed -n '1p ; $p' one.txt two.txt three.txt 84

similar but opposite to the sed -n '1p ; $p' one.txt two.txt three.txt 83 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 -n '1p ; $p' one.txt two.txt three.txt 82 command to skip the $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 66 command on lines containing ‘1’. The second example uses an address with negation (‘. ’) to perform substitution only on desired lines. The sed '30,35d' input.txt > output.txt 04 command is still executed on all lines

sed '30,35d' input.txt > output.txt 7

6. 4. 1 Branching and Cycles

The sed -n '1p ; $p' one.txt two.txt three.txt 82,sed -n '1p ; $p' one.txt two.txt three.txt 83 and sed -n '1p ; $p' one.txt two.txt three.txt 84 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 -n '1p ; $p' one.txt two.txt three.txt 82 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 -n '1p ; $p' one.txt two.txt three.txt 82 command jumps to the ‘x’ label, and a new cycle is never started

sed '30,35d' input.txt > output.txt 8

Branching is often complemented with the sed -n '1p ; $p' one.txt two.txt three.txt 52 or sed -i 's/hello/world/' file.txt 71 commands. both commands read the next input line into the pattern space without waiting for the cycle to restart. Before reading the next input line, sed -n '1p ; $p' one.txt two.txt three.txt 52 prints the current pattern space then empties it, while sed -i 's/hello/world/' file.txt 71 appends a newline and the next input line to the pattern space

Consider the following two examples

sed '30,35d' input.txt > output.txt 9

  • Both examples do not inf-loop, despite never starting a new cycle
  • In the first example, the sed -n '1p ; $p' one.txt two.txt three.txt 52 commands first prints the content of the pattern space, empties the pattern space then reads the next input line
  • In the second example, the sed -i 's/hello/world/' file.txt 71 commands appends the next input line to the pattern space (with a newline). Lines are accumulated in the pattern space until there are no more input lines to read, then the sed -i 's/hello/world/' file.txt 71 command terminates the sed -i 's/hello/world/' file.txt 22 program. When the program terminates, the end-of-cycle actions are performed, and the entire pattern space is printed
  • The second example requires GNU sed -i 's/hello/world/' file.txt 22, because it uses the non-POSIX-standard behavior of sed -i 's/hello/world/' file.txt 71. See the “sed -i 's/hello/world/' file.txt 71 command on the last line” paragraph in
  • To further examine the difference between the two examples, try the following commands

    sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 00

6. 4. 2 Branching example. joining lines

As a real-world example of using branching, consider the case of quoted-printable files, typically used to encode email messages. In these files long lines are split and marked with a soft line break consisting of a single ‘=’ character at the end of the line

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 01

The following program uses an address match ‘/=$/’ as a conditional. If the current pattern space ends with a ‘=’, it reads the next input line using sed -i 's/hello/world/' file.txt 71, replaces all ‘=’ characters which are followed by a newline, and unconditionally branches (sed -n '1p ; $p' one.txt two.txt three.txt 82) to the beginning of the program without restarting a new cycle. If the pattern space does not ends with ‘=’, the default action is performed. the pattern space is printed and a new cycle is started

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 02

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 -n '1p ; $p' one.txt two.txt three.txt 83 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 -n '1p ; $p' one.txt two.txt three.txt 83 command will not branch. Then, sed -n '1p ; $p' one.txt two.txt three.txt 57 will print the pattern space content until the first newline, and sed -n '1p ; $p' one.txt two.txt three.txt 39 will delete the pattern space content until the first new line. (To learn more about sed -i 's/hello/world/' file.txt 71, sed -n '1p ; $p' one.txt two.txt three.txt 57 and sed -n '1p ; $p' one.txt two.txt three.txt 39 commands see )

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 03

For more line-joining examples see

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 -n '1p ; $p' one.txt two.txt three.txt 39 and sed -n '1p ; $p' one.txt two.txt three.txt 57 commands to process multiple lines, and the sed -n '1p ; $p' one.txt two.txt three.txt 82 and sed -n '1p ; $p' one.txt two.txt three.txt 83 commands for branching. See and

Join specific lines (e. g. if lines 2 and 3 need to be joined)

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 04

Join backslash-continued lines

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 05

Join lines that start with whitespace (e. g SMTP headers)

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 06

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 '30,35d' input.txt > output.txt 39 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 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 07sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 08sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 09sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 10sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 11sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 12

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, 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 , is faster. It works by replacing trailing nines with an underscore, then using multiple sed -n '1p ; $p' one.txt two.txt three.txt 67 commands to increment the last digit, and then again substituting underscores with zeros

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 13sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 14sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 15sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 16sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 17

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 '30,35d' input.txt > output.txt 44 into a sed '30,35d' input.txt > output.txt 45 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 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 18sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 19sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 20sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 21sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 22sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 23sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 24sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 25sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 26sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 27sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 28sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 29sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 30sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 31sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 32sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 33sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 34sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 35sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 36sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 37sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 38sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 39sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 40

7. 5 Print sed '30,35d' input.txt > output.txt 47 Environment

This script strips the definition of the shell functions from the output of the sed '30,35d' input.txt > output.txt 48 Bourne-shell command

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 41sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 42sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 43sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 44sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 45sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 46sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 47

7. 6 Reverse Characters of Lines

This script can be used to reverse the position of characters in lines. Kỹ thuật này di chuyển hai ký tự cùng một lúc, do đó nó nhanh hơn các triển khai trực quan hơn

Note the sed '30,35d' input.txt > output.txt 49 command before the definition of the label. This is often needed to reset the flag that is tested by the sed -n '1p ; $p' one.txt two.txt three.txt 83 command

Imaginative readers will find uses for this script. An example is reversing the output of sed '30,35d' input.txt > output.txt 51

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 48sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 49sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 50sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 51

7. 7 Text search across multiple lines

This section uses sed -i 's/hello/world/' file.txt 71 and sed -n '1p ; $p' one.txt two.txt three.txt 39 commands to search for consecutive words spanning multiple lines. See

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

Finding doubled words in a single line is easy using GNU $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 87 and similarly with GNU sed -i 's/hello/world/' file.txt 22

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 52

  • The regular expression ‘\b\w+\s+’ searches for word-boundary (‘\b’), followed by one-or-more word-characters (‘\w+’), followed by whitespace (‘\s+’). See
  • 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
  • The word-boundery expression (‘\b’) at both ends ensures partial words are not matched (e. g. ‘the then’ is not a desired match)
  • The -E option enables extended regular expression syntax, alleviating the need to add backslashes before the parenthesis. See

When the doubled word span two lines the above regular expression will not find them as $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 87 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 -n '1p ; $p' one.txt two.txt three.txt 39 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 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 53

  • 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 -n '1p ; $p' one.txt two.txt three.txt 39 removes the first line from the pattern space (up until the first newline), readying it for the next cycle

See the GNU sed '30,35d' input.txt > output.txt 64 manual for an alternative solution using sed '30,35d' input.txt > output.txt 65 and sed '30,35d' input.txt > output.txt 66 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 -n '1p ; $p' one.txt two.txt three.txt 39 commands to search for consecutive words spanning multiple lines, and the sed -n '1p ; $p' one.txt two.txt three.txt 82 command for branching. See and

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

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 54

The following sed program wraps lines at 40 characters

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 55

The wrapped output

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 56

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 '30,35d' input.txt > output.txt 70 workalike

Note that on implementations other than GNU sed -i 's/hello/world/' file.txt 22 this script might easily overflow internal buffers

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 57sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 58sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 59sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 60

7. 10 Numbering Lines

This script replaces ‘cat -n’; in fact it formats its output exactly like GNU sed '30,35d' input.txt > output.txt 72 does

Of course this is completely useless and for two reasons. first, because somebody else did it in C, second, because the following Bourne-shell script could be used for the same purpose and would be much faster

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 61

It uses sed -i 's/hello/world/' file.txt 22 to print the line number, then groups lines two by two using sed -i 's/hello/world/' file.txt 71. Of course, this script does not teach as much as the one presented below

The algorithm used for incrementing uses both buffers, so the line is printed as soon as possible and then discarded. The number is split so that changing digits go in a buffer and unchanged ones go in the other; the changed digits are modified in a single step (using a sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 83 command). The line number for the next line is then composed and stored in the hold space, to be used in the next iteration

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 62sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 63sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 64sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 65sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 66sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 67sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 68sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 69sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 70

7. 11 Numbering Non-blank Lines

Emulating ‘cat -b’ is almost the same as ‘cat -n’—we only have to select which lines are to be numbered and which are not

The part that is common to this script and the previous one is not commented to show how important it is to comment sed -i 's/hello/world/' file.txt 22 scripts properly

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 62sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 72sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 73

7. 12 Counting Characters

This script shows another way to do arithmetic with sed -i 's/hello/world/' file.txt 22. In this case we have to add possibly large numbers, so implementing this by successive increments would not be feasible (and possibly even more complicated to contrive than this script)

The approach is to map numbers to letters, kind of an abacus implemented with sed -i 's/hello/world/' file.txt 22. ‘a’s are units, ‘b’s are tens and so on. we simply add the number of characters on the current line as units, and then propagate the carry to tens, hundreds, and so on

As usual, running totals are kept in hold space

On the last line, we convert the abacus form back to decimal. For the sake of variety, this is done with a loop rather than with some 80 sed -n '1p ; $p' one.txt two.txt three.txt 67 commands. first we convert units, removing ‘a’s from the number; then we rotate letters so that tens become ‘a’s, and so on until no more letters remain

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 62sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 75sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 76sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 77sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 78sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 79sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 80

7. 13 Counting Words

This script is almost the same as the previous one, once each of the words on the line is converted to a single ‘a’ (in the previous script each letter was changed to an ‘a’)

It is interesting that real sed '30,35d' input.txt > output.txt 80 programs have optimized loops for ‘wc -c’, so they are much slower at counting words rather than characters. This script’s bottleneck, instead, is arithmetic, and hence the word-counting one is faster (it has to manage smaller numbers)

Again, the common parts are not commented to show the importance of commenting sed -i 's/hello/world/' file.txt 22 scripts

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 62sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 82sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 83sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 84

7. 14 Counting Lines

No strange things are done now, because sed -i 's/hello/world/' file.txt 22 gives us ‘wc -l’ functionality for free. Look

7. 15 Printing the First Lines

This script is probably the simplest useful sed -i 's/hello/world/' file.txt 22 script. It displays the first 10 lines of input; the number of displayed lines is right before the sed -n '1p ; $p' one.txt two.txt three.txt 00 command

7. 16 Printing the Last Lines

Printing the last n lines rather than the first is more complex but indeed possible. n is encoded in the second line, before the bang character

This script is similar to the sed '30,35d' input.txt > output.txt 70 script in that it keeps the final output in the hold space and prints it at the end

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 62sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 86

Mainly, the scripts keeps a window of 10 lines and slides it by adding a line and deleting the oldest (the substitution command on the second line works like a sed -n '1p ; $p' one.txt two.txt three.txt 39 command but does not restart the loop)

The “sliding window” technique is a very powerful way to write efficient and complex sed -i 's/hello/world/' file.txt 22 scripts, because commands like sed -n '1p ; $p' one.txt two.txt three.txt 57 would require a lot of work if implemented manually

To introduce the technique, which is fully demonstrated in the rest of this chapter and is based on the sed -i 's/hello/world/' file.txt 71, sed -n '1p ; $p' one.txt two.txt three.txt 57 and sed -n '1p ; $p' one.txt two.txt three.txt 39 commands, here is an implementation of sed '30,35d' input.txt > output.txt 92 using a simple “sliding window. ”

This looks complicated but in fact the working is the same as the last script. after we have kicked in the appropriate number of lines, however, we stop using the hold space to keep inter-line state, and instead use sed -i 's/hello/world/' file.txt 71 and sed -n '1p ; $p' one.txt two.txt three.txt 39 to slide pattern space by one line

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 07sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 88

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

7. 17 Make Duplicate Lines Unique

This is an example of the art of using the sed -i 's/hello/world/' file.txt 71, sed -n '1p ; $p' one.txt two.txt three.txt 57 and sed -n '1p ; $p' one.txt two.txt three.txt 39 commands, probably the most difficult to master

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 89sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 90sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 91sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 92

As you can see, we maintain a 2-line window using sed -n '1p ; $p' one.txt two.txt three.txt 57 and sed -n '1p ; $p' one.txt two.txt three.txt 39. This technique is often used in advanced sed -i 's/hello/world/' file.txt 22 scripts

7. 18 Print Duplicated Lines of Input

This script prints only duplicated lines, like ‘uniq -d’

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 62sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 94sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 95sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 96sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 97

7. 19 Xóa tất cả các dòng trùng lặp

This script prints only unique lines, like ‘uniq -u’

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 07sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 99sed -i 's/hello/world/' file.txt 00sed -i 's/hello/world/' file.txt 01sed -i 's/hello/world/' file.txt 02

7. 20 Squeezing Blank Lines

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

The first leaves a blank line at the beginning and end if there are some already

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 07sed -i 's/hello/world/' file.txt 04sed -i 's/hello/world/' file.txt 05

This one is a bit more complex and removes all empty lines at the beginning. It does leave a single blank line at end if one was there

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 07sed -i 's/hello/world/' file.txt 07sed -i 's/hello/world/' file.txt 08

This removes leading and trailing blank lines. It is also the fastest. Lưu ý rằng các vòng lặp hoàn toàn được thực hiện với sed -n '1p ; $p' one.txt two.txt three.txt 52 và sed -n '1p ; $p' one.txt two.txt three.txt 82, 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 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 62_______0_______10sed -i 's/hello/world/' file.txt 11sed -i 's/hello/world/' file.txt 12sed -i 's/hello/world/' file.txt 13sed -i 's/hello/world/' file.txt 14

8 GNU sed -i 's/hello/world/' file.txt 22’s Limitations and Non-limitations

For those who want to write portable sed -i 's/hello/world/' file.txt 22 scripts, be aware that some implementations have been known to limit line lengths (for the pattern and hold spaces) to be no more than 4000 bytes. The POSIX standard specifies that conforming sed -i 's/hello/world/' file.txt 22 implementations shall support at least 8192 byte line lengths. GNU sed -i 's/hello/world/' file.txt 22 has no built-in limit on line length; as long as it can sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 008 more (virtual) memory, you can feed or construct lines as long as you like

However, recursion is used to handle subpatterns and indefinite repetition. This means that the available stack space may limit the size of the buffer that can be processed by certain patterns

9 Other Resources for Learning About sed -i 's/hello/world/' file.txt 22

For up to date information about GNU sed -i 's/hello/world/' file.txt 22 please visit https. //www. gnu. org/software/sed/

Send general questions and suggestions to sed-devel@gnu. org. Visit the mailing list archives for past discussions at https. //lists. gnu. org/archive/html/sed-devel/

The following resources provide information about sed -i 's/hello/world/' file.txt 22 (both GNU sed -i 's/hello/world/' file.txt 22 and other variations). Note these not maintained by GNU sed -i 's/hello/world/' file.txt 22 developers

10 Reporting Bugs

Email bug reports to bug-sed@gnu. org. Also, please include the output of ‘sed --version’ in the body of your report if at all possible

Please do not send a bug report like this

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

If GNU sed -i 's/hello/world/' file.txt 22 doesn’t configure your favorite package, take a few extra minutes to identify the specific problem and make a stand-alone test case. Unlike other programs such as C compilers, making such test cases for sed -i 's/hello/world/' file.txt 22 is quite simple

A stand-alone test case includes all the data necessary to perform the test, and the specific invocation of sed -i 's/hello/world/' file.txt 22 that causes the problem. The smaller a stand-alone test case is, the better. A test case should not involve something as far removed from sed -i 's/hello/world/' file.txt 22 as “try to configure frobme-1. 3. 4”. Yes, that is in principle enough information to look for the bug, but that is not a very practical prospect

Here are a few commonly reported bugs that are not bugs

sed -i 's/hello/world/' file.txt 71 command on the last line

Most versions of sed -i 's/hello/world/' file.txt 22 exit without printing anything when the sed -i 's/hello/world/' file.txt 71 command is issued on the last line of a file. GNU sed -i 's/hello/world/' file.txt 22 prints pattern space before exiting unless of course the sed -i 's/hello/world/' file.txt 45 command switch has been specified. This choice is by design

Default behavior (gnu extension, non-POSIX conforming)

To force POSIX-conforming behavior

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

For example, the behavior of

would depend on whether foo has an even or an odd number of lines. Or, when writing a script to read the next few lines following a pattern match, traditional implementations of sed -i 's/hello/world/' file.txt 22 would force you to write something like

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

instead of just

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

In any case, the simplest workaround is to use sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 024 in scripts that rely on the traditional behavior, or to set the sed -i 's/hello/world/' file.txt 72 variable to a non-empty value

Regex syntax clashes (problems with backslashes)

sed -i 's/hello/world/' file.txt 22 uses the POSIX basic regular expression syntax. According to the standard, the meaning of some escape sequences is undefined in this syntax; notable in the case of sed -i 's/hello/world/' file.txt 22 are # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 11, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 23, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 25, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 56, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 57, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 91, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 92, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 87, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 88, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 85, and # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 86

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. So, sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 040 matches one or more occurrences of ‘x’. sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 041 matches either ‘abc’ or ‘def’

This syntax may cause problems when running scripts written for other sed -i 's/hello/world/' file.txt 22s. Some sed -i 's/hello/world/' file.txt 22 programs have been written with the assumption that # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 11 and # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 23 match the literal characters sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 046 and sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 047. Such scripts must be modified by removing the spurious backslashes if they are to be used with modern implementations of sed -i 's/hello/world/' file.txt 22, like GNU sed -i 's/hello/world/' file.txt 22

Mặt khác, một số tập lệnh sử dụng s. abc\. def. g to remove occurrences of either sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 050 or sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 051. While this worked until sed -i 's/hello/world/' file.txt 22 4. 0. x, newer versions interpret this as removing the string sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 053. This is again undefined behavior according to POSIX, and this interpretation is arguably more robust. older sed -i 's/hello/world/' file.txt 22s, for example, required that the regex matcher parsed sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 055 as sed -n '1p ; $p' one.txt two.txt three.txt 94 in the common case of escaping a slash, which is again undefined behavior; the new behavior avoids this, and this is good because the regex matcher is only partially under our control

In addition, this version of sed -i 's/hello/world/' file.txt 22 supports several escape characters (some of which are multi-character) to insert non-printable characters in scripts ($ echo | sed 'Q42' ; echo $? 42 02, sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 059, sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 060, sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 061, $ echo | sed 'Q42' ; echo $? 42 05, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 53, $ echo | sed 'Q42' ; echo $? 42 07, sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 065). These can cause similar problems with scripts written for other sed -i 's/hello/world/' file.txt 22s

-i clobbers read-only files

In short, ‘sed -i’ will let you delete the contents of a read-only file, and in general the -i option (see ) lets you clobber protected files. This is not a bug, but rather a consequence of how the Unix file system works

The permissions on a file say what can happen to the data in that file, while the permissions on a directory say what can happen to the list of files in that directory. ‘sed -i’ will not ever open for writing a file that is already on disk. Rather, it will work on a temporary file that is finally renamed to the original name. if you rename or delete files, you’re actually modifying the contents of the directory, so the operation depends on the permissions of the directory, not of the file. For this same reason, sed -i 's/hello/world/' file.txt 22 does not let you use -i on a writable file in a read-only directory, and will break hard or symbolic links when -i is used on such a file

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 068 does not work (gives an error)

There is no line 0. 0 is a special address that is only used to treat addresses like sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 069 as active when the script starts. if you write sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 070 and the first line includes the word ‘abc’, then that match would be ignored because address ranges must span at least two lines (barring the end of the file); but what you probably wanted is to delete every line up to the first one including ‘abc’, and this is obtained with sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 071

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 072 is case insensitive

You are encountering problems with locales. POSIX mandates that sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 072 uses the current locale’s collation order – in C parlance, that means using sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 074 instead of sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 075. Some locales have a case-insensitive collation order, others don’t

Another problem is that sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 072 tries to use collation symbols. This only happens if you are on the GNU system, using GNU libc’s regular expression matcher instead of compiling the one supplied with GNU sed. In a Danish locale, for example, the regular expression sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 077 matches the string ‘aa’, because this is a single collating symbol that comes after ‘a’ and before ‘b’; ‘ll’ behaves similarly in Spanish locales, or ‘ij’ in Dutch locales

To work around these problems, which may cause bugs in shell scripts, set the sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 078 and $ echo | sed 'Q42' ; echo $? 42 46 environment variables to ‘C’

$ echo | sed 'Q42' ; echo $? 42 26 does not clear pattern space

This happens if your input stream includes invalid multibyte sequences. POSIX mandates that such sequences are not matched by ‘. ’, so that ‘s/. *//’ will not clear pattern space as you would expect. In fact, there is no way to clear sed’s buffers in the middle of the script in most multibyte locales (including UTF-8 locales). For this reason, GNU sed -i 's/hello/world/' file.txt 22 provides a ‘z’ command (for ‘zap’) as an extension

To work around these problems, which may cause bugs in shell scripts, set the sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt 078 and $ echo | sed 'Q42' ; echo $? 42 46 environment variables to ‘C’

Appendix A GNU Free Documentation License

Version 1. 3, 3 November 2008

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

  1. PREAMBLE

    The purpose of this License is to make a manual, textbook, or other functional and useful document free in the sense of freedom. to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others

    This License is a kind of “copyleft”, which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General Public License, which is a copyleft license designed for free software

    We have designed this License in order to use it for manuals for free software, because free software needs free documentation. a free program should come with manuals providing the same freedoms that the software does. But this License is not limited to software manuals; it can be used for any textual work, regardless of subject matter or whether it is published as a printed book. We recommend this License principally for works whose purpose is instruction or reference

  2. APPLICABILITY AND DEFINITIONS

    This License applies to any manual or other work, in any medium, that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License. Such a notice grants a world-wide, royalty-free license, unlimited in duration, to use that work under the conditions stated herein. “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. Any member of the public is a licensee, and is addressed as “you”. You accept the license if you copy, modify or distribute the work in a way requiring permission under copyright law

    A “Modified Version” of the Document means any work containing the Document or a portion of it, either copied verbatim, or with modifications and/or translated into another language

    “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 . (Thus, if the Document is in part a textbook of mathematics, a Secondary Section may not explain any mathematics. ) 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

    The “Invariant Sections” are certain Secondary Sections whose titles are designated, as being those of Invariant Sections, in the notice that says that the Document is released under this License. Nếu một phần không phù hợp với định nghĩa ở trên về Thứ cấp 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. If the Document does not identify any Invariant Sections then there are none

    The “Cover Texts” are certain short passages of text that are listed, as Front-Cover Texts or Back-Cover Texts, in the notice that says that the Document is released under this License. A Front-Cover Text may be at most 5 words, and a Back-Cover Text may be at most 25 words

    A “Transparent” copy of the Document means a machine-readable copy, represented in a format whose specification is available to the general public, that is suitable for revising the document straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup, or absence of markup, has been arranged to thwart or discourage subsequent modification by readers is not Transparent. An image format is not Transparent if used for any substantial amount of text. A copy that is not “Transparent” is called “Opaque”

    Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standard-conforming simple HTML, PostScript or PDF designed for human modification. Examples of transparent image formats include PNG, XCF and JPG. Opaque formats include proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machine-generated HTML, PostScript or PDF produced by some word processors for output purposes only

    The “Title Page” means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, “Title Page” means the text near the most prominent appearance of the work’s title, preceding the beginning of the body of the text

    The “publisher” means any person or entity that distributes copies of the Document to the public

    A section “Entitled XYZ” means a named subunit of the Document whose title either is precisely XYZ or contains XYZ in parentheses following text that translates XYZ in another language. (Here XYZ stands for a specific section name mentioned below, such as “Acknowledgements”, “Dedications”, “Endorsements”, or “History”. ) To “Preserve the Title” of such a section when you modify the Document means that it remains a section “Entitled XYZ” according to this definition

    The Document may include Warranty Disclaimers next to the notice which states that this License applies to the Document. These Warranty Disclaimers are considered to be included by reference in this License, but only as regards disclaiming warranties. any other implication that these Warranty Disclaimers may have is void and has no effect on the meaning of this License

  3. VERBATIM COPYING

    You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this License, the copyright notices, and the license notice saying this License applies to the Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to obstruct or control the reading or further copying of the copies you make or distribute. However, you may accept compensation in exchange for copies. If you distribute a large enough number of copies you must also follow the conditions in section 3

    You may also lend copies, under the same conditions stated above, and you may publicly display copies

  4. COPYING IN QUANTITY

    If you publish printed copies (or copies in media that commonly have printed covers) of the Document, numbering more than 100, and the Document’s license notice requires Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all these Cover Texts. Front-Cover Texts on the front cover, and Back-Cover Texts on the back cover. Both covers must also clearly and legibly identify you as the publisher of these copies. The front cover must present the full title with all words of the title equally prominent and visible. You may add other material on the covers in addition. Copying with changes limited to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be treated as verbatim copying in other respects

    If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed (as many as fit reasonably) on the actual cover, and continue the rest onto adjacent pages

    If you publish or distribute Opaque copies of the Document numbering more than 100, you must either include a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a computer-network location from which the general network-using public has access to download using public-standard network protocols a complete Transparent copy of the Document, free of added material. If you use the latter option, you must take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy (directly or through your agents or retailers) of that edition to the public

    It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document

  5. MODIFICATIONS

    You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modified Version under precisely this License, with the Modified Version filling the role of the Document, thus licensing distribution and modification of the Modified Version to whoever possesses a copy of it. In addition, you must do these things in the Modified Version

    1. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those of previous versions (which should, if there were any, be listed in the History section of the Document). You may use the same title as a previous version if the original publisher of that version gives permission
    2. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modifications in the Modified Version, together with at least five of the principal authors of the Document (all of its principal authors, if it has fewer than five), unless they release you from this requirement
    3. State on the Title page the name of the publisher of the Modified Version, as the publisher
    4. Preserve all the copyright notices of the Document
    5. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices
    6. Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified Version under the terms of this License, in the form shown in the Addendum below
    7. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document’s license notice
    8. Include an unaltered copy of this License
    9. Preserve the section Entitled “History”, Preserve its Title, and add to it an item stating at least the title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no section Entitled “History” in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modified Version as stated in the previous sentence
    10. Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document, and likewise the network locations given in the Document for previous versions it was based on. These may be placed in the “History” section. You may omit a network location for a work that was published at least four years before the Document itself, or if the original publisher of the version it refers to gives permission
    11. Đố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. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles
    13. Delete any section Entitled “Endorsements”. Such a section may not be included in the Modified Version
    14. Do not retitle any existing section to be Entitled “Endorsements” or to conflict in title with any Invariant Section
    15. Preserve any Warranty Disclaimers

    If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document, you may at your option designate some or all of these sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version’s license notice. These titles must be distinct from any other section titles

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

    You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover Text and one of Back-Cover Text may be added by (or through arrangements made by) any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one

    The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modified Version

  6. COMBINING DOCUMENTS

    You may combine the Document with other documents released under this License, under the terms defined in section 4 above for modified versions, provided that you include in the combination all of the Invariant Sections of all of the original documents, unmodified, and list them all as Invariant Sections of your combined work in its license notice, and that you preserve all their Warranty Disclaimers

    The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents, make the title of each such section unique by adding at the end of it, in parentheses, the name of the original author or publisher of that section if known, or else a unique number. Make the same adjustment to the section titles in the list of Invariant Sections in the license notice of the combined work

    In the combination, you must combine any sections Entitled “History” in the various original documents, forming one section Entitled “History”; likewise combine any sections Entitled “Acknowledgements”, and any sections Entitled “Dedications”. You must delete all sections Entitled “Endorsements. ”

  7. COLLECTIONS OF DOCUMENTS

    You may make a collection consisting of the Document and other documents released under this License, and replace the individual copies of this License in the various documents with a single copy that is included in the collection, provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects

    You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy of this License into the extracted document, and follow this License in all other respects regarding verbatim copying of that document

  8. AGGREGATION WITH INDEPENDENT WORKS

    A compilation of the Document or its derivatives with other separate and independent documents or works, in or on a volume of a storage or distribution medium, is called an “aggregate” if the copyright resulting from the compilation is not used to limit the legal rights of the compilation’s users beyond what the individual works permit. When the Document is included in an aggregate, this License does not apply to the other works in the aggregate which are not themselves derivative works of the Document

    If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document is less than one half of the entire aggregate, the Document’s Cover Texts may be placed on covers that bracket the Document within the aggregate, or the electronic equivalent of covers if the Document is in electronic form. Otherwise they must appear on printed covers that bracket the whole aggregate

  9. TRANSLATION

    Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License, and all the license notices in the Document, and any Warranty Disclaimers, provided that you also include the original English version of this License and the original versions of those notices and disclaimers. In case of a disagreement between the translation and the original version of this License or a notice or disclaimer, the original version will prevail

    If a section in the Document is Entitled “Acknowledgements”, “Dedications”, or “History”, the requirement (section 4) to Preserve its Title (section 1) will typically require changing the actual title

  10. TERMINATION

    You may not copy, modify, sublicense, or distribute the Document except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, or distribute it is void, and will automatically terminate your rights under this License

    However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation

    Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice

    Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, receipt of a copy of some or all of the same material does not give you any rights to use it

  11. FUTURE REVISIONS OF THIS LICENSE

    The Free Software Foundation may publish new, revised versions of the GNU Free Documentation License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. See https. //www. gnu. org/copyleft/

    Each version of the License is given a distinguishing version number. If the Document specifies that a particular numbered version of this License “or any later version” applies to it, you have the option of following the terms and conditions either of that specified version or of any later version that has been published (not as a draft) by the Free Software Foundation. If the Document does not specify a version number of this License, you may choose any version ever published (not as a draft) by the Free Software Foundation. If the Document specifies that a proxy can decide which future versions of this License can be used, that proxy’s public statement of acceptance of a version permanently authorizes you to choose that version for the Document

  12. RELICENSING

    “Massive Multiauthor Collaboration Site” (or “MMC Site”) means any World Wide Web server that publishes copyrightable works and also provides prominent facilities for anybody to edit those works. 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. A “Massive Multiauthor Collaboration” (or “MMC”) contained in the site means any set of copyrightable works thus published on the MMC site

    “CC-BY-SA” means the Creative Commons Attribution-Share Alike 3. 0 license published by Creative Commons Corporation, a not-for-profit corporation with a principal place of business in San Francisco, California, as well as future copyleft versions of that license published by that same organization

    “Incorporate” means to publish or republish a Document, in whole or in part, as part of another Document

    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 khác 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 để tái cấp phép

PHỤ LỤC. How to use this License for your documents

To use this License in a document you have written, include a copy of the License in the document and put the following copyright and license notices just after the title page

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

If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the “with…Texts. ” 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ủ đề