Ubuntu mysql không phân biệt chữ hoa chữ thường

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 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License”

1 Introduction

sed -i 's/hello/world/' file.txt 22 is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as sed -i 's/hello/world/' file.txt 24), sed -i 's/hello/world/' file.txt 22 works by making only one pass over the input(s), and is consequently more efficient. 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. Ví dụ sau in dòng đầu tiên của tệp đầu tiên (một. 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)

Các ví dụ sau là tương đươ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

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

Định dạng đầy đủ để gọi sed -i 's/hello/world/' file.txt 22 là

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

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

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

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

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

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

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

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

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

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

$ 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

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

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

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

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

Tùy chọn này chỉ định rằng các tệp sẽ được chỉnh sửa tại chỗ. GNU sed -i 's/hello/world/' file.txt 22 thực hiện điều này bằng cách tạo một tệp tạm thời và gửi đầu ra tới tệp này thay vì tới đầu ra tiêu chuẩn

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

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

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

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

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

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

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

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

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

Hãy thận trọng khi sử dụng -n với -i. cái trước tắt tự động in các dòng và cái sau thay đổi tệp tại chỗ mà không cần sao lưu. Được sử dụng bất cẩn (và không có lệnh sed -i 's/hello/world/' file.txt 37 rõ ràng), tệp đầu ra sẽ trống

# 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

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

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

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

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

Tùy chọn này khả dụng trên mọi nền tảng, nhưng chỉ hiệu quả khi hệ điều hành phân biệt giữa tệp văn bản và tệp nhị phân. Khi có sự phân biệt như vậy—như trường hợp của MS-DOS, Windows, Cygwin—các tệp văn bản bao gồm các dòng được phân tách bằng ký tự xuống dòng và ký tự nguồn cấp dữ liệu, và sed -i 's/hello/world/' file.txt 22 không thấy chữ CR kết thúc. Khi tùy chọn này được chỉ định, sed -i 's/hello/world/' file.txt 22 sẽ mở các tệp đầu vào ở chế độ nhị phân, do đó không yêu cầu xử lý đặc biệt này và xem xét các dòng kết thúc tại một nguồn cấp dữ liệu

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

Tùy chọn này chỉ khả dụng trên các nền tảng hỗ trợ liên kết tượng trưng và chỉ có tác dụng nếu tùy chọn -i được chỉ định. Trong trường hợp này, nếu tệp được chỉ định trên dòng lệnh là một liên kết tượng trưng, ​​​​_______0_______22 sẽ theo liên kết và chỉnh sửa đích cuối cùng của liên kết. Hành vi mặc định là ngắt liên kết tượng trưng để đích liên kết sẽ không bị sửa đổi

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

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

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

In sandbox mode, sed -i 's/hello/world/' file.txt 90 commands are rejected - programs containing them will be aborted without being run. 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 94sed -i 's/hello/world/' file.txt 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 ý

If no -e, -f, --expression, or --file options are given on the command-line, then the first non-option argument on the command line is taken to be the script to be executed

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ý. A file name of ‘-’ refers to the standard input stream. The standard input will be processed if no file names are specified

2. 3 Exit status

An exit status of zero indicates success, and a nonzero value indicates failure. GNU sed -i 's/hello/world/' file.txt 22 returns the following exit status error values

0

Successful completion

1

Invalid command, invalid syntax, invalid regular expression or a GNU sed -i 's/hello/world/' file.txt 22 extension command used with --posix

2

One or more of the input file specified on the command line could not be opened (e. g. if a file is not found, or read permission is denied). Processing continued with other files

4

An I/O error, or a serious processing error during runtime, GNU sed -i 's/hello/world/' file.txt 22 aborted immediately

Additionally, the commands sed -n '1p ; $p' one.txt two.txt three.txt 00 and sed -n '1p ; $p' one.txt two.txt three.txt 01 can be used to terminate sed -i 's/hello/world/' file.txt 22 with a custom exit code value (this is a GNU sed -i 's/hello/world/' file.txt 22 extension)

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

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

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

A sed -i 's/hello/world/' file.txt 22 program consists of one or more sed -i 's/hello/world/' file.txt 22 commands, passed in by one or more of the -e, -f, --expression, and --file options, or the first non-option argument if zero of these options are used. This document will refer to “the” sed -i 's/hello/world/' file.txt 22 script; this is understood to mean the in-order concatenation of all of the scripts and script-files passed in. See

sed -i 's/hello/world/' file.txt 22 commands follow this syntax

X is a single-letter sed -i 's/hello/world/' file.txt 22 command. sed -n '1p ; $p' one.txt two.txt three.txt 11 is an optional line address. If sed -n '1p ; $p' one.txt two.txt three.txt 11 is specified, the command X will be executed only on the matched lines. sed -n '1p ; $p' one.txt two.txt three.txt 11 can be a single line number, a regular expression, or a range of lines (see ). Additional sed -n '1p ; $p' one.txt two.txt three.txt 14 are used for some sed -i 's/hello/world/' file.txt 22 commands

The following example deletes lines 30 to 35 in the input. sed -n '1p ; $p' one.txt two.txt three.txt 16 is an address range. sed -i 's/hello/world/' file.txt 36 is the delete command

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

The following example prints all input until a line starting with the word ‘foo’ is found. If such line is found, sed -i 's/hello/world/' file.txt 22 will terminate with exit status 42. 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 is a regular-expression address. sed -n '1p ; $p' one.txt two.txt three.txt 00 is the quit command. sed -n '1p ; $p' one.txt two.txt three.txt 22 is the command option

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

Commands within a script or script-file can be separated by semicolons (sed -n '1p ; $p' one.txt two.txt three.txt 23) or newlines (ASCII 10). Multiple scripts can be specified with -e or -f options

The following examples are all equivalent. They perform two sed -i 's/hello/world/' file.txt 22 operations. deleting any lines matching the regular expression sed -n '1p ; $p' one.txt two.txt three.txt 20, and replacing all occurrences of the string ‘hello’ with ‘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

Commands 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, due to their syntax, cannot be followed by semicolons working as command separators and thus should be terminated with newlines or be placed at the end of a script or script-file. Commands can also be preceded with optional non-significant whitespace characters. See

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

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

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

Append text after a line

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

Append text after a line (alternative syntax)

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

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

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

Replace (change) lines with text

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

Replace (change) lines with text (alternative syntax)

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. The meaning is as follows

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

Turn the replacement to uppercase until 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 04 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 06 is found,

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'. When replacing the first ‘-’, the ‘\u’ sequence only affects the empty replacement of ‘\1’. It does not affect the sed -n '1p ; $p' one.txt two.txt three.txt 75 character that is added to pattern space when replacing 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 with 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

On the other hand, 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 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 11 do affect the remainder of the replacement text if they are followed by an empty substitution. With ‘a-b-’ in pattern space, the following command

will replace ‘-’ with ‘X’ (uppercase) and ‘b-’ with ‘Bx’. If this behavior is undesirable, you can prevent it by adding a ‘\E’ sequence—after ‘\1’ in this case

To include a literal sed -n '1p ; $p' one.txt two.txt three.txt 97, sed -n '1p ; $p' one.txt two.txt three.txt 93, or newline in the final replacement, be sure to precede the desired sed -n '1p ; $p' one.txt two.txt three.txt 97, sed -n '1p ; $p' one.txt two.txt three.txt 93, or newline in the replacement with a sed -n '1p ; $p' one.txt two.txt three.txt 97

The sed -n '1p ; $p' one.txt two.txt three.txt 67 command can be followed by zero or more of the following flags

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

Apply the replacement to all matches to the regexp, not just the first

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

Only replace the numberth match of the regexp

interaction in sed -n '1p ; $p' one.txt two.txt three.txt 67 command Note. the POSIX standard does not specify what should happen when you mix the sed -n '1p ; $p' one.txt two.txt three.txt 44 and number modifiers, and currently there is no widely agreed upon meaning across sed -i 's/hello/world/' file.txt 22 implementations. For GNU sed -i 's/hello/world/' file.txt 22, the interaction is defined to be. ignore matches before the numberth, and then match and replace all matches from the numberth on

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

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

Note. when both the sed -i 's/hello/world/' file.txt 37 and sed -n '1p ; $p' one.txt two.txt three.txt 41 options are specified, the relative ordering of the two produces very different results. In general, 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 (evaluate then print) is what you want, but operating the other way round can be useful for debugging. For this reason, the current version of GNU sed -i 's/hello/world/' file.txt 22 interprets specially the presence of sed -i 's/hello/world/' file.txt 37 options both before and after sed -n '1p ; $p' one.txt two.txt three.txt 41, printing the pattern space before and after evaluation, while in general flags for the sed -n '1p ; $p' one.txt two.txt three.txt 67 command show their effect just once. This behavior, although documented, might change in future versions

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

If the substitution was made, then write out the result to the named file. As a GNU sed -i 's/hello/world/' file.txt 22 extension, two special values of filename are supported. /dev/stderr, which writes the result to the standard error, and /dev/stdout, which writes to the standard output

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

This command allows one to pipe input from a shell command into pattern space. If a substitution was made, the command that is found in pattern space is executed and pattern space is replaced with its output. Một dòng mới ở cuối bị chặn; . This is a GNU sed -i 's/hello/world/' file.txt 22 extension

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

The 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 modifier to regular-expression matching is a GNU extension which makes sed -i 's/hello/world/' file.txt 22 match regexp in a case-insensitive manner

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

The 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 modifier to regular-expression matching is a GNU sed -i 's/hello/world/' file.txt 22 extension which directs GNU sed -i 's/hello/world/' file.txt 22 to match the regular expression in multi-line mode. The modifier causes 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 to match respectively (in addition to the normal behavior) the empty string after a newline, and the empty string before a newline. There are special character sequences (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 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 57) which always match the beginning or the end of the buffer. In addition, the period character does not match a new-line character in multi-line mode

3. 4 Often-Used Commands

If you use sed -i 's/hello/world/' file.txt 22 at all, you will quite likely want to know these commands

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

[No addresses allowed. ]

The sed -n '1p ; $p' one.txt two.txt three.txt 78 character begins a comment; the comment continues until the next newline

If you are concerned about portability, be aware that some implementations of sed -i 's/hello/world/' file.txt 22 (which are not POSIX conforming) may only support a single one-line comment, and then only when the very first character of the script is a sed -n '1p ; $p' one.txt two.txt three.txt 78

Warning. if the first two characters of the sed -i 's/hello/world/' file.txt 22 script are 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, then the -n (no-autoprint) option is forced. If you want to put a comment in the first line of your script and that comment begins with the letter ‘n’ and you do not want this behavior, then be sure to either use a capital ‘N’, or place at least one space before the ‘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 65

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

Example. stop after printing the second line

This command accepts only one address. Note that the current pattern space is printed if auto-print is not disabled with the -n options. The ability to return an exit code from the sed -i 's/hello/world/' file.txt 22 script is a GNU sed -i 's/hello/world/' file.txt 22 extension

See also the GNU sed -i 's/hello/world/' file.txt 22 extension sed -n '1p ; $p' one.txt two.txt three.txt 01 command which quits silently without printing the current pattern space

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

Xóa không gian mẫu;

Example. delete the second input line

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

Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option

Example. print only the second input line

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

If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then sed -i 's/hello/world/' file.txt 22 exits without processing any more commands

This command is useful to skip lines (e. g. process every Nth line)

Example. perform substitution on every 3rd line (i. e. two sed -n '1p ; $p' one.txt two.txt three.txt 52 commands skip two 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 2

GNU sed -i 's/hello/world/' file.txt 22 provides an extension address syntax of first~step to achieve the same result

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

A group of commands may be enclosed between 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 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 79 characters. This is particularly useful when you want a group of commands to be triggered by a single address (or address-range) match

Example. perform substitution then print the second input line

sed '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 Less Frequently-Used Commands

Though perhaps less frequently used than those in the previous section, some very small yet useful sed -i 's/hello/world/' file.txt 22 scripts can be built with these commands

sed '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

Example. transliterate ‘a-j’ into ‘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

(The sed -n '1p ; $p' one.txt two.txt three.txt 94 characters may be uniformly replaced by any other single character within any given 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. )

Instances of the sed -n '1p ; $p' one.txt two.txt three.txt 94 (or whatever other character is used in its stead), sed -n '1p ; $p' one.txt two.txt three.txt 97, or newlines can appear in the source-chars or dest-chars lists, provide that each instance is escaped by a sed -n '1p ; $p' one.txt two.txt three.txt 97. The source-chars and dest-chars lists must contain the same number of characters (after de-escaping)

See the 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 command from GNU coreutils for similar functionality

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

Appending text after a line. This is a GNU extension to the standard sed -n '1p ; $p' one.txt two.txt three.txt 26 command - see below for details

Example. Add the word ‘hello’ after the second 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 6

Leading whitespace after the sed -n '1p ; $p' one.txt two.txt three.txt 26 command is ignored. The text to add is read until the end of the line

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

Appending text after a line

Example. Add ‘hello’ after the second line (-. indicates printed output 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 7

The sed -n '1p ; $p' one.txt two.txt three.txt 26 command queues the lines of text which follow this command (each but the last ending with a sed -n '1p ; $p' one.txt two.txt three.txt 97, which are removed from the output) to be output at the end of the current cycle, or when the next input line is read

As a GNU extension, this command accepts two addresses

Escape sequences in text are processed, so you should use 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 text to print a single backslash

The commands resume after the last line without a backslash (sed -n '1p ; $p' one.txt two.txt three.txt 97) - ‘world’ in the following example

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

As a GNU extension, the sed -n '1p ; $p' one.txt two.txt three.txt 26 command and text can be separated into two 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 parameters, enabling easier scripting

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

insert text before a line. Đâ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

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

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

Leading whitespace after the sed -n '1p ; $p' one.txt two.txt three.txt 28 command is ignored. The text to add is read until the end of the line

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

Immediately output the lines of text which follow this command

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

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

As a GNU extension, this command accepts two addresses

Escape sequences in text are processed, so you should use 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 text to print a single backslash

The commands resume after the last line without a backslash (sed -n '1p ; $p' one.txt two.txt three.txt 97) - ‘world’ in the following example

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

As a GNU extension, the sed -n '1p ; $p' one.txt two.txt three.txt 28 command and text can be separated into two 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 parameters, enabling easier scripting

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

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

Replaces the line(s) with text. This is a GNU extension to the standard sed -n '1p ; $p' one.txt two.txt three.txt 27 command - see below for details

Example. Replace the 2nd to 9th lines with the word ‘hello’

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

Leading whitespace after the sed -n '1p ; $p' one.txt two.txt three.txt 27 command is ignored. The text to add is read until the end of the line

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

Delete the lines matching the address or address-range, and output the lines of text which follow this command

Example. Replace 2nd to 4th lines with the words ‘hello’ and ‘world’ (-. indicates printed output lines)

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

If no addresses are given, each line is replaced

A new cycle is started after this command is done, since the pattern space will have been deleted. In the following example, the sed -n '1p ; $p' one.txt two.txt three.txt 27 starts a new cycle and the substitution command is not performed on the replaced text

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

As a GNU extension, the sed -n '1p ; $p' one.txt two.txt three.txt 27 command and text can be separated into two 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 parameters, enabling easier scripting

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

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

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

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

As a GNU extension, this command accepts two addresses

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

Print the pattern space in an unambiguous form. 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 specifies the desired line-wrap length; a length of 0 (zero) means to never wrap long lines. If omitted, the default as specified on the command line is used. The n parameter is a GNU sed -i 's/hello/world/' file.txt 22 extension

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

Đọc tên tập tin. Example

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

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

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

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

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

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

The file will be created (or truncated) before the first input line is read; all sed OPTIONS.. [SCRIPT] [INPUTFILE...] 26 commands (including instances of the sed OPTIONS.. [SCRIPT] [INPUTFILE...] 26 flag on successful sed -n '1p ; $p' one.txt two.txt three.txt 67 commands) which refer to the same filename are output without closing and reopening the file

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

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

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

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

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

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

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

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

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

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

In most cases, use of these commands indicates that you are probably better off programming in something like sed OPTIONS.. [SCRIPT] [INPUTFILE...] 41 or Perl. But occasionally one is committed to sticking with sed -i 's/hello/world/' file.txt 22, and these commands can enable one to write quite convoluted scripts

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

[No addresses allowed. ]

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

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

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

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

Branch to label only if there has been a successful sed -n '1p ; $p' one.txt two.txt three.txt 67ubstitution since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started

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

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

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

This command allows one to pipe input from a shell command into pattern space. Without parameters, the sed -n '1p ; $p' one.txt two.txt three.txt 41 command executes the command that is found in pattern space and replaces the pattern space with the output; a trailing newline is suppressed

If a parameter is specified, instead, the sed -n '1p ; $p' one.txt two.txt three.txt 41 command interprets it as a command and sends its output to the output stream. The command can run across multiple lines, all but the last ending with a back-slash

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

Note that, unlike the sed OPTIONS.. [SCRIPT] [INPUTFILE...] 54 command, the output of the command will be printed immediately; the sed OPTIONS.. [SCRIPT] [INPUTFILE...] 54 command instead delays the output to the end of the current cycle

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

Print out the file name of the current input file (with a trailing newline)

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

This command accepts only one address

This command is the same as sed -n '1p ; $p' one.txt two.txt three.txt 00, but will not print the contents of pattern space. Like sed -n '1p ; $p' one.txt two.txt three.txt 00, it provides the ability to return an exit code to the caller

This command can be useful because the only alternative ways to accomplish this apparently trivial function are to use the -n option (which can unnecessarily complicate your script) or resorting to the following snippet, which wastes time by reading the whole file without any visible effect

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

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

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

As with the sed OPTIONS.. [SCRIPT] [INPUTFILE...] 54 command, the special value /dev/stdin is supported for the file name, which reads a line from the standard input

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

Branch to label only if there have been no successful sed -n '1p ; $p' one.txt two.txt three.txt 67ubstitutions since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started

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

This command does nothing, but makes sed -i 's/hello/world/' file.txt 22 fail if GNU sed -i 's/hello/world/' file.txt 22 extensions are not supported, simply because other versions of sed -i 's/hello/world/' file.txt 22 do not implement it. In addition, you can specify the version of sed -i 's/hello/world/' file.txt 22 that your script requires, such as sed OPTIONS.. [SCRIPT] [INPUTFILE...] 69. The default is sed OPTIONS.. [SCRIPT] [INPUTFILE...] 70 because that is the first version that implemented this command

This command enables all GNU extensions even if sed -i 's/hello/world/' file.txt 72 is set in the environment

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

Write to the given filename the portion of the pattern space up to the first newline. Everything said under the sed OPTIONS.. [SCRIPT] [INPUTFILE...] 26 command about file handling holds here too

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

This command empties the content of pattern space. It is usually the same as ‘s/. *//’, but is more efficient and works in the presence of invalid multibyte sequences in the input stream. POSIX mandates that such sequences are not matched by ‘. ’, so that there is no portable way to clear sed -i 's/hello/world/' file.txt 22’s buffers in the middle of the script in most multibyte locales (including UTF-8 locales)

3. 8 Multiple commands syntax

There are several methods to specify multiple commands in a sed -i 's/hello/world/' file.txt 22 program

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

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

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

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

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

The 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,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 commands can be separated with a semicolon (this is a non-portable GNU sed -i 's/hello/world/' file.txt 22 extension)

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

Labels used in 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 commands are read until a semicolon. Leading and trailing whitespace is ignored. In the examples below the label is ‘x’. The first example works with GNU sed -i 's/hello/world/' file.txt 22. The second is a portable equivalent. For more information about branching and labels see

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

3. 8. 1 Commands Requiring a newline

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

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 (append/change/insert)

All characters following 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 commands are taken as the text to append/change/insert. Using a semicolon leads to undesirable results

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

Separate the commands using -e or a newline

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

Note that specifying the text to add (‘Hello’) immediately after 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 is itself a GNU sed -i 's/hello/world/' file.txt 22 extension. A portable, POSIX-compliant alternative is

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

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

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

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

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 (reading and writing files)

The 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 commands parse the filename until end of the line. If whitespace, comments or semicolons are found, they will be included in the filename, leading to unexpected results

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

Note that sed -i 's/hello/world/' file.txt 22 silently ignores read/write errors in 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 commands (such as missing files). In the following example, sed -i 's/hello/world/' file.txt 22 tries to read a file named ‘hello. txt ; N’. The file is missing, and the error is silently ignored

sed '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 (command execution)

Any characters following the sed -n '1p ; $p' one.txt two.txt three.txt 41 command until the end of the line will be sent to the shell. If whitespace, comments or semicolons are found, they will be included in the shell command, leading to unexpected results

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 (substitute with sed -n '1p ; $p' one.txt two.txt three.txt 41 or sed OPTIONS.. [SCRIPT] [INPUTFILE...] 26 flags)

In a substitution command, the sed OPTIONS.. [SCRIPT] [INPUTFILE...] 26 flag writes the substitution result to a file, and the sed -n '1p ; $p' one.txt two.txt three.txt 41 flag executes the subsitution result as a shell command. As with 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 22 commands, these must be terminated with a newline. If whitespace, comments or semicolons are found, they will be included in the shell command or filename, leading to unexpected results

sed '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 Addresses. selecting lines

4. 1 Addresses overview

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

sed '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

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

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

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

sed '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

An address range is specified with two addresses separated by a comma ($ 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). Addresses can be numeric, regular expressions, or a mix of both. The following command replaces the word ‘hello’ with ‘world’ only in lines 4 to 17 (inclusive)

sed '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

Appending 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 25 character to the end of an address specification (before the command letter) negates the sense of the match. That is, if 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 25 character follows an address or an address range, then only lines which do not match the addresses will be selected. The following command replaces the word ‘hello’ with ‘world’ only in lines not containing the word ‘apple’

sed '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

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

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

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

sed '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

Specifying a line number will match only that line in the input. (Note that sed -i 's/hello/world/' file.txt 22 counts lines continuously across all input files unless -i or -s options are specified. )

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

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

$ 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

This GNU extension matches every stepth line starting with line first. In particular, lines will be selected when there exists a non-negative n such that the current line-number equals first + (n * step). Thus, one would use $ 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 to select the odd-numbered lines and $ 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 for even-numbered lines; to pick every third line starting with the second, ‘2~3’ would be used; to pick every fifth line starting with the tenth, use ‘10~5’; and ‘50~0’ is just an obscure way of saying $ 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 34

The following commands demonstrate the step address usage

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 selecting lines by text matching

GNU sed -i 's/hello/world/' file.txt 22 supports the following regular expression addresses. The default regular expression is . If -E or -r options are used, The regular expression should be in syntax. See

$ 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

This will select any line which matches the regular expression regexp. If regexp itself includes any sed -n '1p ; $p' one.txt two.txt three.txt 94 characters, each must be escaped by a backslash (sed -n '1p ; $p' one.txt two.txt three.txt 97)

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

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

The empty regular expression ‘//’ repeats the last regular expression match (the same holds if the empty regular expression is passed to the sed -n '1p ; $p' one.txt two.txt three.txt 67 command). Note that modifiers to regular expressions are evaluated when the regular expression is compiled, thus it is invalid to specify them together with the empty regular expression

$ 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

(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 41 may be replaced by any other single character. )

This also matches the regular expression regexp, but allows one to use a different delimiter than sed -n '1p ; $p' one.txt two.txt three.txt 94. This is particularly useful if the regexp itself contains a lot of slashes, since it avoids the tedious escaping of every sed -n '1p ; $p' one.txt two.txt three.txt 94. If regexp itself includes any delimiter characters, each must be escaped by a backslash (sed -n '1p ; $p' one.txt two.txt three.txt 97)

The following commands are equivalent. 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 để so khớp biểu thức chính quy là một phần mở rộng GNU làm cho biểu thức chính quy được so 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

The 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 modifier to regular-expression matching is a GNU sed -i 's/hello/world/' file.txt 22 extension which directs GNU sed -i 's/hello/world/' file.txt 22 to match the regular expression in multi-line mode. The modifier causes 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 to match respectively (in addition to the normal behavior) the empty string after a newline, and the empty string before a newline. There are special character sequences (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 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 57) which always match the beginning or the end of the buffer. In addition, the period character does not match a new-line character in multi-line mode

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

A line number of $ 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 can be used in an address specification like $ 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 so that sed -i 's/hello/world/' file.txt 22 will try to match regexp in the first input line too. 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 có thể là số dòng hoặ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 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 có thể là số dòng hoặc biểu thức chính quy

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

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

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

The following command prints lines containing the word ‘hello’

The above example is equivalent to this $ 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 command

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

The character sed '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 (caret) in a regular expression matches the beginning of the line. The 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 89 (dot) matches any single character. The following sed -i 's/hello/world/' file.txt 22 command matches and prints lines which start with the letter ‘b’, followed by any single character, followed by the letter ‘d’

$ 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

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

5. 2 Basic (BRE) and extended (ERE) regular expression

Basic and extended regular expressions are two variations on the syntax of the specified pattern. Basic Regular Expression (BRE) syntax is the default in sed -i 's/hello/world/' file.txt 22 (and similarly in $ 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). Use the POSIX-specified -E option (-r, --regexp-extended) to enable Extended Regular Expression (ERE) syntax

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

With basic (BRE) syntax, these characters do not have special meaning unless prefixed with a backslash (‘\’); While with extended (ERE) syntax it is reversed. these characters are special unless they are prefixed with backslash (‘\’)

Desired patternBasic (BRE) SyntaxExtended (ERE) Syntaxliteral ‘+’ (plus sign)

$ 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

One or more ‘a’ characters followed by ‘b’ (plus sign as special meta-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 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

Here is a brief description of regular expression syntax as used in 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

A single ordinary character matches itself

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

Matches a sequence of zero or more instances of matches for the preceding regular expression, which must be an ordinary character, a special character preceded by sed -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. Sự đơn giản hóa này có hiệu lực miễn là nội dung ban đầu của không gian mẫu không bị sửa đổi, ví dụ như với lệnh sed -n '1p ; $p' one.txt two.txt three.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 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

Lưu ý rằng trình so khớp cụm từ thông dụng là tham lam, tô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’

Phù hợp với 'abcdef'

‘a*b’

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

‘a\?b’

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

‘a\+b\+’

So khớp một hoặc nhiều chữ 'a' theo sau bởi một hoặc nhiều chữ 'b'. ‘ab’ is the shortest possible match, but other examples are ‘aaaab’ or ‘abbbbb’ or ‘aaaaaabbbbbbb’

‘. *''. \+’

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

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

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

'^#'

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

‘\\$’

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

‘\$’

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

‘[a-zA-Z0-9]’

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

‘[^ TAB]\+’

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

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

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

‘. \{9\}A$’

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

‘^. \{15\}A’

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

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

Sự khác biệt duy nhất giữa biểu thức chính quy cơ bản và mở rộng là trong hành vi của một vài ký tự. ‘?’, ‘+’, dấu ngoặc đơn, dấu ngoặc nhọn (‘{}’) và ‘. ’. Mặc dù các biểu thức chính quy cơ bản yêu cầu chúng phải được thoát nếu bạn muốn chúng hoạt động như các ký tự đặc biệt, nhưng khi sử dụng các biểu thức chính quy mở rộng, bạn phải thoát chúng nếu bạn muốn chúng khớp với một ký tự chữ. ‘. ’ đặc biệt ở đây vì ‘\. ' là một phần mở rộng GNU - các biểu thức chính quy cơ bản tiêu chuẩn không cung cấp chức năng của nó

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

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

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

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

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

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

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

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

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

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

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

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

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

Các lớp được đặt tên này phải được sử dụng bên trong ngoặc đơn. Cách sử dụng đú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 6

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

$ 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

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

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

‘[. chữ cái. ]'

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

‘[. trống. ]'

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

‘[. cntrl. ]'

Ký tự điều khiển. Trong ASCII, các ký tự này có mã bát phân từ 000 đến 037 và 177 (DEL). Trong các bộ ký tự khác, đây là các ký tự tương đương, nếu có

‘[. chữ số. ]'

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

‘[. đồ thị. ]'

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

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

Chữ viết thường;

‘[. in. ]'

Ký tự in được. ‘[. hàng năm. ]’, ‘[. dấu câu. ]', và dấu cách

‘[. dấu câu. ]'

Ký tự dấu câu;

‘[. không gian. ]'

Ký tự khoảng trắng. trong ngôn ngữ 'C', đây là tab, dòng mới, tab dọc, nguồn cấp dữ liệu biểu mẫu, xuống dòng và dấu cách

‘[. phía trên. ]'

Chữ in hoa. trong ngôn ngữ 'C' và mã hóa ký tự ASCII, đây là # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 65

‘[. x chữ số. ]'

chữ số thập lục phân. # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE 66

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

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

‘]’

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. Nếu bạn muốn biến ký tự '^' thành một mục trong danh sách, hãy đặt ký tự đó ở bất kỳ đâu nhưng trước tiên

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

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

'[='

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

'=]'

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

‘[. ’

đại diện cho biểu tượng lớp ký tự mở và phải được theo sau bởi tên lớp ký tự hợp lệ

‘. ]'

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

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

Các chuỗi sau đây có ý nghĩa đặc biệt bên trong các biểu thức chính quy (được sử dụng trong và lệnh sed -n '1p ; $p' one.txt two.txt three.txt 67)

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

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

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

$ 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

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

$ 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

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

# 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

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

# 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

So khớp các ký tự khoảng trắng (dấu cách và tab). Các dòng mới được nhúng trong mẫu/không gian giữ cũng sẽ khớp

# 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

So khớp các ký tự không phải khoảng trắng

# 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

Khớp với phần đầu của một từ

# 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

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

# 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

Chỉ khớp khi bắt đầu không gian mẫu. Điều này khác vớ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 54 ở chế độ nhiều dòng

So sánh hai ví dụ sau

# 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

Chỉ khớp ở cuối không gian mẫu. Điều này khác với sed -i 's/hello/world/' file.txt 87 ở chế độ nhiều dòng

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

tham chiếu ngược là các lệnh biểu thức chính quy đề cập đến một phần trước đó của biểu thức chính quy phù hợp. Tham chiếu ngược được chỉ định bằng dấu gạch chéo ngược và một chữ số (e. g. ‘\1’). Phần của biểu thức chính quy mà chúng đề cập đến được gọi là biểu thức con và được chỉ định bằng dấu ngoặc đơn

Tham chiếu ngược và biểu thức con được sử dụng trong hai trường hợp. trong mẫu tìm kiếm biểu thức chính quy và trong phần thay thế của lệnh sed -n '1p ; $p' one.txt two.txt three.txt 67 (xem và )

Trong mẫu biểu thức chính quy, tham chiếu ngược được sử dụng để khớp cùng nội dung với biểu thức con đã khớp trước đó. Trong ví dụ sau, biểu thức con là '. ’ - bất kỳ ký tự đơn nào (được bao quanh bởi dấu ngoặc đơn khiến nó trở thành biểu thức con). Tham chiếu ngược '\ 1' yêu cầu khớp cùng nội dung (cùng ký tự) với biểu thức con

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

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

Nhiều biểu thức con được đánh số tự động từ trái sang phải. Lệnh này tìm kiếm các bảng đối xứng gồm 6 chữ cái (ba chữ cái đầu tiên là 3 biểu thức con, tiếp theo là 3 tham chiếu ngược theo thứ tự ngược lại)

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

Trong lệnh sed -n '1p ; $p' one.txt two.txt three.txt 67, các tham chiếu ngược có thể được sử dụng trong phần thay thế để tham chiếu lại các biểu thức con trong phần biểu thức chính quy

Ví dụ sau sử dụng hai biểu thức con trong biểu thức chính quy để khớp với hai từ được phân tách bằng dấu cách. Các tham chiếu ngược trong phần thay thế in các từ theo một thứ tự khác

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

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

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

Cho đến chương này, chúng ta mới chỉ gặp các lối thoát có dạng '\^', lệnh này yêu cầu sed -i 's/hello/world/' file.txt 22 không hiểu dấu mũ là một ký tự đặc biệt, mà nên hiểu nó theo nghĩa đen. Ví dụ: '\*' khớp với một dấu hoa thị thay vì 0 hoặc nhiều dấu gạch chéo ngược

Chương này giới thiệu một loại thoát khác—đó là, các thoát được áp dụng cho một ký tự hoặc chuỗi ký tự thường được hiểu theo nghĩa đen và sed -i 's/hello/world/' file.txt 22 đó thay thế bằng một ký tự đặc biệt. Điều này cung cấp một cách mã hóa các ký tự không in được trong các mẫu theo cách hiển thị. Không có hạn chế về sự xuất hiện của các ký tự không in được trong tập lệnh sed -i 's/hello/world/' file.txt 22 nhưng khi tập lệnh đang được chuẩn bị trong trình bao hoặc bằng cách chỉnh sửa văn bản, việc sử dụng một trong các chuỗi thoát sau đây thường dễ dàng hơn so với ký tự nhị phân mà nó đại diện

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Tạo hoặc khớp với CONTROL-x, trong đó x là ký tự bất kỳ. 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

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

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

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

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

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

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

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

Như sau (‘0x5b’,‘0x5d’ lần lượt là các giá trị ASCII thập lục phân của ‘[’, ‘]’)

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

Tuy nhiên, nên tránh các ký tự đặc biệt như vậy do các trường hợp cạnh không mong muốn. Ví dụ, những điều sau đây không tương đương

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

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

GNU sed -i 's/hello/world/' file.txt 22 xử lý các ký tự nhiều byte hợp lệ trong các ngôn ngữ nhiều byte (e. g. $ echo | sed 'Q42' ; echo $? 42 15).

Ví dụ sau sử dụng chữ cái Hy Lạp Capital Sigma (Σ, điểm mã Unicode $ echo | sed 'Q42' ; echo $? 42 16). Trong ngôn ngữ $ echo | sed 'Q42' ; echo $? 42 15, sed -i 's/hello/world/' file.txt 22 xử lý chính xác Sigma dưới dạng một ký tự mặc dù nó là 2 octet (byte)

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

Để buộc sed -i 's/hello/world/' file.txt 22 xử lý các octet riêng biệt, hãy sử dụng ngôn ngữ $ echo | sed 'Q42' ; echo $? 42 20 (còn được gọi là ngôn ngữ $ echo | sed 'Q42' ; echo $? 42 21)

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

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

Các biểu thức chính quy của sed -i 's/hello/world/' file.txt 22 không khớp với các chuỗi nhiều byte không hợp lệ trong một ngôn ngữ nhiều byte

Trong các ví dụ sau, giá trị ascii $ echo | sed 'Q42' ; echo $? 42 23 là một ký tự nhiều byte không hoàn chỉnh (được hiển thị ở đây là �). Biểu thức chính quy ‘. ' không phù hợp với nó

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

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

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

GNU sed -i 's/hello/world/' file.txt 22 cung cấp lệnh sed -n '1p ; $p' one.txt two.txt three.txt 77 đặc biệt để xóa không gian mẫu hiện tại bất kể các ký tự nhiều byte không hợp lệ (i. e. nó hoạt động giống như $ echo | sed 'Q42' ; echo $? 42 26 nhưng cũng loại bỏ các ký tự nhiều byte không hợp lệ)

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

Ngoài ra, buộc ngôn ngữ $ echo | sed 'Q42' ; echo $? 42 20 xử lý riêng từng octet (mỗi octet là một ký tự hợp lệ trong ngôn ngữ $ echo | sed 'Q42' ; echo $? 42 20)

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

Việc sed -i 's/hello/world/' file.txt 22 không có khả năng xử lý các ký tự nhiều byte không hợp lệ có thể được sử dụng để phát hiện các chuỗi không hợp lệ đó trong một tệp. Trong các ví dụ sau, $ echo | sed 'Q42' ; echo $? 42 30 là một chuỗi nhiều byte không hợp lệ, trong khi $ echo | sed 'Q42' ; echo $? 42 31 là một chuỗi nhiều byte hợp lệ (của ký tự Sigma trong tiếng Hy Lạp)

Chương trình sed -i 's/hello/world/' file.txt 22 sau đây xóa tất cả các ký tự hợp lệ bằng cách sử dụng $ echo | sed 'Q42' ; echo $? 42 33. Bất kỳ nội dung nào còn lại trong không gian mẫu (các ký tự không hợp lệ) đều được thêm vào không gian giữ bằng lệnh sed -n '1p ; $p' one.txt two.txt three.txt 47. Ở dòng cuối cùng (sed -i 's/hello/world/' file.txt 87), khoảng trống được lấy ra (sed -n '1p ; $p' one.txt two.txt three.txt 75), các dòng mới bị xóa ($ echo | sed 'Q42' ; echo $? 42 37) và mọi octet còn lại được in rõ ràng (sed -i 's/hello/world/' file.txt 67). Do đó, bất kỳ chuỗi multibyte không hợp lệ nào được in dưới dạng giá trị bát phân

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

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

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

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

Lệnh thay thế của GNU sed -i 's/hello/world/' file.txt 22 (sed -n '1p ; $p' one.txt two.txt three.txt 67) hỗ trợ chuyển đổi chữ hoa/chữ thường sử dụng 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 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. Những chuyển đổi này hỗ trợ các ký tự nhiều byte

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

Thấy

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

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

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

Giải thích của họ phụ thuộc vào ngôn ngữ $ echo | sed 'Q42' ; echo $? 42 46; . hàng năm. ]]’ có nghĩa là lớp ký tự gồm số và chữ cái trong ngôn ngữ hiện tại

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

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

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

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

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

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

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

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

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

LÀM

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

Nhiều dòng có thể được xử lý như một bộ đệm bằng cách sử dụng sed -n '1p ; $p' one.txt two.txt three.txt 39,sed -n '1p ; $p' one.txt two.txt three.txt 45,______19_______47,sed -i 's/hello/world/' file.txt 71,sed -n '1p ; $p' one.txt two.txt three.txt 57. Chúng tương tự như các lệnh viết thường (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), ngoại trừ việc các lệnh này nối hoặc trừ dữ liệu trong khi tôn trọng các dòng mới được nhúng - cho phép thêm và xóa các dòng khỏi mẫu và giữ khoảng trắng

Họ hoạt động như sau

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

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

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

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

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

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

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

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

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

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

Ví dụ sau minh họa hoạt động của lệnh sed -i 's/hello/world/' file.txt 71 và sed -n '1p ; $p' one.txt two.txt three.txt 39

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

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

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

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

  1. Biểu thức đầu tiên, $ echo | sed 'Q42' ; echo $? 42 74 hoạt động trên tất cả các dòng không trống và thêm dòng hiện tại (trong không gian mẫu) vào không gian giữ. Trên tất cả các dòng ngoại trừ dòng cuối cùng, không gian mẫu bị xóa và chu kỳ được bắt đầu lại
  2. Các biểu thức khác sed -n '1p ; $p' one.txt two.txt three.txt 75 và sed -n '1p ; $p' one.txt two.txt three.txt 67 chỉ được thực hiện trên các dòng trống (i. e. ngăn cách đoạn văn). Lệnh sed -n '1p ; $p' one.txt two.txt three.txt 75 tìm nạp các dòng tích lũy từ không gian giữ trở lại không gian mẫu. Sau đó, lệnh $ 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 sẽ hoạt động trên tất cả văn bản trong đoạn văn (bao gồm cả các dòng mới được nhúng)

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

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

Để biết thêm các ví dụ được chú thích, hãy xem và

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

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ự. Các lệnh không có địa chỉ ảnh hưởng đến tất cả các dòng. Các lệnh có địa chỉ chỉ ảnh hưởng đến các dòng phù hợp. Xem và

sed -i 's/hello/world/' file.txt 22 không hỗ trợ cấu trúc $ echo | sed 'Q42' ; echo $? 42 85 điển hình. Thay vào đó, một số lệnh có thể được sử dụng làm điều kiện hoặc để thay đổi điều khiển luồng mặc định

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

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

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

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

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

Địa chỉ và biểu thức chính quy có thể được sử dụng như một câu điều kiện $ echo | sed 'Q42' ; echo $? 42 85. Nếu [addr] khớp với không gian mẫu hiện tại, hãy thực hiện (các) lệnh. Ví dụ. Lệnh $ echo | sed 'Q42' ; echo $? 42 93 có nghĩa là. nếu mẫu hiện tại khớp với biểu thức chính quy $ echo | sed 'Q42' ; echo $? 42 94 (dòng bắt đầu bằng dấu băm), thì hãy thực hiện lệnh sed -i 's/hello/world/' file.txt 36. xóa dòng mà không in và khởi động lại chu kỳ chương trình ngay lập tức

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

nhánh vô điều kiện (nghĩa là. luôn nhảy tới một nhãn, bỏ qua hoặc lặp lại các lệnh khác mà không bắt đầu lại một chu kỳ mới). Kết hợp với một địa chỉ, nhánh có thể được thực hiện có điều kiện trên các dòng phù hợp

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

nhánh có điều kiện (nghĩa là. nhảy tới nhãn) chỉ khi 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 đã thành công kể từ khi dòng đầu vào cuối cùng được đọc hoặc một nhánh có điều kiện khác được thực hiện

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

tương tự nhưng ngược lại với lệnh sed -n '1p ; $p' one.txt two.txt three.txt 83. chỉ rẽ nhánh nếu không có sự thay thế thành công nào kể từ khi dòng đầu vào cuối cùng được đọc

Hai chương trình sed -i 's/hello/world/' file.txt 22 sau đây là tương đương. Ví dụ (giả tạo) đầu tiên sử dụng lệnh sed -n '1p ; $p' one.txt two.txt three.txt 82 để bỏ qua 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 trên các dòng chứa '1'. Ví dụ thứ hai sử dụng một địa chỉ phủ định (‘. ’) để chỉ thực hiện thay thế trên các dòng mong muốn. Lệnh sed '30,35d' input.txt > output.txt 04 vẫn được thực hiện trên tất cả các dòng

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

6. 4. 1 Phân nhánh và Chu kỳ

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 và sed -n '1p ; $p' one.txt two.txt three.txt 84 có thể được theo sau bởi một nhãn (thường là một chữ cái). Nhãn được xác định bằng dấu hai chấm theo sau bởi một hoặc nhiều chữ cái (e. g. ‘. x’). Nếu nhãn bị bỏ qua, các lệnh nhánh sẽ khởi động lại chu trình. Lưu ý sự khác biệt giữa phân nhánh thành nhãn và khởi động lại chu kỳ. khi một chu kỳ được khởi động lại, trước tiên, sed -i 's/hello/world/' file.txt 22 sẽ in nội dung hiện tại của không gian mẫu, sau đó đọc dòng đầu vào tiếp theo vào không gian mẫu;

Chương trình sau đây là một no-op. Lệnh sed -n '1p ; $p' one.txt two.txt three.txt 82 (lệnh duy nhất trong chương trình) không có nhãn và do đó chỉ cần khởi động lại chu trình. Trên mỗi chu kỳ, không gian mẫu được in và dòng đầu vào tiếp theo được đọc

Ví dụ sau đây là một vòng lặp vô hạn - nó không kết thúc và không in bất cứ thứ gì. Lệnh sed -n '1p ; $p' one.txt two.txt three.txt 82 nhảy tới nhãn 'x' và một chu kỳ mới không bao giờ được bắt đầu

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

Phân nhánh thường được bổ sung bằng các lệnh sed -n '1p ; $p' one.txt two.txt three.txt 52 hoặc sed -i 's/hello/world/' file.txt 71. cả hai lệnh đều đọc dòng đầu vào tiếp theo vào không gian mẫu mà không cần đợi chu kỳ khởi động lại. Trước khi đọc dòng đầu vào tiếp theo, sed -n '1p ; $p' one.txt two.txt three.txt 52 in không gian mẫu hiện tại sau đó làm trống nó, trong khi sed -i 's/hello/world/' file.txt 71 nối một dòng mới và dòng đầu vào tiếp theo vào không gian mẫu

Xét hai ví dụ sau

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

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

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

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

sed '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

Chương trình sau sử dụng đối sánh địa chỉ '/=$/' làm điều kiện. Nếu không gian mẫu hiện tại kết thúc bằng dấu '=', nó sẽ đọc dòng đầu vào tiếp theo bằng cách sử dụng sed -i 's/hello/world/' file.txt 71, thay thế tất cả các ký tự '=' được theo sau bởi một dòng mới và phân nhánh vô điều kiện (sed -n '1p ; $p' one.txt two.txt three.txt 82) đến đầu chương trình mà không cần khởi động lại một dòng mới . Nếu không gian mẫu không kết thúc bằng '=', hành động mặc định được thực hiện. không gian mẫu được in và một chu kỳ mới được bắt đầ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 02

Đây là một chương trình thay thế với cách tiếp cận hơi khác. Trên tất cả các dòng trừ dòng cuối cùng, sed -i 's/hello/world/' file.txt 71 nối thêm dòng vào không gian mẫu. Sau đó, một lệnh thay thế sẽ loại bỏ các ngắt dòng mềm ('=' ở cuối dòng, i. e. theo sau là một dòng mới) bằng cách thay thế chúng bằng một chuỗi rỗng. nếu thay thế thành công (có nghĩa là không gian mẫu chứa một dòng cần được nối), Lệnh rẽ nhánh có điều kiện sed -n '1p ; $p' one.txt two.txt three.txt 83 nhảy đến đầu chương trình mà không hoàn thành hoặc khởi động lại chu trình. Nếu thay thế không thành công (có nghĩa là không có ngắt dòng mềm), Lệnh sed -n '1p ; $p' one.txt two.txt three.txt 83 sẽ không phân nhánh. Sau đó, sed -n '1p ; $p' one.txt two.txt three.txt 57 sẽ in nội dung không gian mẫu cho đến dòng mới đầu tiên và sed -n '1p ; $p' one.txt two.txt three.txt 39 sẽ xóa nội dung không gian mẫu cho đến dòng mới đầu tiên. (Để tìm hiểu thêm về các lệnh sed -i 's/hello/world/' file.txt 71, sed -n '1p ; $p' one.txt two.txt three.txt 57 và sed -n '1p ; $p' one.txt two.txt three.txt 39, hãy xem )

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

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

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

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

7. 1 Đường nối

Phần này sử dụng các lệnh sed -i 's/hello/world/' file.txt 71, sed -n '1p ; $p' one.txt two.txt three.txt 39 và sed -n '1p ; $p' one.txt two.txt three.txt 57 để xử lý nhiều dòng và các lệnh sed -n '1p ; $p' one.txt two.txt three.txt 82 và sed -n '1p ; $p' one.txt two.txt three.txt 83 để phân nhánh. Xem và

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

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

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

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

Nối các dòng bắt đầu bằng khoảng trắng (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 07_______10_______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. The technique moves two characters at a time, hence it is faster than more intuitive implementations

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. Tất nhiên, bản kinh này không dạy nhiều như bản trình bày dưới đây

Thuật toán được sử dụng để tăng dần sử dụng cả hai bộ đệm, vì vậy dòng được in càng sớm càng tốt và sau đó bị loại bỏ. 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

Tập lệnh này chỉ in các dòng trùng lặp, như '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 Remove All Duplicated Lines

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

As a final example, here are three scripts, of increasing complexity and speed, that implement the same function as ‘cat -s’, that is squeezing blank lines

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

sed '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

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

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

Điều này loại bỏ các dòng trống đầu và cuối. Nó cũng là nhanh nhất. Lưu ý rằng các vòng lặp hoàn toàn được thực hiện với sed -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 Hạn chế và Không hạn chế của GNU sed -i 's/hello/world/' file.txt 22

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

On the other hand, some scripts use 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

Một vấn đề khác là 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 cố gắng sử dụng các ký hiệu đối chiếu. 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. The “Document”, below, refers to any such manual or work. Any member of the public is a licensee, and is addressed as “you”. You accept the license if you copy, modify or distribute the work in a way requiring permission under copyright law

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

    A “Secondary Section” is a named appendix or a front-matter section of the Document that deals exclusively with the relationship of the publishers or authors of the Document to the Document’s overall subject (or to related matters) and contains nothing that could fall directly within that overall subject. (Thus, if the Document is in part a textbook of mathematics, a Secondary Section may not explain any mathematics. ) The relationship could be a matter of historical connection with the subject or with related matters, or of legal, commercial, philosophical, ethical or political position regarding them

    The “Invariant Sections” are certain Secondary Sections whose titles are designated, as being those of Invariant Sections, in the notice that says that the Document is released under this License. If a section does not fit the above definition of Secondary then it is not allowed to be designated as Invariant. The Document may contain zero Invariant Sections. If the Document does not identify any Invariant Sections then there are none

    “Văn bản bìa” là những đoạn văn bản ngắn nhất định được liệt kê, dưới dạng Văn bản bìa trước hoặc Văn bản bìa sau, trong thông báo nói rằng Tài liệu được phát hành theo Giấy phép này. A Front-Cover Text may be at most 5 words, and a Back-Cover Text may be at most 25 words

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

    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. SAO CHÉP NGUYÊN BẢN

    Bạn có thể sao chép và phân phối Tài liệu bằng bất kỳ phương tiện nào, thương mại hoặc phi thương mại, với điều kiện là Giấy phép này, thông báo bản quyền và thông báo cấp phép cho biết Giấy phép này áp dụng cho Tài liệu được sao chép thành tất cả các bản sao và bạn không thêm bất kỳ điều kiện nào khác . 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. Bạn có thể thêm các tài liệu khác trên bìa ngoài. Sao chép với các thay đổi giới hạn ở bìa, miễn là chúng giữ nguyên tiêu đề của Tài liệu và đáp ứng các điều kiện này, có thể được coi là sao chép nguyên văn ở các khía cạnh khác

    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. For any section Entitled “Acknowledgements” or “Dedications”, Preserve the Title of the section, and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein
    12. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles
    13. Delete any section Entitled “Endorsements”. Such a section may not be included in the Modified Version
    14. Do not retitle any existing section to be Entitled “Endorsements” or to conflict in title with any Invariant Section
    15. Preserve any Warranty Disclaimers

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

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

    Bạn có thể thêm một đoạn văn dài tối đa năm từ làm Văn bản Bìa trước và một đoạn văn tối đa 25 từ làm Văn bản Bìa sau, vào cuối danh sách Văn bản Bìa trong Phiên bản đã Sửa đổi. 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. A public wiki that anybody can edit is an example of such a server. A “Massive Multiauthor Collaboration” (or “MMC”) contained in the site means any set of copyrightable works thus published on the MMC site

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

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

    An MMC is “eligible for relicensing” if it is licensed under this License, and if all works that were first published under this License somewhere other than this MMC, and subsequently incorporated in whole or in part into the MMC, (1) had no cover texts or invariant sections, and (2) were thus incorporated prior to November 1, 2008

    The operator of an MMC Site may republish an MMC contained in the site under CC-BY-SA on the same site at any time before August 1, 2009, provided the MMC is eligible for relicensing

ADDENDUM. How to use this License for your documents

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

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

If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the “with…Texts. ” line with this

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

If you have Invariant Sections without Cover Texts, or some other combination of the three, merge those two alternatives to suit the situation

If your document contains nontrivial examples of program code, we recommend releasing these examples in parallel under your choice of free software license, such as the GNU General Public License, to permit their use in free software

Concept Index

This is a general index of all issues discussed in this manual, with the exception of the sed -i 's/hello/world/' file.txt 22 commands and command-line options

Chủ đề