Quyền được cấp để sao chép, phân phối và/hoặc sửa đổi tài liệu này theo các điều khoản của Giấy phép Tài liệu Tự do GNU, Phiên bản 1. 3 hoặc bất kỳ phiên bản nào mới hơn do Tổ chức Phần mềm Tự do xuất bản; . A copy of the license is included in the section entitled “GNU Free Documentation License”
1 Introduction
sed -i 's/hello/world/' file.txt22 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.txt24],
sed -i 's/hello/world/' file.txt22 works by making only one pass over the input[s], and is consequently more efficient. But it is
sed -i 's/hello/world/' file.txt22’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.txt22. Details of
sed -i 's/hello/world/' file.txt22 scripts and individual
sed -i 's/hello/world/' file.txt22 commands are discussed in the next chapter
2. 1 Overview
Normally
sed -i 's/hello/world/' file.txt22 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
Nếu bạn không chỉ định INPUTFILE hoặc nếu INPUTFILE là -,
sed -i 's/hello/world/' file.txt22 lọc nội dung của đầu vào tiêu chuẩn. 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.txt22 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.txt33 and
sed -i 's/hello/world/' file.txt34 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.txt22 prints all processed input [except input that has been modified/deleted by commands such as
sed -i 's/hello/world/' file.txt36]. Use -n to suppress output, and the
sed -i 's/hello/world/' file.txt37 command to print specific lines. The following command prints only line 45 of the input file
sed -i 's/hello/world/' file.txt22 treats multiple input files as one long stream. The following example prints the first line of the first file [one. txt] and the last line of the last file [three. txt]. Use -s to reverse this behavior
sed -n '1p ; $p' one.txt two.txt three.txt
Without -e or -f options,
sed -i 's/hello/world/' file.txt22 uses the first non-option parameter as the script, and the following non-option parameters as input files. If -e or -f options are used to specify a script, all non-option parameters are taken as input files. Options -e and -f can be combined, and can appear multiple times [in which case the final effective script will be concatenation of all the individual scripts]
The following examples are equivalent
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt
2. 2 Command-Line Options
The full format for invoking
sed -i 's/hello/world/' file.txt22 is
sed OPTIONS.. [SCRIPT] [INPUTFILE...]
sed -i 's/hello/world/' file.txt22 có thể được gọi với các tùy chọn dòng lệnh sau
sed -i 's/hello/world/' file.txt42
Print out the version of
sed -i 's/hello/world/' file.txt22 that is being run and a copyright notice, then exit
sed -i 's/hello/world/' file.txt44
Print a usage message briefly summarizing these command-line options and the bug-reporting address, then exit
sed -i 's/hello/world/' file.txt45
sed -i 's/hello/world/' file.txt46
sed -i 's/hello/world/' file.txt47
By default,
sed -i 's/hello/world/' file.txt22 prints out the pattern space at the end of each cycle through the script [see ]. These options disable this automatic printing, and
sed -i 's/hello/world/' file.txt22 only produces output when explicitly told to via the
sed -i 's/hello/world/' file.txt37 command
sed -i 's/hello/world/' file.txt52
Print the input sed program in canonical form, and annotate program execution
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3
sed -i 's/hello/world/' file.txt53
sed -i 's/hello/world/' file.txt54
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.txt55
sed -i 's/hello/world/' file.txt56
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.txt57
sed -i 's/hello/world/' file.txt58
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.txt22 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.txt60, 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.txt62
Same as -E -i with no backup suffix - FILE will be edited in-place without creating a backup
sed -i 's/hello/world/' file.txt63
This is equivalent to --in-place=E, creating FILEE as backup of FILE
Be cautious of using -n with -i. the former disables automatic printing of lines and the latter changes the file in-place without a backup. Used carelessly [and without an explicit
sed -i 's/hello/world/' file.txt37 command], the output file will be empty
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE
sed -i 's/hello/world/' file.txt65
sed -i 's/hello/world/' file.txt66
Specify the default line-wrap length for the
sed -i 's/hello/world/' file.txt67 command. A length of 0 [zero] means to never wrap long lines. If not specified, it is taken to be 70
sed -i 's/hello/world/' file.txt68
GNU
sed -i 's/hello/world/' file.txt22 includes several extensions to POSIX sed. In order to simplify writing portable scripts, this option disables all the extensions that this manual documents, including additional commands. Most of the extensions accept
sed -i 's/hello/world/' file.txt22 programs that are outside the syntax mandated by POSIX, but some of them [such as the behavior of the
sed -i 's/hello/world/' file.txt71 command described in ] actually violate the standard. If you want to disable only the latter kind of extension, you can set the
sed -i 's/hello/world/' file.txt72 variable to a non-empty value
sed -i 's/hello/world/' file.txt73
sed -i 's/hello/world/' file.txt74
This option is available on every platform, but is only effective where the operating system makes a distinction between text files and binary files. When such a distinction is made—as is the case for MS-DOS, Windows, Cygwin—text files are composed of lines separated by a carriage return and a line feed character, and
sed -i 's/hello/world/' file.txt22 does not see the ending CR. When this option is specified,
sed -i 's/hello/world/' file.txt22 will open input files in binary mode, thus not requesting this special processing and considering lines to end at a line feed
sed -i 's/hello/world/' file.txt77
This option is available only on platforms that support symbolic links and has an effect only if option -i is specified. In this case, if the file that is specified on the command line is a symbolic link,
sed -i 's/hello/world/' file.txt22 will follow the link and edit the ultimate destination of the link. The default behavior is to break the symbolic link, so that the link destination will not be modified
sed -i 's/hello/world/' file.txt79
sed -i 's/hello/world/' file.txt80
sed -i 's/hello/world/' file.txt81
Use extended regular expressions rather than basic regular expressions. Extended regexps are those that
sed -i 's/hello/world/' file.txt82 accepts; they can be clearer because they usually have fewer backslashes. Historically this was a GNU extension, but the -E extension has since been added to the POSIX standard [http. //austingroupbugs. net/view. php?id=528], so use -E for portability. GNU sed has accepted -E as an undocumented option for years, and *BSD seds have accepted -E for years as well, but scripts that use -E might not port to other older systems. See
sed -i 's/hello/world/' file.txt83
sed -i 's/hello/world/' file.txt84
By default,
sed -i 's/hello/world/' file.txt22 will consider the files specified on the command line as a single continuous long stream. This GNU
sed -i 's/hello/world/' file.txt22 extension allows the user to consider them as separate files. range addresses [such as ‘/abc/,/def/’] are not allowed to span several files, line numbers are relative to the start of each file,
sed -i 's/hello/world/' file.txt87 refers to the last line of each file, and files invoked from the
sed -i 's/hello/world/' file.txt88 commands are rewound at the start of each file
sed -i 's/hello/world/' file.txt89
In sandbox mode,
sed -i 's/hello/world/' file.txt90 commands are rejected - programs containing them will be aborted without being run. Sandbox mode ensures
sed -i 's/hello/world/' file.txt22 operates only on the input files designated on the command line, and cannot run external programs
sed -i 's/hello/world/' file.txt92
sed -i 's/hello/world/' file.txt93
Buffer both input and output as minimally as practical. [This is particularly useful if the input is coming from the likes of ‘tail -f’, and you wish to see the transformed output as soon as possible. ]
sed -i 's/hello/world/' file.txt94
sed -i 's/hello/world/' file.txt95
sed -i 's/hello/world/' file.txt96
Treat the input as a set of lines, each terminated by a zero byte [the ASCII ‘NUL’ character] instead of a newline. This option can be used with commands like ‘sort -z’ and ‘find -print0’ to process arbitrary file names
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
If any command-line parameters remain after processing the above, these parameters are interpreted as the names of input files to be processed. 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.txt22 returns the following exit status error values0
Hoàn thành thành công
1Lệnh không hợp lệ, cú pháp không hợp lệ, biểu thức chính quy không hợp lệ hoặc lệnh mở rộng GNU
sed -i 's/hello/world/' file.txt22 được sử dụng với --posix2
Không thể mở một hoặc nhiều tệp đầu vào được chỉ định trên dòng lệnh [e. g. nếu không tìm thấy tệp hoặc quyền đọc bị từ chối]. Processing continued with other files
4Lỗi I/O hoặc lỗi xử lý nghiêm trọng trong thời gian chạy, GNU
sed -i 's/hello/world/' file.txt22 bị hủy bỏ ngay lập tức
Ngoài ra, các lệnh
sed -n '1p ; $p' one.txt two.txt three.txt00 và
sed -n '1p ; $p' one.txt two.txt three.txt01 có thể được sử dụng để chấm dứt
sed -i 's/hello/world/' file.txt22 với giá trị mã thoát tùy chỉnh [đây là tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22]
$ echo | sed 'Q42' ; echo $? 42
3 sed -i 's/hello/world/' file.txt
22 kịch bản
3. 1 sed -i 's/hello/world/' file.txt
22 tổng quan kịch bản
Chương trình
sed -i 's/hello/world/' file.txt22 bao gồm một hoặc nhiều lệnh
sed -i 's/hello/world/' file.txt22, được truyền bởi một hoặc nhiều tùy chọn -e, -f, --expression và --file hoặc đối số không phải tùy chọn đầu tiên nếu không sử dụng tùy chọn nào trong số này. Tài liệu này sẽ đề cập đến “chữ viết”
sed -i 's/hello/world/' file.txt22; . Thấy
sed -i 's/hello/world/' file.txt22 lệnh theo cú pháp này
X là lệnh một ký tự
sed -i 's/hello/world/' file.txt22.
sed -n '1p ; $p' one.txt two.txt three.txt11 là một địa chỉ dòng tùy chọn. Nếu
sed -n '1p ; $p' one.txt two.txt three.txt11 được chỉ định, lệnh X sẽ chỉ được thực hiện trên các dòng phù hợp.
sed -n '1p ; $p' one.txt two.txt three.txt11 có thể là số dòng đơn, biểu thức chính quy hoặc dải dòng [xem phần ].
sed -n '1p ; $p' one.txt two.txt three.txt14 bổ sung được sử dụng cho một số lệnh
sed -i 's/hello/world/' file.txt22
Ví dụ sau xóa dòng 30 đến 35 trong đầu vào.
sed -n '1p ; $p' one.txt two.txt three.txt16 là một dải địa chỉ.
sed -i 's/hello/world/' file.txt36 là lệnh xóa
sed '30,35d' input.txt > output.txt
Ví dụ sau in tất cả đầu vào cho đến khi tìm thấy một dòng bắt đầu bằng từ 'foo'. Nếu dòng đó được tìm thấy,
sed -i 's/hello/world/' file.txt22 sẽ chấm dứt với trạng thái thoát 42. Nếu dòng đó không được tìm thấy [và không có lỗi nào khác xảy ra],
sed -i 's/hello/world/' file.txt22 sẽ thoát với trạng thái 0.
sed -n '1p ; $p' one.txt two.txt three.txt20 là một địa chỉ biểu thức chính quy.
sed -n '1p ; $p' one.txt two.txt three.txt00 là lệnh thoát.
sed -n '1p ; $p' one.txt two.txt three.txt22 là tùy chọn lệnh
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt0
Các lệnh trong tập lệnh hoặc tệp tập lệnh có thể được phân tách bằng dấu chấm phẩy [
sed -n '1p ; $p' one.txt two.txt three.txt23] hoặc dòng mới [ASCII 10]. Nhiều tập lệnh có thể được chỉ định với các tùy chọn -e hoặc -f
Các ví dụ sau đây đều tương đương. Họ thực hiện hai hoạt động
sed -i 's/hello/world/' file.txt22. xóa bất kỳ dòng nào khớp với biểu thức chính quy
sed -n '1p ; $p' one.txt two.txt three.txt20 và thay thế tất cả các lần xuất hiện của chuỗi 'hello' bằng 'world'
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt1
Lệnh
sed -n '1p ; $p' one.txt two.txt three.txt26,
sed -n '1p ; $p' one.txt two.txt three.txt27,
sed -n '1p ; $p' one.txt two.txt three.txt28, do cú pháp của chúng, không thể theo sau dấu chấm phẩy hoạt động như dấu phân cách lệnh và do đó nên được kết thúc bằng dòng mới hoặc được đặt ở cuối tập lệnh hoặc tệp tập lệnh. Các lệnh cũng có thể được đặt trước các ký tự khoảng trắng không quan trọng tùy chọn. Thấy
3. 2 sed -i 's/hello/world/' file.txt
22 lệnh tóm tắt
Các lệnh sau được hỗ trợ trong GNU
sed -i 's/hello/world/' file.txt22. Một số là các lệnh POSIX tiêu chuẩn, trong khi một số khác là các phần mở rộng của GNU. Chi tiết và ví dụ cho từng lệnh có trong các phần sau. [Mnemonics] được hiển thị trong ngoặc đơn
sed -n '1p ; $p' one.txt two.txt three.txt31
sed -n '1p ; $p' one.txt two.txt three.txt32
Nối văn bản sau một dòng
sed -n '1p ; $p' one.txt two.txt three.txt33
Nối văn bản sau một dòng [cú pháp thay thế]
sed -n '1p ; $p' one.txt two.txt three.txt34
Chi nhánh vô điều kiện để dán nhãn. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu
sed -n '1p ; $p' one.txt two.txt three.txt35
sed -n '1p ; $p' one.txt two.txt three.txt32
Thay thế [thay đổi] dòng bằng văn bản
sed -n '1p ; $p' one.txt two.txt three.txt37
Thay thế [thay đổi] dòng bằng văn bản [cú pháp thay thế]
sed -i 's/hello/world/' file.txt36
Xóa không gian mẫu;
sed -n '1p ; $p' one.txt two.txt three.txt39
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.txt36 đã được ban hành
sed -n '1p ; $p' one.txt two.txt three.txt41
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.txt42
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.txt43
[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.txt44
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.txt45
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.txt46
[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.txt47
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.txt48
sed -n '1p ; $p' one.txt two.txt three.txt32
chèn văn bản trước một dòng
sed -n '1p ; $p' one.txt two.txt three.txt50
chèn văn bản trước một dòng [cú pháp thay thế]
sed -i 's/hello/world/' file.txt67
In không gian mẫu ở dạng rõ ràng
sed -n '1p ; $p' one.txt two.txt three.txt52
[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.txt22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào
sed -i 's/hello/world/' file.txt71
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.txt22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào
sed -i 's/hello/world/' file.txt37
In không gian mẫu
sed -n '1p ; $p' one.txt two.txt three.txt57
In không gian mẫu, cho đến mẫu đầu tiên
sed -n '1p ; $p' one.txt two.txt three.txt58
[thoát] Thoát
sed -i 's/hello/world/' file.txt22 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.txt60
[thoát] Lệnh này giống như lệnh
sed -n '1p ; $p' one.txt two.txt three.txt00, 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.txt00, 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.txt63
Đọc tên tập tin
sed -n '1p ; $p' one.txt two.txt three.txt64
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.txt65
[substitute] Match the regular-expression against the content of the pattern space. 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.txt66
[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.txt67 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.txt68
[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.txt67 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.txt70
[phiên bản] Lệnh này không làm gì, nhưng làm cho
sed -i 's/hello/world/' file.txt22 không thành công nếu tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22 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.txt73
Viết không gian mẫu vào tên tệp
sed -n '1p ; $p' one.txt two.txt three.txt74
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.txt75
Trao đổi nội dung của không gian giữ và mẫu
sed -n '1p ; $p' one.txt two.txt three.txt76
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.txt77
[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.txt78
Một nhận xét, cho đến dòng mới tiếp theo
sed -n '1p ; $p' one.txt two.txt three.txt79
Nhóm một số lệnh lại với nhau
sed -n '1p ; $p' one.txt two.txt three.txt80
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.txt81
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.txt83,
sed -n '1p ; $p' one.txt two.txt three.txt84]
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.txt67 [để thay thế] có lẽ là lệnh quan trọng nhất trong
sed -i 's/hello/world/' file.txt22 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.txt67 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.txt67 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.txt90 [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.txt91 thứ n và kết quả trùng khớp của nó
sed -n '1p ; $p' one.txt two.txt three.txt92. 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.txt94 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.txt67 nào. Ký tự
sed -n '1p ; $p' one.txt two.txt three.txt94 [hoặc bất kỳ ký tự nào khác được sử dụng thay cho ký tự đó] chỉ có thể xuất hiện trong biểu thức chính quy hoặc ký tự thay thế nếu ký tự đó đứng trước ký tự
sed -n '1p ; $p' one.txt two.txt three.txt97
Cuối cùng, với tư cách là tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22, 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.txt99,
sed -i 's/hello/world/' file.txt67,
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.txt01,
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.txt02 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.txt03. Ý nghĩa như sau
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt04
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.txt05 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.txt06,
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.txt07
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.txt05
Chuyển từ thay thế sang chữ hoa cho đến khi tìm thấy
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt04 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.txt06,
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.txt11
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.txt06
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.txt04 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.txt05
Khi cờ
sed -n '1p ; $p' one.txt two.txt three.txt44 đang được sử dụng, chuyển đổi kiểu chữ không lan truyền từ lần xuất hiện này sang lần xuất hiện khác của biểu thức chính quy. Ví dụ: khi lệnh sau được thực thi với 'a-b-' trong không gian mẫu
đầu ra là 'axxB'. Khi thay thế '-' đầu tiên, chuỗi '\ u' chỉ ảnh hưởng đến thay thế trống của '\ 1'. Nó không ảnh hưởng đến ký tự
sed -n '1p ; $p' one.txt two.txt three.txt75 được thêm vào không gian mẫu khi thay thế
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt17 bằng
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt18
Mặt khác,
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt07 và
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt11 ảnh hưởng đến phần còn lại của văn bản thay thế nếu theo sau chúng là một từ thay thế trống. Với 'a-b-' trong không gian mẫu, lệnh sau
sẽ thay thế '-' bằng 'X' [chữ hoa] và 'b-' bằng 'Bx'. Nếu hành vi này là không mong muốn, bạn có thể ngăn chặn nó bằng cách thêm một chuỗi '\E'—sau '\1' trong trường hợp này
Để bao gồm một ký tự
sed -n '1p ; $p' one.txt two.txt three.txt97,
sed -n '1p ; $p' one.txt two.txt three.txt93 hoặc dòng mới trong phần thay thế cuối cùng, hãy đảm bảo đặt trước
sed -n '1p ; $p' one.txt two.txt three.txt97,
sed -n '1p ; $p' one.txt two.txt three.txt93 mong muốn hoặc dòng mới trong phần thay thế bằng một ____19_______97
Lệnh
sed -n '1p ; $p' one.txt two.txt three.txt67 có thể được theo sau bởi 0 hoặc nhiều cờ sau
sed -n '1p ; $p' one.txt two.txt three.txt44
Áp dụng thay thế cho tất cả các kết quả phù hợp với biểu thức chính quy, không chỉ lần đầu tiên
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt28
Chỉ thay thế trận đấu số của regrec
tương tác trong lệnh
sed -n '1p ; $p' one.txt two.txt three.txt67 Lưu ý. tiêu chuẩn POSIX không chỉ định điều gì sẽ xảy ra khi bạn kết hợp công cụ sửa đổi số và số
sed -n '1p ; $p' one.txt two.txt three.txt44 và hiện tại không có ý nghĩa thống nhất rộng rãi trong việc triển khai
sed -i 's/hello/world/' file.txt22. Đối với GNU
sed -i 's/hello/world/' file.txt22, tương tác được định nghĩa là. bỏ qua các kết quả khớp trước số, sau đó khớp và thay thế tất cả các kết quả khớp từ số trở đi
sed -i 's/hello/world/' file.txt37
Nếu thay thế đã được thực hiện, sau đó in không gian mẫu mới
Ghi chú. khi cả hai tùy chọn
sed -i 's/hello/world/' file.txt37 và
sed -n '1p ; $p' one.txt two.txt three.txt41 được chỉ định, thứ tự tương đối của hai tùy chọn tạo ra kết quả rất khác nhau. Nói chung,
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt36 [đánh giá rồi in] là điều bạn muốn, nhưng thao tác ngược lại có thể hữu ích cho việc gỡ lỗi. Vì lý do này, phiên bản hiện tại của GNU
sed -i 's/hello/world/' file.txt22 diễn giải một cách đặc biệt sự hiện diện của các tùy chọn
sed -i 's/hello/world/' file.txt37 cả trước và sau
sed -n '1p ; $p' one.txt two.txt three.txt41, in không gian mẫu trước và sau khi đánh giá, trong khi nói chung các cờ cho lệnh
sed -n '1p ; $p' one.txt two.txt three.txt67 chỉ hiển thị tác dụng của chúng một lần. This behavior, although documented, might change in future versions
sed -n '1p ; $p' one.txt two.txt three.txt73
Nếu thay thế được thực hiện, sau đó ghi kết quả vào tệp được đặt tên. Là một tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22, hai giá trị đặc biệt của tên tệp được hỗ trợ. /dev/stderr, ghi kết quả vào lỗi tiêu chuẩn và /dev/stdout, ghi vào đầu ra tiêu chuẩn
sed -n '1p ; $p' one.txt two.txt three.txt41
Lệnh này cho phép một người chuyển đầu vào từ lệnh shell sang không gian mẫu. Nếu thay thế được thực hiện, lệnh được tìm thấy trong không gian mẫu được thực thi và không gian mẫu được thay thế bằng đầu ra của nó. Một dòng mới ở cuối bị chặn; . Đây là tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22
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.txt45_______19_______28
Công cụ sửa đổi
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt45 cho đối sánh biểu thức chính quy là một tiện ích mở rộng GNU làm cho _________22 đối sánh biểu thức chính quy theo cách không phân biệt chữ hoa chữ thường
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt49
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.txt50
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.txt49 để khớp biểu thức chính quy là một tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22 hướng GNU
sed -i 's/hello/world/' file.txt22 khớp biểu thức chính quy ở chế độ nhiều dòng. Công cụ sửa đổi làm cho
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 và
sed -i 's/hello/world/' file.txt87 khớp tương ứng [ngoài hành vi bình thường] chuỗi trống sau một dòng mới và chuỗi trống trước một dòng mới. Có các chuỗi ký tự đặc biệt [
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt56 và
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt57] luôn khớp với phần đầu hoặc phần cuối của bộ đệm. Ngoài ra, ký tự dấu chấm không khớp với ký tự dòng mới ở chế độ nhiều dòng
3. 4 lệnh thường dùng
Nếu bạn hoàn toàn sử dụng
sed -i 's/hello/world/' file.txt22, rất có thể bạn sẽ muốn biết các lệnh này
sed -n '1p ; $p' one.txt two.txt three.txt78
[Không cho phép địa chỉ. ]
Ký tự
sed -n '1p ; $p' one.txt two.txt three.txt78 bắt đầu nhận xét;
Nếu bạn lo lắng về tính di động, hãy lưu ý rằng một số triển khai của
sed -i 's/hello/world/' file.txt22 [không tuân thủ POSIX] chỉ có thể hỗ trợ một nhận xét một dòng và sau đó chỉ khi ký tự đầu tiên của tập lệnh là một
sed -n '1p ; $p' one.txt two.txt three.txt78
Cảnh báo. nếu hai ký tự đầu tiên của tập lệnh
sed -i 's/hello/world/' file.txt22 là
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt64, thì tùy chọn -n [no-autoprint] là bắt buộc. 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.txt65
Thoát
sed -i 's/hello/world/' file.txt22 mà không xử lý thêm bất kỳ lệnh hoặc đầu vào nào
Thí dụ. dừng lại sau khi in dòng thứ hai
Lệnh này chỉ chấp nhận một địa chỉ. Note that the current pattern space is printed if auto-print is not disabled with the -n options. Khả năng trả lại mã thoát từ tập lệnh
sed -i 's/hello/world/' file.txt22 là một tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22
Xem thêm lệnh GNU
sed -i 's/hello/world/' file.txt22 tiện ích mở rộng
sed -n '1p ; $p' one.txt two.txt three.txt01 thoát âm thầm mà không in không gian mẫu hiện tại
sed -i 's/hello/world/' file.txt36
Xóa không gian mẫu;
Example. xóa dòng đầu vào thứ hai
sed -i 's/hello/world/' file.txt37
In ra không gian mẫu [đến đầu ra tiêu chuẩn]. This command is usually only used in conjunction with the -n command-line option
Thí dụ. print only the second input line
sed -n '1p ; $p' one.txt two.txt three.txt52
If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. Nếu không có thêm thông tin đầu vào nào thì
sed -i 's/hello/world/' file.txt22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào
Lệnh này rất hữu ích để bỏ qua các dòng [e. g. process every Nth line]
Example. thực hiện thay thế trên mỗi dòng thứ 3 [i. e. two
sed -n '1p ; $p' one.txt two.txt three.txt52 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.txt2
GNU
sed -i 's/hello/world/' file.txt22 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.txt3
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.txt77
Một nhóm lệnh có thể được đặt trong khoảng từ
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt78 đế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.txt79 ký tự. 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.txt4
3. 5 Less Frequently-Used Commands
Mặc dù có lẽ ít được sử dụng hơn so với các lệnh trong phần trước, một số tập lệnh
sed -i 's/hello/world/' file.txt22 rất nhỏ nhưng hữu ích có thể được tạo bằng các lệnh này
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt81
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. phiên âm 'a-j' thành '0-9'
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt5
[Các ký tự
sed -n '1p ; $p' one.txt two.txt three.txt94 có thể được thay thế thống nhất bằng bất kỳ ký tự đơn nào khác trong bất kỳ lệnh
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt83 nào. ]
Các trường hợp của
sed -n '1p ; $p' one.txt two.txt three.txt94 [hoặc bất kỳ ký tự nào khác được sử dụng thay cho nó],
sed -n '1p ; $p' one.txt two.txt three.txt97 hoặc các dòng mới có thể xuất hiện trong danh sách ký tự nguồn hoặc ký tự đích, với điều kiện là mỗi trường hợp được thoát bởi một ____19_______97. 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.txt87 command from GNU coreutils for similar functionality
sed -n '1p ; $p' one.txt two.txt three.txt33
Nối văn bản sau một dòng. This is a GNU extension to the standard
sed -n '1p ; $p' one.txt two.txt three.txt26 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.txt6
Khoảng trắng phía trước sau lệnh
sed -n '1p ; $p' one.txt two.txt three.txt26 bị bỏ qua. Văn bản cần thêm được đọc cho đến cuối dòng
sed -n '1p ; $p' one.txt two.txt three.txt31
sed -n '1p ; $p' one.txt two.txt three.txt32
Nối văn bản sau một dòng
Example. Thêm 'xin chào' sau dòng thứ hai [-. 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.txt7
The
sed -n '1p ; $p' one.txt two.txt three.txt26 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.txt97, which are removed from the output] to be output at the end of the current cycle, or when the next input line is read
As a GNU extension, this command accepts two addresses
Trình tự thoát trong văn bản được xử lý, vì vậy bạn nên sử dụng
sed '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.txt95 trong văn bản để in một dấu gạch chéo ngược
The commands resume after the last line without a backslash [
sed -n '1p ; $p' one.txt two.txt three.txt97] - ‘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.txt8
As a GNU extension, the
sed -n '1p ; $p' one.txt two.txt three.txt26 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.txt98 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.txt9
sed -n '1p ; $p' one.txt two.txt three.txt50
insert text before a line. This is a GNU extension to the standard
sed -n '1p ; $p' one.txt two.txt three.txt28 command - see below for details
Example. Chèn từ 'xin chào' trước dòng thứ hai
sed -i 's/hello/world/' file.txt0
Khoảng trắng phía trước sau lệnh
sed -n '1p ; $p' one.txt two.txt three.txt28 bị bỏ qua. Văn bản cần thêm được đọc cho đến cuối dòng
sed -n '1p ; $p' one.txt two.txt three.txt48
sed -n '1p ; $p' one.txt two.txt three.txt32
Xuất ngay các dòng văn bản theo lệnh này
Example. Chèn 'xin chào' trước dòng thứ hai [-. cho biết các dòng đầu ra được in]
sed -i 's/hello/world/' file.txt1
As a GNU extension, this command accepts two addresses
Trình tự thoát trong văn bản được xử lý, vì vậy bạn nên sử dụng
sed '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.txt95 trong văn bản để in một dấu gạch chéo ngược
The commands resume after the last line without a backslash [
sed -n '1p ; $p' one.txt two.txt three.txt97] - ‘world’ in the following example
sed -i 's/hello/world/' file.txt2
Là một phần mở rộng của GNU, lệnh và văn bản
sed -n '1p ; $p' one.txt two.txt three.txt28 có thể được tách thành hai tham số
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt98, cho phép tạo tập lệnh dễ dàng hơn
sed -i 's/hello/world/' file.txt3
sed -n '1p ; $p' one.txt two.txt three.txt37
Replaces the line[s] with text. This is a GNU extension to the standard
sed -n '1p ; $p' one.txt two.txt three.txt27 command - see below for details
Example. Replace the 2nd to 9th lines with the word ‘hello’
sed -i 's/hello/world/' file.txt4
Khoảng trắng phía trước sau lệnh
sed -n '1p ; $p' one.txt two.txt three.txt27 bị bỏ qua. Văn bản cần thêm được đọc cho đến cuối dòng
sed -n '1p ; $p' one.txt two.txt three.txt35
sed -n '1p ; $p' one.txt two.txt three.txt32
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.txt5
If no addresses are given, each line is replaced
Một chu kỳ mới được bắt đầu sau khi lệnh này được thực hiện, vì không gian mẫu sẽ bị xóa. Trong ví dụ sau,
sed -n '1p ; $p' one.txt two.txt three.txt27 bắt đầu một chu kỳ mới và lệnh thay thế không được thực hiện trên văn bản đã thay thế
sed -i 's/hello/world/' file.txt6
Là một phần mở rộng của GNU, lệnh và văn bản
sed -n '1p ; $p' one.txt two.txt three.txt27 có thể được tách thành hai tham số
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt98, cho phép tạo tập lệnh dễ dàng hơn
sed -i 's/hello/world/' file.txt7
sed -n '1p ; $p' one.txt two.txt three.txt80
Print out the current input line number [with a trailing newline]
sed -i 's/hello/world/' file.txt8
As a GNU extension, this command accepts two addresses
sed OPTIONS.. [SCRIPT] [INPUTFILE...]17
In không gian mẫu ở dạng rõ ràng. các ký tự không in được [và ký tự
sed -n '1p ; $p' one.txt two.txt three.txt97] đượ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. Tham số n là phần mở rộng GNU
sed -i 's/hello/world/' file.txt22
sed -n '1p ; $p' one.txt two.txt three.txt63
Đọc tên tập tin. Thí dụ
sed -i 's/hello/world/' file.txt9
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.txt22 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.txt73
Write the pattern space to filename. Là một tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22, hai giá trị đặc biệt của tên tệp được hỗ trợ. /dev/stderr, which writes the result to the standard error, and /dev/stdout, which writes to the standard output
Tệp sẽ được tạo [hoặc cắt bớt] trước khi dòng đầu vào đầu tiên được đọc;
sed -n '1p ; $p' one.txt two.txt three.txt39
If pattern space contains no newline, start a normal new cycle as if the
sed -i 's/hello/world/' file.txt36 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.txt71
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.txt22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào
When -z is used, a zero byte [the ascii ‘NUL’ character] is added between the lines [instead of a new line]
By default
sed -i 's/hello/world/' file.txt22 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.txt57
Print out the portion of the pattern space up to the first newline
sed -n '1p ; $p' one.txt two.txt three.txt46
Replace the contents of the hold space with the contents of the pattern space
sed -n '1p ; $p' one.txt two.txt three.txt47
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.txt44
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.txt45
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.txt75
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.txt22, and these commands can enable one to write quite convoluted scripts
sed -n '1p ; $p' one.txt two.txt three.txt81
[Không cho phép địa chỉ. ]
Specify the location of label for branch commands. In all other respects, a no-op
sed -n '1p ; $p' one.txt two.txt three.txt34
Unconditionally branch to label. 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.txt66
Chỉ nhánh để gắn nhãn nếu đã có một sự thay thế
sed -n '1p ; $p' one.txt two.txt three.txt67 thành công kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện đã được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu
3. 7 Lệnh dành riêng cho GNU sed -i 's/hello/world/' file.txt
22
Các lệnh này dành riêng cho GNU
sed -i 's/hello/world/' file.txt22, vì vậy bạn phải cẩn thận khi sử dụng chúng và chỉ khi bạn chắc chắn rằng việc cản trở tính di động không phải là điều xấu. Chúng cho phép bạn kiểm tra các tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22 hoặc thực hiện các tác vụ được yêu cầu khá thường xuyên, nhưng không được hỗ trợ bởi các
sed -i 's/hello/world/' file.txt22 tiêu chuẩn
sed OPTIONS.. [SCRIPT] [INPUTFILE...]51
Lệnh này cho phép một người chuyển đầu vào từ lệnh shell sang không gian mẫu. Không có tham số, lệnh
sed -n '1p ; $p' one.txt two.txt three.txt41 thực thi lệnh được tìm thấy trong không gian mẫu và thay thế không gian mẫu bằng đầu ra;
Thay vào đó, nếu một tham số được chỉ định, lệnh
sed -n '1p ; $p' one.txt two.txt three.txt41 diễn giải nó thành một lệnh và gửi đầu ra của nó đến luồng đầu ra. Lệnh có thể chạy trên nhiều dòng, trừ dòng cuối cùng có dấu gạch chéo ngược
Trong cả hai trường hợp, kết quả không được xác định nếu lệnh được thực thi chứa ký tự NUL
Lưu ý rằng, không giống như lệnh
sed OPTIONS.. [SCRIPT] [INPUTFILE...]54, đầu ra của lệnh sẽ được in ngay lập tức;
sed -n '1p ; $p' one.txt two.txt three.txt43
In ra tên tệp của tệp đầu vào hiện tại [với một dòng mới ở cuối]
sed OPTIONS.. [SCRIPT] [INPUTFILE...]57
Lệnh này chỉ chấp nhận một địa chỉ
Lệnh này giống như lệnh
sed -n '1p ; $p' one.txt two.txt three.txt00, 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.txt00, nó cung cấp khả năng trả lại mã thoát cho người gọi
Lệnh này có thể hữu ích vì các cách thay thế duy nhất để thực hiện chức năng có vẻ tầm thường này là sử dụng tùy chọn -n [có thể làm phức tạp tập lệnh của bạn một cách không cần thiết] hoặc sử dụng đoạn mã sau, điều này làm lãng phí thời gian bằng cách đọc toàn bộ tệp mà không có bất kỳ hiệu ứng rõ ràng nào
sed -n '1p ; $p' one.txt two.txt three.txt0
sed -n '1p ; $p' one.txt two.txt three.txt64
Xếp hàng một dòng tên tệp sẽ được đọc và chèn vào luồng đầu ra ở cuối chu kỳ hiện tại hoặc khi dòng đầu vào tiếp theo được đọc. Lưu ý rằng nếu tên tệp không thể đọc được hoặc nếu đã đến cuối thì không có dòng nào được thêm vào mà không có bất kỳ dấu hiệu lỗi nào
Như với lệnh
sed OPTIONS.. [SCRIPT] [INPUTFILE...]54, giá trị đặc biệt /dev/stdin được hỗ trợ cho tên tệp, đọc một dòng từ đầu vào tiêu chuẩn
sed -n '1p ; $p' one.txt two.txt three.txt68
Chỉ phân nhánh để gắn nhãn nếu không có thay thế
sed -n '1p ; $p' one.txt two.txt three.txt67 thành công nào kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu
sed OPTIONS.. [SCRIPT] [INPUTFILE...]64
Lệnh này không làm gì, nhưng làm cho
sed -i 's/hello/world/' file.txt22 không thành công nếu các tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22 không được hỗ trợ, đơn giản vì các phiên bản khác của
sed -i 's/hello/world/' file.txt22 không triển khai lệnh này. In addition, you can specify the version of
sed -i 's/hello/world/' file.txt22 that your script requires, such as
sed OPTIONS.. [SCRIPT] [INPUTFILE...]69. Giá trị mặc định là
sed OPTIONS.. [SCRIPT] [INPUTFILE...]70 vì đó là phiên bản đầu tiên triển khai lệnh này
Lệnh này cho phép tất cả các tiện ích mở rộng GNU ngay cả khi
sed -i 's/hello/world/' file.txt72 được đặt trong môi trường
sed -n '1p ; $p' one.txt two.txt three.txt74
Ghi vào tên tệp đã cho phần không gian mẫu cho đến dòng mới đầu tiên. Mọi thứ được nói theo lệnh
sed OPTIONS.. [SCRIPT] [INPUTFILE...]26 về xử lý tệp cũng được giữ ở đây
sed -n '1p ; $p' one.txt two.txt three.txt77
Lệnh này làm trống nội dung của không gian mẫu. Nó thường giống như ‘s/. *//', nhưng hiệu quả hơn và hoạt động khi có chuỗi nhiều byte không hợp lệ trong luồng đầu vào. POSIX yêu cầu các chuỗi như vậy không khớp với '. ’, do đó không có cách di động nào để xóa bộ đệm của
sed -i 's/hello/world/' file.txt22 ở giữa tập lệnh ở hầu hết các ngôn ngữ nhiều byte [bao gồm cả ngôn ngữ UTF-8]
3. 8 Cú pháp nhiều lệnh
Có một số phương pháp để chỉ định nhiều lệnh trong chương trình
sed -i 's/hello/world/' file.txt22
Sử dụng dòng mới là cách tự nhiên nhất khi chạy tập lệnh sed từ tệp [sử dụng tùy chọn -f]
On the command line, all
sed -i 's/hello/world/' file.txt22 commands may be separated by newlines. Ngoài ra, bạn có thể chỉ định từng lệnh làm đối số cho tùy chọn -e
sed -n '1p ; $p' one.txt two.txt three.txt1
Dấu chấm phẩy [‘;’] có thể được sử dụng để phân tách hầu hết các lệnh đơn giản
sed -n '1p ; $p' one.txt two.txt three.txt2
Các lệnh
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt78,
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.txt79,
sed -n '1p ; $p' one.txt two.txt three.txt82,_______19_______83,
sed -n '1p ; $p' one.txt two.txt three.txt84,
sed OPTIONS.. [SCRIPT] [INPUTFILE...]83 có thể được phân tách bằng dấu chấm phẩy [đây là tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22 không di động]
sed -n '1p ; $p' one.txt two.txt three.txt3
Các nhãn được sử dụng trong các lệnh
sed -n '1p ; $p' one.txt two.txt three.txt82,
sed -n '1p ; $p' one.txt two.txt three.txt83,
sed -n '1p ; $p' one.txt two.txt three.txt84,
sed OPTIONS.. [SCRIPT] [INPUTFILE...]83 được đọc cho đến khi có dấu chấm phẩy. Khoảng trắng ở đầu và cuối bị bỏ qua. Trong các ví dụ bên dưới nhãn là 'x'. Ví dụ đầu tiên hoạt động với GNU
sed -i 's/hello/world/' file.txt22. Thứ hai là tương đương di động. Để biết thêm thông tin về phân nhánh và nhãn, hãy xem
sed -n '1p ; $p' one.txt two.txt three.txt4
3. 8. 1 Lệnh Yêu cầu một dòng mới
Các lệnh sau không thể được phân tách bằng dấu chấm phẩy và yêu cầu một dòng mới
sed -n '1p ; $p' one.txt two.txt three.txt26,______19_______27,
sed -n '1p ; $p' one.txt two.txt three.txt28 [nối thêm/thay đổi/chèn]
Tất cả các ký tự theo sau lệnh
sed -n '1p ; $p' one.txt two.txt three.txt26,
sed -n '1p ; $p' one.txt two.txt three.txt27,
sed -n '1p ; $p' one.txt two.txt three.txt28 được lấy làm văn bản để thêm/thay đổi/chèn. Sử dụng dấu chấm phẩy dẫn đến kết quả không mong muốn
sed -n '1p ; $p' one.txt two.txt three.txt5
Tách các lệnh bằng cách sử dụng -e hoặc một dòng mới
sed -n '1p ; $p' one.txt two.txt three.txt6
Lưu ý rằng việc chỉ định văn bản để thêm ['Xin chào'] ngay sau
sed -n '1p ; $p' one.txt two.txt three.txt26,
sed -n '1p ; $p' one.txt two.txt three.txt27,
sed -n '1p ; $p' one.txt two.txt three.txt28 chính nó là một tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22. Một giải pháp thay thế di động, tuân thủ POSIX là
sed -n '1p ; $p' one.txt two.txt three.txt7
sed -n '1p ; $p' one.txt two.txt three.txt78 [bình luận]
All characters following ‘#’ until the next newline are ignored
sed -n '1p ; $p' one.txt two.txt three.txt8
sed OPTIONS.. [SCRIPT] [INPUTFILE...]54,______0_______88,
sed OPTIONS.. [SCRIPT] [INPUTFILE...]26,
sed -i 's/hello/world/' file.txt33 [đọc và ghi tệp]
Các lệnh
sed OPTIONS.. [SCRIPT] [INPUTFILE...]54,
sed -i 's/hello/world/' file.txt88,
sed OPTIONS.. [SCRIPT] [INPUTFILE...]26,
sed -i 's/hello/world/' file.txt33 phân tích cú pháp tên tệp cho đến cuối dòng. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào tên tệp, dẫn đến kết quả không mong muốn
sed -n '1p ; $p' one.txt two.txt three.txt9
Lưu ý rằng
sed -i 's/hello/world/' file.txt22 âm thầm bỏ qua lỗi đọc/ghi trong các lệnh
sed OPTIONS.. [SCRIPT] [INPUTFILE...]54,
sed -i 's/hello/world/' file.txt88,
sed OPTIONS.. [SCRIPT] [INPUTFILE...]26,
sed -i 's/hello/world/' file.txt33 [chẳng hạn như các tệp bị thiếu]. Trong ví dụ sau,
sed -i 's/hello/world/' file.txt22 cố gắng đọc một tệp có tên 'hello. txt ; N’. Tệp bị thiếu và lỗi được âm thầm bỏ qua
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt0
sed -n '1p ; $p' one.txt two.txt three.txt41 [thực thi lệnh]
Any characters following the
sed -n '1p ; $p' one.txt two.txt three.txt41 command until the end of the line will be sent to the shell. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào lệnh trình bao, dẫn đến kết quả không mong muốn
sed '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.txt1
$ 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: 317 [substitute with
sed -n '1p ; $p' one.txt two.txt three.txt41 or
sed OPTIONS.. [SCRIPT] [INPUTFILE...]26 flags]
Trong một lệnh thay thế, cờ
sed OPTIONS.. [SCRIPT] [INPUTFILE...]26 ghi kết quả thay thế vào một tệp và cờ
sed -n '1p ; $p' one.txt two.txt three.txt41 thực thi kết quả thay thế dưới dạng lệnh trình bao. Như với các lệnh
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 322, chúng phải được kết thúc bằng một dòng mới. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào lệnh shell hoặc tên tệp, dẫn đến kết quả không mong muốn
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt2
4 Addresses. chọn dòng
4. 1 Addresses overview
Các địa chỉ xác định [các] dòng lệnh
sed -i 's/hello/world/' file.txt22 sẽ được thực thi. 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.txt3
Nếu không có địa chỉ nào được đưa ra, lệnh được thực hiện trên tất cả các dòng. Lệnh sau thay thế từ 'xin chào' bằng 'thế giới' trên tất cả các dòng trong tệp đầu vào
sed 's/hello/world/' input.txt > output.txt
Địa chỉ có thể chứa các biểu thức chính quy để khớp các dòng dựa trên nội dung thay vì số dòng. Lệnh sau chỉ thay thế từ 'hello' bằng 'world' trong các dòng có chứa từ 'apple'
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt5
Một dải địa chỉ được chỉ định với hai địa chỉ được phân tách bằng dấu phẩy [
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 324]. Địa chỉ có thể là số, biểu thức chính quy hoặc kết hợp cả hai. Lệnh sau thay thế từ 'xin chào' bằng 'thế giới' chỉ trong các dòng 4 đến 17 [đã bao gồm]
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt6
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: 325 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: 325 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.txt7
Lệnh sau chỉ thay thế từ 'hello' bằng 'world' trong các dòng từ 1 đến 3 và 18 cho đến dòng cuối cùng của tệp đầu vào [i. e. trừ dòng 4 đến 17]
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt8
4. 2 Chọn dòng theo số
Địa chỉ trong tập lệnh
sed -i 's/hello/world/' file.txt22 có thể ở bất kỳ dạng nào sau đây
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt28
Chỉ định một số dòng sẽ chỉ khớp với dòng đó trong đầu vào. [Note that
sed -i 's/hello/world/' file.txt22 counts lines continuously across all input files unless -i or -s options are specified. ]
sed -i 's/hello/world/' file.txt87
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: 331
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]. Do đó, người ta sẽ sử dụng
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 332 để chọn các dòng đánh số lẻ và
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 333 cho các dòng đánh số chẵn;
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.txt9
4. 3 selecting lines by text matching
GNU
sed -i 's/hello/world/' file.txt22 hỗ trợ các địa chỉ biểu thức chính quy sau. Biểu thức chính quy mặc định là. Nếu các tùy chọn -E hoặc -r được sử dụng, Biểu thức chính quy phải theo cú pháp. Thấy
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 336
Điều này sẽ chọn bất kỳ dòng nào khớp với biểu thức chính quy. If regexp itself includes any
sed -n '1p ; $p' one.txt two.txt three.txt94 characters, each must be escaped by a backslash [
sed -n '1p ; $p' one.txt two.txt three.txt97]
The following command prints lines in /etc/passwd which end with ‘bash’
sed OPTIONS.. [SCRIPT] [INPUTFILE...]0
Biểu thức chính quy trống '//' lặp lại kết quả khớp biểu thức chính quy cuối cùng [tương tự nếu biểu thức chính quy trống được truyền cho lệnh
sed -n '1p ; $p' one.txt two.txt three.txt67]. Lưu ý rằng các công cụ sửa đổi cho biểu thức chính quy được đánh giá khi biểu thức chính quy được biên dịch, do đó, việc chỉ định chúng cùng với biểu thức chính quy trống là không hợp lệ
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 340
[Có thể thay thế
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 341 bằng bất kỳ ký tự đơn nào khác. ]
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.txt94. Điều này đặc biệt hữu ích nếu bản thân biểu thức chính quy chứa nhiều dấu gạch chéo, vì nó tránh được việc thoát tẻ nhạt của mỗi
sed -n '1p ; $p' one.txt two.txt three.txt94. Nếu bản thân biểu thức chính quy bao gồm bất kỳ ký tự phân cách nào, thì mỗi ký tự đó phải được thoát bằng dấu gạch chéo ngược [
sed -n '1p ; $p' one.txt two.txt three.txt97]
Các lệnh sau đây là tương đương. They print lines which start with ‘/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: 345
$ 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: 346
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.txt45 để khớp biểu thức chính quy là một tiện ích mở rộng GNU làm cho biểu thức chính quy được khớp theo cách không phân biệt chữ hoa chữ thường
Trong nhiều ngôn ngữ lập trình khác, chữ thường
sed -n '1p ; $p' one.txt two.txt three.txt28 đượ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. However, in
sed -i 's/hello/world/' file.txt22 the
sed -n '1p ; $p' one.txt two.txt three.txt28 is used for the insert command [see ]
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: 351 là địa chỉ. regular expression 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.txt45 modifier.
sed -i 's/hello/world/' file.txt36 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: 354 là địa chỉ. a regular expression.
sed -n '1p ; $p' one.txt two.txt three.txt28 is the insert command.
sed -i 's/hello/world/' file.txt36 là giá trị cần chèn. A line with ‘d’ is then inserted above the matched line
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: 357____35_______58
Công cụ sửa đổi
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt49 để khớp biểu thức chính quy là một tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22 hướng GNU
sed -i 's/hello/world/' file.txt22 khớp biểu thức chính quy ở chế độ nhiều dòng. Công cụ sửa đổi làm cho
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 và
sed -i 's/hello/world/' file.txt87 khớp tương ứng [ngoài hành vi bình thường] chuỗi trống sau một dòng mới và chuỗi trống trước một dòng mới. Có các chuỗi ký tự đặc biệt [
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt56 và
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt57] luôn khớp với phần đầu hoặc phần cuối của bộ đệm. Ngoài ra, ký tự dấu chấm không khớp với ký tự dòng mới ở chế độ nhiều dòng
Địa chỉ regex hoạt động trên nội dung của không gian mẫu hiện tại. Nếu không gian mẫu bị thay đổi [ví dụ: với lệnh
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 366], khớp biểu thức chính quy sẽ hoạt động trên văn bản đã thay đổi
In the following example, automatic printing is disabled with -n. 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: 367 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: 368 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: 369, 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: 324]. An address range matches lines starting from where the first address matches, and continues until the second address matches [inclusively]
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.txt22 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: 372
Một số dòng của
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 373 có thể được sử dụng trong một đặc tả địa chỉ như
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 372 để
sed -i 's/hello/world/' file.txt22 cũng sẽ cố khớp với biểu thức chính quy trong dòng đầu vào đầu tiên. Nói cách khác,
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 372 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: 377, 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: 372 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: 377 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: 373 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: 382
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: 383
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: 30
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.txt22, 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.txt22 được chỉ định giữa hai dấu gạch chéo
Lệnh sau in các dòng có chứa từ 'hello'
Ví dụ trên tương đương với lệnh
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 387 này
Sức mạnh của biểu thức chính quy đến từ khả năng bao gồm các lựa chọn thay thế và lặp lại trong mẫu. Chúng được mã hóa trong mẫu bằng cách sử dụng các ký tự đặc biệt, không đại diện cho chính chúng mà thay vào đó được diễn giải theo một cách đặc biệt nào đó
Ký tự
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 [dấu mũ] trong biểu thức chính quy khớp với đầu dòng. Ký tự
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 389 [dấu chấm] khớp với bất kỳ ký tự đơn nào. Lệnh
sed -i 's/hello/world/' file.txt22 sau khớp và in các dòng bắt đầu bằng chữ 'b', theo sau là bất kỳ ký tự đơn nào, tiếp theo là chữ 'd'
$ 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: 31
Các phần sau giải thích ý nghĩa và cách sử dụng các ký tự đặc biệt trong biểu thức chính quy
5. 2 Biểu thức chính quy cơ bản [BRE] và mở rộng [ERE]
Biểu thức chính quy cơ bản và mở rộng là hai biến thể về cú pháp của mẫu đã chỉ định. Basic Regular Expression [BRE] syntax is the default in
sed -i 's/hello/world/' file.txt22 [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: 387]. Sử dụng tùy chọn -E do POSIX chỉ định [-r, --regexp-extended] để bật cú pháp Biểu thức chính quy mở rộng [ERE]
Trong GNU
sed -i 's/hello/world/' file.txt22, sự khác biệt duy nhất giữa biểu thức chính quy cơ bản và mở rộng là ở hành vi của một vài ký tự đặc biệt. ‘?’, ‘+’, dấu ngoặc đơn, dấu ngoặc nhọn [‘{}’] và ‘. ’
Với cú pháp cơ bản [BRE], các ký tự này không có ý nghĩa đặc biệt trừ khi được đặt trước bằng dấu gạch chéo ngược ['\']; . các ký tự này là đặc biệt trừ khi chúng có tiền tố là dấu gạch chéo ngược ['\']
Mẫu mong muốn Cơ bản [BRE] Cú phápMở rộng [ERE] Cú phápChữ ‘+’ [dấu cộng]$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 32
$ 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: 33Một hoặc nhiều ký tự 'a' theo sau là 'b' [dấu cộng là siêu ký tự đặc biệt]
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 34
$ 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: 35
5. 3 Tổng quan về cú pháp biểu thức chính quy cơ bản
Dưới đây là mô tả ngắn gọn về cú pháp biểu thức chính quy như được sử dụng trong
sed -i 's/hello/world/' file.txt22
$ 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: 395
Một ký tự thông thường duy nhất khớp với chính nó
sed -i 's/hello/world/' file.txt60
Khớp một chuỗi gồm 0 hoặc nhiều trường hợp khớp cho biểu thức chính quy trước đó, phải là một ký tự thông thường, một ký tự đặc biệt đứng trước
sed -n '1p ; $p' one.txt two.txt three.txt97, mộ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: 389, một biểu thức chính quy được nhóm [xem bên dưới] hoặc một biểu thức ngoặc. Là một phần mở rộng của GNU, một biểu thức chính quy có hậu tố cũng có thể được theo sau bởi
sed -i 's/hello/world/' file.txt60; . VỊ TRÍ 1003. 1-2001 nói rằng
sed -i 's/hello/world/' file.txt60 là viết tắt của chính nó khi nó xuất hiện ở đầu biểu thức chính quy hoặc biểu thức con, nhưng nhiều triển khai nonGNU không hỗ trợ điều này và thay vào đó, các tập lệnh di động nên sử dụng
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE03 trong các ngữ cảnh 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: 389
Khớp với bất kỳ ký tự nào, bao gồm cả dòng mớ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.txt54
So khớp với chuỗi null ở đầu không gian mẫu, i. e. những gì xuất hiện sau dấu mũ phải xuất hiện ở đầu không gian mẫu
Trong hầu hết các tập lệnh, không gian mẫu được khởi tạo thành nội dung của mỗi dòng [xem phần ]. Vì vậy, sẽ là một cách đơn giản hóa hữu ích khi coi
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE07 chỉ khớp với các dòng trong đó '#include' là điều đầu tiên trên dòng—ví dụ: nếu có khoảng trắng trước đó thì khớp không thành công. Sự đơn giản hóa này có hiệu lực miễn là nội dung ban đầu của không gian mẫu không bị sửa đổi, ví dụ như với lệnh
sed -n '1p ; $p' one.txt two.txt three.txt67
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.txt54 chỉ hoạt động như một ký tự đặc biệt ở đầu biểu thức chính quy hoặc biểu thức con [nghĩa là sau
sed -n '1p ; $p' one.txt two.txt three.txt91 hoặc
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE11]. Tuy nhiên, các tập lệnh di động nên tránh
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 ở đầu biểu thức con, vì POSIX cho phép triển khai coi
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.txt54 như một ký tự thông thường trong ngữ cảnh đó
sed -i 's/hello/world/' file.txt87
Nó giống như
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54, nhưng đề cập đến phần cuối của không gian mẫu.
sed -i 's/hello/world/' file.txt87 cũng chỉ hoạt động như một ký tự đặc biệt ở cuối biểu thức chính quy hoặc biểu thức con [nghĩa là trước
sed -n '1p ; $p' one.txt two.txt three.txt92 hoặc
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE11] và việc sử dụng nó ở cuối biểu thức con là không thể di chuyển
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE19_______48_______20
Khớp với bất kỳ ký tự đơn nào trong danh sách. ví dụ:
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE21 khớp với tất cả các nguyên âm. Một danh sách có thể bao gồm các chuỗi như
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE22, khớp với bất kỳ ký tự nào giữa [bao gồm] char1 và char2. See
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE23
Là
sed -i 's/hello/world/' file.txt60, nhưng khớp với một hoặc nhiều. Nó là một phần mở rộng của GNU
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE25
Là
sed -i 's/hello/world/' file.txt60, nhưng chỉ khớp với 0 hoặc 1. Nó là một phần mở rộng của GNU
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE27
Là
sed -i 's/hello/world/' file.txt60, nhưng khớp chính xác với i chuỗi [i là số nguyên thập phân; để dễ mang theo, hãy giữ nó trong khoảng từ 0 đến 255]
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE29
Khớp giữa i và j, bao gồm, trình tự
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE30
So khớp nhiều hơn hoặc bằng i chuỗi
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE31
Nhóm toàn bộ biểu thức chính quy bên trong, điều này được sử dụng để
- Áp dụng các toán tử hậu tố, như
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE
32. điều này sẽ tìm kiếm không hoặc nhiều hơn toàn bộ chuỗi 'abcd', trong khi# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE
33 sẽ tìm kiếm 'abc' theo sau là không hoặc nhiều lần xuất hiện của 'd'. Lưu ý rằng POSIX 1003 yêu cầu hỗ trợ cho# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE
32. 1-2001, nhưng nhiều triển khai không phải GNU không hỗ trợ nó và do đó nó không thể di động phổ biến - Use back references [see below]
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE35
Khớp với regexp1 hoặc regexp2. Sử dụng dấu ngoặc đơn để sử dụng các biểu thức chính quy thay thế phức tạp. Quá trình so khớp lần lượt thử từng phương án, từ trái sang phải và phương án đầu tiên thành công được sử dụng. Nó là một phần mở rộng của GNU
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE36
So khớp nối của regexp1 và regexp2. Phép nối liên kết chặt chẽ hơn
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE11,
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.txt54 và
sed -i 's/hello/world/' file.txt87, nhưng kém chặt chẽ hơn so với các toán tử biểu thức chính quy khác
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE40
So khớp với biểu thức con được đặt trong ngoặc đơn thứ _____________thứ ____ trong biểu thức chính quy. Điều này được gọi là một tài liệu tham khảo trở lại. Các biểu thức con được đánh số hoàn toàn bằng cách đếm số lần xuất hiện của
sed -n '1p ; $p' one.txt two.txt three.txt91 từ trái sang phải
sed -n '1p ; $p' one.txt two.txt three.txt90
Khớp với ký tự xuống dòng
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE44
So khớp char, trong đó char là một trong số
sed -i 's/hello/world/' file.txt87,
sed -i 's/hello/world/' file.txt60,
$ 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: 389,
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE48,
sed -n '1p ; $p' one.txt two.txt three.txt97 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.txt54. Lưu ý rằng các chuỗi dấu gạch chéo ngược giống như chữ C duy nhất mà bạn có thể cho là được diễn giải là
sed -n '1p ; $p' one.txt two.txt three.txt90 và
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt95;
Lưu ý rằng trình so khớp cụm từ thông dụng là tham lam, tôi. e. , các trận đấu được thử từ trái sang phải và, nếu có thể có hai hoặc nhiều trận đấu bắt đầu từ cùng một ký tự, nó sẽ chọn trận đấu dài nhất
ví dụ
'abcdef'Phù hợp với 'abcdef'
'a*b'Khớp không hoặc nhiều chữ 'a' theo sau bởi một chữ 'b'. Ví dụ: 'b' hoặc 'aaaaab'
'a\?b'Khớp 'b' hoặc 'ab'
'a\+b\+'So khớp một hoặc nhiều chữ 'a' theo sau bởi một hoặc nhiều chữ 'b'. 'ab' là từ khớp ngắn nhất có thể, nhưng các ví dụ khác là 'aaaab' hoặc 'abbbbb' hoặc 'aaaaaabbbbbbbb'
‘. *''. \+’Cả hai đều khớp với tất cả các ký tự trong một chuỗi;
'^ chính. *[. *]’Điều này khớp với một chuỗi bắt đầu bằng 'main', theo sau là dấu ngoặc đơn mở và đóng. 'n', '[' và ']' không cần phải liền kề
'^#'Điều này khớp với một chuỗi bắt đầu bằng '#'
‘\\$’Điều này khớp với một chuỗi kết thúc bằng một dấu gạch chéo ngược. Biểu thức chính quy chứa hai dấu gạch chéo ngược để thoát
‘\$’Thay vào đó, điều này phù hợp với một chuỗi bao gồm một ký hiệu đô la, bởi vì nó được thoát
‘[a-zA-Z0-9]’Trong ngôn ngữ C, điều này khớp với bất kỳ chữ cái hoặc chữ số ASCII nào
‘[^ TAB]\+’[Ở đây TAB là viết tắt của một ký tự tab đơn. ] Điều này khớp với một chuỗi gồm một hoặc nhiều ký tự, không có ký tự nào là khoảng trắng hoặc tab. Thông thường điều này có nghĩa là một từ
‘^\[. *\]\n\1$’This matches a string consisting of two equal substrings separated by a newline
‘. \{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ó
ví dụ
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE55
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/' FILE56
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/' FILE57
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/' FILE58
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/' FILE59
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/' FILE60
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: 36
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.txt22 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: 37‘[. hàng năm. ]'
Ký tự chữ và số. ‘[. chữ cái. ]' và '[. chữ số. ]’; in the ‘C’ locale and ASCII character encoding, this is the same as ‘[0-9A-Za-z]’
‘[. 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/' FILE62‘[. đồ 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. ]'Space characters. 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/' FILE65‘[. x chữ số. ]'
chữ số thập lục phân.
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE66
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.txt87,
sed -i 's/hello/world/' file.txt60,
$ 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: 389,
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE48, và
sed -n '1p ; $p' one.txt two.txt three.txt97 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/' FILE72 khớp với ‘\’ hoặc ‘*’, vì
sed -n '1p ; $p' one.txt two.txt three.txt97 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/' FILE74,
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE75 và
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE76 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/' FILE48 đặ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: 389,
sed -n '1p ; $p' one.txt two.txt three.txt80 hoặc
sed OPTIONS.. [SCRIPT] [INPUTFILE...]83. Ngoài ra, khi không ở chế độ
sed -i 's/hello/world/' file.txt72, các lần thoát đặc biệt như
sed -n '1p ; $p' one.txt two.txt three.txt90 và
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE53 được công nhận trong danh sách. Thấy‘[. ’
đại diện cho biểu tượng đối chiếu mở
‘. ]'đại diện cho biểu tượng đối chiếu gần
'[='đạ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.txt67]
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/' FILE85
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: 38
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE86
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: 39
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE87
Khớp với một ranh giới từ;
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE0
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE88
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/' FILE1
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE89
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/' FILE2
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE90
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/' FILE3
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE91
Khớp với phần đầu của một từ
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE4
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE92
Khớp với phần cuối của một từ
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE5
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.txt56
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.txt54 ở chế độ nhiều dòng
So sánh hai ví dụ sau
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE6
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.txt57
Chỉ khớp ở cuối không gian mẫu. Điều này khác với
sed -i 's/hello/world/' file.txt87 ở chế độ nhiều dòng
5. 7 Tham chiếu ngược và Biểu thức con
back-references are regular expression commands which refer to a previous part of the matched regular expression. Tham chiếu ngược được chỉ định bằng dấu gạch chéo ngược và một chữ số [e. g. ‘\1’]. The part of the regular expression they refer to is called a subexpression, and is designated with parentheses
Tham chiếu ngược và biểu thức con được sử dụng trong hai trường hợp. trong mẫu tìm kiếm biểu thức chính quy và trong phần thay thế của lệnh
sed -n '1p ; $p' one.txt two.txt three.txt67 [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/' FILE7
Multiple subexpressions are automatically numbered from left-to-right. This command searches for 6-letter palindromes [the first three letters are 3 subexpressions, followed by 3 back-references in reverse order]
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE8
In the
sed -n '1p ; $p' one.txt two.txt three.txt67 command, back-references can be used in the replacement part to refer back to subexpressions in the regexp part
The following example uses two subexpressions in the regular expression to match two space-separated words. The back-references in the replacement part prints the words in a different order
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE9
When used with alternation, if the group does not participate in the match then the back-reference makes the whole match fail. For example, ‘a[. ]. b\1’ will not match ‘ba’. When multiple regular expressions are given with -e or from a file [‘-f file’], back-references are local to each expression
5. 8 Escape Sequences - specifying special characters
Until this chapter, we have only encountered escapes of the form ‘\^’, which tell
sed -i 's/hello/world/' file.txt22 not to interpret the circumflex as a special character, but rather to take it literally. For example, ‘\*’ matches a single asterisk rather than zero or more backslashes
This chapter introduces another kind of escape—that is, escapes that are applied to a character or sequence of characters that ordinarily are taken literally, and that
sed -i 's/hello/world/' file.txt22 replaces with a special character. This provides a way of encoding non-printable characters in patterns in a visible manner. There is no restriction on the appearance of non-printing characters in a
sed -i 's/hello/world/' file.txt22 script but when a script is being prepared in the shell or by text editing, it is usually easier to use one of the following escape sequences than the binary character it represents
The list of these escapes is
$ echo | sed 'Q42' ; echo $? 4202
Produces or matches a BEL character, that is an “alert” [ASCII 7]
$ echo | sed 'Q42' ; echo $? 4203
Produces or matches a form feed [ASCII 12]
sed -n '1p ; $p' one.txt two.txt three.txt90
Produces or matches a newline [ASCII 10]
$ echo | sed 'Q42' ; echo $? 4205
Produces or matches a carriage return [ASCII 13]
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE53
Produces or matches a horizontal tab [ASCII 9]
$ echo | sed 'Q42' ; echo $? 4207
Produces or matches a so called “vertical tab” [ASCII 11]
$ echo | sed 'Q42' ; echo $? 4208
Produces or matches CONTROL-x, where x is any character. The precise effect of ‘\cx’ is as follows. if x is a lower case letter, it is converted to upper case. Then bit 6 of the character [hex 40] is inverted. Thus ‘\cz’ becomes hex 1A, but ‘\c{’ becomes hex 3B, while ‘\c;’ becomes hex 7B
$ echo | sed 'Q42' ; echo $? 4209
Produces or matches a character whose decimal ASCII value is xxx
$ echo | sed 'Q42' ; echo $? 4210
Produces or matches a character whose octal ASCII value is xxx
$ echo | sed 'Q42' ; echo $? 4211
Produces or matches a character whose hexadecimal ASCII value is xx
‘\b’ [backspace] was omitted because of the conflict with the existing “word boundary” meaning
5. 8. 1 Escaping Precedence
GNU
sed -i 's/hello/world/' file.txt22 processes escape sequences before passing the text onto the regular-expression matching of the
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 366 command and Address matching. Thus the follwing two commands are equivalent [‘0x5e’ is the hexadecimal ASCII value of the character ‘^’]
$ echo | sed 'Q42' ; echo $? 420
As are the following [‘0x5b’,‘0x5d’ are the hexadecimal ASCII values of ‘[’,‘]’, respectively]
$ echo | sed 'Q42' ; echo $? 421
However it is recommended to avoid such special characters due to unexpected edge-cases. For example, the following are not equivalent
$ echo | sed 'Q42' ; echo $? 422
5. 9 Multibyte characters and Locale Considerations
GNU
sed -i 's/hello/world/' file.txt22 processes valid multibyte characters in multibyte locales [e. g.
$ echo | sed 'Q42' ; echo $? 4215].
The following example uses the Greek letter Capital Sigma [Σ, Unicode code point
$ echo | sed 'Q42' ; echo $? 4216]. In a
$ echo | sed 'Q42' ; echo $? 4215 locale,
sed -i 's/hello/world/' file.txt22 correctly processes the Sigma as one character despite it being 2 octets [bytes]
$ echo | sed 'Q42' ; echo $? 423
To force
sed -i 's/hello/world/' file.txt22 to process octets separately, use the
$ echo | sed 'Q42' ; echo $? 4220 locale [also known as the
$ echo | sed 'Q42' ; echo $? 4221 locale]
$ echo | sed 'Q42' ; echo $? 424
5. 9. 1 Invalid multibyte characters
sed -i 's/hello/world/' file.txt22’s regular expressions do not match invalid multibyte sequences in a multibyte locale
In the following examples, the ascii value
$ echo | sed 'Q42' ; echo $? 4223 is an incomplete multibyte character [shown here as �]. The regular expression ‘. ’ does not match it
$ echo | sed 'Q42' ; echo $? 425
Similarly, the ’catch-all’ regular expression ‘. *’ does not match the entire line
$ echo | sed 'Q42' ; echo $? 426
GNU
sed -i 's/hello/world/' file.txt22 offers the special
sed -n '1p ; $p' one.txt two.txt three.txt77 command to clear the current pattern space regardless of invalid multibyte characters [i. e. it works like
$ echo | sed 'Q42' ; echo $? 4226 but also removes invalid multibyte characters]
$ echo | sed 'Q42' ; echo $? 427
Alternatively, force the
$ echo | sed 'Q42' ; echo $? 4220 locale to process each octet separately [every octet is a valid character in the
$ echo | sed 'Q42' ; echo $? 4220 locale]
$ echo | sed 'Q42' ; echo $? 428
sed -i 's/hello/world/' file.txt22’s inability to process invalid multibyte characters can be used to detect such invalid sequences in a file. In the following examples, the
$ echo | sed 'Q42' ; echo $? 4230 is an invalid multibyte sequence, while
$ echo | sed 'Q42' ; echo $? 4231 is a valid multibyte sequence [of the Greek Sigma character]
The following
sed -i 's/hello/world/' file.txt22 program removes all valid characters using
$ echo | sed 'Q42' ; echo $? 4233. Any content left in the pattern space [the invalid characters] are added to the hold space using the
sed -n '1p ; $p' one.txt two.txt three.txt47 command. On the last line [
sed -i 's/hello/world/' file.txt87], the hold space is retrieved [
sed -n '1p ; $p' one.txt two.txt three.txt75], newlines are removed [
$ echo | sed 'Q42' ; echo $? 4237], and any remaining octets are printed unambiguously [
sed -i 's/hello/world/' file.txt67]. Thus, any invalid multibyte sequences are printed as octal values
$ echo | sed 'Q42' ; echo $? 429
With a few more commands,
sed -i 's/hello/world/' file.txt22 can print the exact line number corresponding to each invalid characters [line 3]. These characters can then be removed by forcing the
$ echo | sed 'Q42' ; echo $? 4220 locale and using octal escape sequences
sed '30,35d' input.txt > output.txt0
5. 9. 2 Upper/Lower case conversion
GNU
sed -i 's/hello/world/' file.txt22’s substitute command [
sed -n '1p ; $p' one.txt two.txt three.txt67] supports upper/lower case conversions using
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt05,
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.txt04 codes. These conversions support multibyte characters
sed '30,35d' input.txt > output.txt1
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 $? 4245 thành giá trị 'C'
sed '30,35d' input.txt > output.txt2
Giải thích của họ phụ thuộc vào ngôn ngữ
$ echo | sed 'Q42' ; echo $? 4246; . 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
TODO. hiển thị ví dụ về đối chiếu
sed '30,35d' input.txt > output.txt3
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.txt22 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.txt22 hoạt động bằng cách thực hiện chu trình sau trên mỗi dòng đầu vào. first,
sed -i 's/hello/world/' file.txt22 reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it. addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed
When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed. Then the next cycle starts for the next input line
Unless special commands [like ‘D’] are used, the pattern space is deleted between two cycles. The hold space, on the other hand, keeps its data between cycles [see commands ‘h’, ‘H’, ‘x’, ‘g’, ‘G’ to move data between both buffers]
6. 2 Hold and Pattern Buffers
TODO
6. 3 Multiline techniques - using D,G,H,N,P to process multiple lines
Multiple lines can be processed as one buffer using the
sed -n '1p ; $p' one.txt two.txt three.txt39,
sed -n '1p ; $p' one.txt two.txt three.txt45,
sed -n '1p ; $p' one.txt two.txt three.txt47,
sed -i 's/hello/world/' file.txt71,
sed -n '1p ; $p' one.txt two.txt three.txt57. They are similar to their lowercase counterparts [
sed -i 's/hello/world/' file.txt36,
sed -n '1p ; $p' one.txt two.txt three.txt44,
sed -n '1p ; $p' one.txt two.txt three.txt46,
sed -n '1p ; $p' one.txt two.txt three.txt52,
sed -i 's/hello/world/' file.txt37], except that these commands append or subtract data while respecting embedded newlines - allowing adding and removing lines from the pattern and hold spaces
They operate as follows
sed -n '1p ; $p' one.txt two.txt three.txt39
deletes line from the pattern space until the first newline, and restarts the cycle
sed -n '1p ; $p' one.txt two.txt three.txt45
appends line from the hold space to the pattern space, with a newline before it
sed -n '1p ; $p' one.txt two.txt three.txt47
appends line from the pattern space to the hold space, with a newline before it
sed -i 's/hello/world/' file.txt71
appends line from the input file to the pattern space
sed -n '1p ; $p' one.txt two.txt three.txt57
prints line from the pattern space until the first newline
The following example illustrates the operation of
sed -i 's/hello/world/' file.txt71 and
sed -n '1p ; $p' one.txt two.txt three.txt39 commands
sed '30,35d' input.txt > output.txt4
sed -i 's/hello/world/' file.txt
22 starts by reading the first line into the pattern space [i. e. ‘1’]- At the beginning of every cycle, the
sed -i 's/hello/world/' file.txt
71 command appends a newline and the next line to the pattern space [i. e. ‘1’, ‘\n’, ‘2’ in the first cycle] - The
sed -i 's/hello/world/' file.txt
67 command prints the content of the pattern space unambiguously - The
sed -n '1p ; $p' one.txt two.txt three.txt
39 command then removes the content of pattern space up to the first newline [leaving ‘2’ at the end of the first cycle] - At the next cycle the
sed -i 's/hello/world/' file.txt
71 command appends a newline and the next input line to the pattern space [e. g. ‘2’, ‘\n’, ‘3’]
A common technique to process blocks of text such as paragraphs [instead of line-by-line] is using the following construct
sed '30,35d' input.txt > output.txt5
- The first expression,
$ echo | sed 'Q42' ; echo $? 42
74 operates on all non-empty lines, and adds the current line [in the pattern space] to the hold space. On all lines except the last, the pattern space is deleted and the cycle is restarted - The other expressions
sed -n '1p ; $p' one.txt two.txt three.txt
75 andsed -n '1p ; $p' one.txt two.txt three.txt
67 are executed only on empty lines [i. e. paragraph separators]. Thesed -n '1p ; $p' one.txt two.txt three.txt
75 command fetches the accumulated lines from the hold space back to the pattern space. The$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3
66 command then operates on all the text in the paragraph [including the embedded newlines]
The following example demonstrates this technique
sed '30,35d' input.txt > output.txt6
For more annotated examples, see and
6. 4 Branching and Flow Control
The branching commands
sed -n '1p ; $p' one.txt two.txt three.txt82,
sed -n '1p ; $p' one.txt two.txt three.txt83, and
sed -n '1p ; $p' one.txt two.txt three.txt84 enable changing the flow of
sed -i 's/hello/world/' file.txt22 programs
By default,
sed -i 's/hello/world/' file.txt22 reads an input line into the pattern buffer, then continues to processes all commands in order. Commands without addresses affect all lines. Commands with addresses affect only matching lines. See and
sed -i 's/hello/world/' file.txt22 does not support a typical
$ echo | sed 'Q42' ; echo $? 4285 construct. Instead, some commands can be used as conditionals or to change the default flow control
sed -i 's/hello/world/' file.txt36
delete [clears] the current pattern space, and restart the program cycle without processing the rest of the commands and without printing the pattern space
sed -n '1p ; $p' one.txt two.txt three.txt39
delete the contents of the pattern space up to the first newline, and restart the program cycle without processing the rest of the commands and without printing the pattern space
$ echo | sed 'Q42' ; echo $? 4288
$ echo | sed 'Q42' ; echo $? 4289
$ echo | sed 'Q42' ; echo $? 4290
$ echo | sed 'Q42' ; echo $? 4291
Addresses and regular expressions can be used as an
$ echo | sed 'Q42' ; echo $? 4285 conditional. If [addr] matches the current pattern space, execute the command[s]. For example. The command
$ echo | sed 'Q42' ; echo $? 4293 means. if the current pattern matches the regular expression
$ echo | sed 'Q42' ; echo $? 4294 [a line starting with a hash], then execute the
sed -i 's/hello/world/' file.txt36 command. delete the line without printing it, and restart the program cycle immediately
sed -n '1p ; $p' one.txt two.txt three.txt82
branch unconditionally [that is. always jump to a label, skipping or repeating other commands, without restarting a new cycle]. Combined with an address, the branch can be conditionally executed on matched lines
sed -n '1p ; $p' one.txt two.txt three.txt83
branch conditionally [that is. jump to a label] only if a
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 366 command has succeeded since the last input line was read or another conditional branch was taken
sed -n '1p ; $p' one.txt two.txt three.txt84
similar but opposite to the
sed -n '1p ; $p' one.txt two.txt three.txt83 command. branch only if there has been no successful substitutions since the last input line was read
The following two
sed -i 's/hello/world/' file.txt22 programs are equivalent. The first [contrived] example uses the
sed -n '1p ; $p' one.txt two.txt three.txt82 command to skip the
$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 366 command on lines containing ‘1’. The second example uses an address with negation [‘. ’] to perform substitution only on desired lines. The
sed '30,35d' input.txt > output.txt04 command is still executed on all lines
sed '30,35d' input.txt > output.txt7
6. 4. 1 Branching and Cycles
The
sed -n '1p ; $p' one.txt two.txt three.txt82,
sed -n '1p ; $p' one.txt two.txt three.txt83 and
sed -n '1p ; $p' one.txt two.txt three.txt84 commands can be followed by a label [typically a single letter]. Labels are defined with a colon followed by one or more letters [e. g. ‘. x’]. If the label is omitted the branch commands restart the cycle. Note the difference between branching to a label and restarting the cycle. when a cycle is restarted,
sed -i 's/hello/world/' file.txt22 first prints the current content of the pattern space, then reads the next input line into the pattern space; Jumping to a label [even if it is at the beginning of the program] does not print the pattern space and does not read the next input line
The following program is a no-op. The
sed -n '1p ; $p' one.txt two.txt three.txt82 command [the only command in the program] does not have a label, and thus simply restarts the cycle. On each cycle, the pattern space is printed and the next input line is read
The following example is an infinite-loop - it doesn’t terminate and doesn’t print anything. The
sed -n '1p ; $p' one.txt two.txt three.txt82 command jumps to the ‘x’ label, and a new cycle is never started
sed '30,35d' input.txt > output.txt8
Branching is often complemented with the
sed -n '1p ; $p' one.txt two.txt three.txt52 or
sed -i 's/hello/world/' file.txt71 commands. both commands read the next input line into the pattern space without waiting for the cycle to restart. Before reading the next input line,
sed -n '1p ; $p' one.txt two.txt three.txt52 prints the current pattern space then empties it, while
sed -i 's/hello/world/' file.txt71 appends a newline and the next input line to the pattern space
Consider the following two examples
sed '30,35d' input.txt > output.txt9
- Both examples do not inf-loop, despite never starting a new cycle
- In the first example, the
sed -n '1p ; $p' one.txt two.txt three.txt
52 commands first prints the content of the pattern space, empties the pattern space then reads the next input line - In the second example, the
sed -i 's/hello/world/' file.txt
71 commands appends the next input line to the pattern space [with a newline]. Lines are accumulated in the pattern space until there are no more input lines to read, then thesed -i 's/hello/world/' file.txt
71 command terminates thesed -i 's/hello/world/' file.txt
22 program. When the program terminates, the end-of-cycle actions are performed, and the entire pattern space is printed - The second example requires GNU
sed -i 's/hello/world/' file.txt
22, because it uses the non-POSIX-standard behavior ofsed -i 's/hello/world/' file.txt
71. See the “sed -i 's/hello/world/' file.txt
71 command on the last line” paragraph in - To further examine the difference between the two examples, try the following commands
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt
00
6. 4. 2 Branching example. đường nối
As a real-world example of using branching, consider the case of quoted-printable files, typically used to encode email messages. 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.txt01
The following program uses an address match ‘/=$/’ as a conditional. If the current pattern space ends with a ‘=’, it reads the next input line using
sed -i 's/hello/world/' file.txt71, replaces all ‘=’ characters which are followed by a newline, and unconditionally branches [
sed -n '1p ; $p' one.txt two.txt three.txt82] to the beginning of the program without restarting a new cycle. If the pattern space does not ends with ‘=’, the default action is performed. the pattern space is printed and a new cycle is started
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt02
Here’s an alternative program with a slightly different approach. On all lines except the last,
sed -i 's/hello/world/' file.txt71 appends the line to the pattern space. A substitution command then removes soft line breaks [‘=’ at the end of a line, i. e. followed by a newline] by replacing them with an empty string. if the substitution was successful [meaning the pattern space contained a line which should be joined], The conditional branch command
sed -n '1p ; $p' one.txt two.txt three.txt83 jumps to the beginning of the program without completing or restarting the cycle. If the substitution failed [meaning there were no soft line breaks], The
sed -n '1p ; $p' one.txt two.txt three.txt83 command will not branch. Then,
sed -n '1p ; $p' one.txt two.txt three.txt57 will print the pattern space content until the first newline, and
sed -n '1p ; $p' one.txt two.txt three.txt39 will delete the pattern space content until the first new line. [To learn more about
sed -i 's/hello/world/' file.txt71,
sed -n '1p ; $p' one.txt two.txt three.txt57 and
sed -n '1p ; $p' one.txt two.txt three.txt39 commands see ]
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt03
For more line-joining examples see
7 Some Sample Scripts
Here are some
sed -i 's/hello/world/' file.txt22 scripts to guide you in the art of mastering
sed -i 's/hello/world/' file.txt22
7. 1 Joining lines
This section uses
sed -i 's/hello/world/' file.txt71,
sed -n '1p ; $p' one.txt two.txt three.txt39 and
sed -n '1p ; $p' one.txt two.txt three.txt57 commands to process multiple lines, and the
sed -n '1p ; $p' one.txt two.txt three.txt82 and
sed -n '1p ; $p' one.txt two.txt three.txt83 commands for branching. See and
Join specific lines [e. g. if lines 2 and 3 need to be joined]
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt04
Join backslash-continued lines
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt05
Join lines that start with whitespace [e. g SMTP headers]
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt06
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.txt39 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.txt07
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt08
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt09
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt10
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt11
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt12
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.txt22. 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.txt67 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.txt13
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt14
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt15
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt16
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt17
7. 4 Rename Files to Lower Case
This is a pretty strange use of
sed -i 's/hello/world/' file.txt22. 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.txt22; I have seen a script converting the output of
sed '30,35d' input.txt > output.txt44 into a
sed '30,35d' input.txt > output.txt45 program
The main body of this is the
sed -i 's/hello/world/' file.txt22 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.txt18
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt19
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt20
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt21
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt22
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt23
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt24
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt25
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt26
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt27
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt28
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt29
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt30
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt31
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt32
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt33
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt34
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt35
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt36
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt37
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt38
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt39
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt40
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.txt48 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.txt41
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt42
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt43
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt44
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt45
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt46
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt47
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.txt49 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.txt83 command
Imaginative readers will find uses for this script. An example is reversing the output of
sed '30,35d' input.txt > output.txt51
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt48
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt49
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt50
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt51
7. 7 Text search across multiple lines
This section uses
sed -i 's/hello/world/' file.txt71 and
sed -n '1p ; $p' one.txt two.txt three.txt39 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: 387 and similarly with GNU
sed -i 's/hello/world/' file.txt22
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt52
- 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: 387 and
sed -i 's/hello/world/' file.txt22 operate line-by-line
By using
sed -i 's/hello/world/' file.txt71 and
sed -n '1p ; $p' one.txt two.txt three.txt39 commands,
sed -i 's/hello/world/' file.txt22 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.txt53
- 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.txt64 manual for an alternative solution using
sed '30,35d' input.txt > output.txt65 and
sed '30,35d' input.txt > output.txt66 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.txt71 and
sed -n '1p ; $p' one.txt two.txt three.txt39 commands to search for consecutive words spanning multiple lines, and the
sed -n '1p ; $p' one.txt two.txt three.txt82 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.txt54
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.txt55
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.txt56
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.txt70 workalike
Note that on implementations other than GNU
sed -i 's/hello/world/' file.txt22 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.txt57
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt58
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt59
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt60
7. 10 Numbering Lines
This script replaces ‘cat -n’; in fact it formats its output exactly like GNU
sed '30,35d' input.txt > output.txt72 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.txt61
It uses
sed -i 's/hello/world/' file.txt22 to print the line number, then groups lines two by two using
sed -i 's/hello/world/' file.txt71. Of course, this script does not teach as much as the one presented below
The algorithm used for incrementing uses both buffers, so the line is printed as soon as possible and then discarded. The number is split so that changing digits go in a buffer and unchanged ones go in the other; the changed digits are modified in a single step [using a
sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt83 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.txt62_______10_______63
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt64_______10_______65
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt66
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt67
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt68
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt69
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt70
7. 11 Numbering Non-blank Lines
Mô phỏng 'cat -b' gần giống như 'cat -n'—chúng ta chỉ phải chọn dòng nào sẽ được đánh số và dòng nào không
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.txt22 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.txt62
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt72
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt73
7. 12 Counting Characters
This script shows another way to do arithmetic with
sed -i 's/hello/world/' file.txt22. Trong trường hợp này, chúng tôi phải thêm các số lượng lớn có thể, do đó, việc thực hiện điều này theo các bước tăng liên tiếp sẽ không khả thi [và thậm chí có thể phức tạp hơn so với tập lệnh này]
The approach is to map numbers to letters, kind of an abacus implemented with
sed -i 's/hello/world/' file.txt22. ‘a’s are units, ‘b’s are tens and so on. chúng ta chỉ cần thêm số lượng ký tự trên dòng hiện tại dưới dạng đơn vị, sau đó chuyển số ký tự thành hàng chục, hàng trăm, v.v.
Như thường lệ, tổng số chạy được giữ trong không gian giữ
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.txt67 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.txt62
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt75
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt76
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt77
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt78
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt79
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt80
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.txt80 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.txt22 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.txt62
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt82
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt83
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt84
7. 14 Counting Lines
No strange things are done now, because
sed -i 's/hello/world/' file.txt22 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.txt22 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.txt00 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.txt70 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.txt62
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt86
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.txt39 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.txt22 scripts, because commands like
sed -n '1p ; $p' one.txt two.txt three.txt57 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.txt71,
sed -n '1p ; $p' one.txt two.txt three.txt57 and
sed -n '1p ; $p' one.txt two.txt three.txt39 commands, here is an implementation of
sed '30,35d' input.txt > output.txt92 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.txt71 and
sed -n '1p ; $p' one.txt two.txt three.txt39 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.txt07
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt88
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.txt71,
sed -n '1p ; $p' one.txt two.txt three.txt57 and
sed -n '1p ; $p' one.txt two.txt three.txt39 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.txt89
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt90
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt91
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt92
As you can see, we maintain a 2-line window using
sed -n '1p ; $p' one.txt two.txt three.txt57 and
sed -n '1p ; $p' one.txt two.txt three.txt39. This technique is often used in advanced
sed -i 's/hello/world/' file.txt22 scripts
7. 18 Print Duplicated Lines of Input
This script prints only duplicated lines, like ‘uniq -d’
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt62
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt94
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt95
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt96
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt97
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.txt07
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt99
sed -i 's/hello/world/' file.txt00
sed -i 's/hello/world/' file.txt01
sed -i 's/hello/world/' file.txt02
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.txt07
sed -i 's/hello/world/' file.txt04
sed -i 's/hello/world/' file.txt05
This one is a bit more complex and removes all empty lines at the beginning. It does leave a single blank line at end if one was there
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt07
sed -i 's/hello/world/' file.txt07
sed -i 's/hello/world/' file.txt08
This removes leading and trailing blank lines. It is also the fastest. Note that loops are completely done with
sed -n '1p ; $p' one.txt two.txt three.txt52 and
sed -n '1p ; $p' one.txt two.txt three.txt82, without relying on
sed -i 's/hello/world/' file.txt22 to restart the script automatically at the end of a 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.txt62
sed -i 's/hello/world/' file.txt10
sed -i 's/hello/world/' file.txt11
sed -i 's/hello/world/' file.txt12
sed -i 's/hello/world/' file.txt13
sed -i 's/hello/world/' file.txt14
8 GNU sed -i 's/hello/world/' file.txt
22’s Limitations and Non-limitations
For those who want to write portable
sed -i 's/hello/world/' file.txt22 scripts, be aware that some implementations have been known to limit line lengths [for the pattern and hold spaces] to be no more than 4000 bytes. The POSIX standard specifies that conforming
sed -i 's/hello/world/' file.txt22 implementations shall support at least 8192 byte line lengths. GNU
sed -i 's/hello/world/' file.txt22 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.txt008 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.txt22 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.txt22 [both GNU
sed -i 's/hello/world/' file.txt22 and other variations]. Note these not maintained by GNU
sed -i 's/hello/world/' file.txt22 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.txt15
If GNU
sed -i 's/hello/world/' file.txt22 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.txt22 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.txt22 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.txt22 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.txt71 command on the last line
Most versions of
sed -i 's/hello/world/' file.txt22 exit without printing anything when the
sed -i 's/hello/world/' file.txt71 command is issued on the last line of a file. GNU
sed -i 's/hello/world/' file.txt22 prints pattern space before exiting unless of course the
sed -i 's/hello/world/' file.txt45 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.txt16
For example, the behavior of
would depend on whether foo has an even or an odd number of lines. Hoặc, khi viết một tập lệnh để đọc một vài dòng tiếp theo theo mẫu khớp, cách triển khai truyền thống của
sed -i 's/hello/world/' file.txt22 sẽ buộc bạn phải viết một cái gì đó như
sed -i 's/hello/world/' file.txt17
instead of just
sed -i 's/hello/world/' file.txt18
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.txt024 in scripts that rely on the traditional behavior, or to set the
sed -i 's/hello/world/' file.txt72 variable to a non-empty valueRegex syntax clashes [problems with backslashes]
sed -i 's/hello/world/' file.txt22 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.txt22 are
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE11,
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE23,
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE25,
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.txt56,
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.txt57,
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE91,
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE92,
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE87,
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE88,
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE85, and
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE86
Như trong tất cả các chương trình GNU sử dụng các biểu thức chính quy cơ bản POSIX,
sed -i 's/hello/world/' file.txt22 diễn giải các chuỗi thoát này thành các ký tự đặc biệt. 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.txt040 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.txt041 matches either ‘abc’ or ‘def’
This syntax may cause problems when running scripts written for other
sed -i 's/hello/world/' file.txt22s. Some
sed -i 's/hello/world/' file.txt22 programs have been written with the assumption that
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE11 and
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE23 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.txt046 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.txt047. Các tập lệnh như vậy phải được sửa đổi bằng cách xóa các dấu gạch chéo ngược giả nếu chúng được sử dụng với các triển khai hiện đại của
sed -i 's/hello/world/' file.txt22, như GNU
sed -i 's/hello/world/' file.txt22
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.txt050 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.txt051. While this worked until
sed -i 's/hello/world/' file.txt22 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.txt053. This is again undefined behavior according to POSIX, and this interpretation is arguably more robust. older
sed -i 's/hello/world/' file.txt22s, 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.txt055 as
sed -n '1p ; $p' one.txt two.txt three.txt94 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.txt22 supports several escape characters [some of which are multi-character] to insert non-printable characters in scripts [
$ echo | sed 'Q42' ; echo $? 4202,
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt059,
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt060,
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt061,
$ echo | sed 'Q42' ; echo $? 4205,
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE53,
$ echo | sed 'Q42' ; echo $? 4207,
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt065]. These can cause similar problems with scripts written for other
sed -i 's/hello/world/' file.txt22s-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.txt22 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.txt068 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.txt069 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.txt070 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.txt071
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt072 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.txt072 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.txt074 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.txt075. Some locales have a case-insensitive collation order, others don’t
Another problem is that
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt072 tries to use collation symbols. This only happens if you are on the GNU system, using GNU libc’s regular expression matcher instead of compiling the one supplied with GNU sed. In a Danish locale, for example, the regular expression
sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt077 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.txt078 and
$ echo | sed 'Q42' ; echo $? 4246 environment variables to ‘C’
$ echo | sed 'Q42' ; echo $? 4226 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.txt22 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.txt078 and
$ echo | sed 'Q42' ; echo $? 4246 environment variables to ‘C’
Appendix A GNU Free Documentation License
Version 1. 3, 3 November 2008
sed -i 's/hello/world/' file.txt19
- 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
- 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
The “Cover Texts” are certain short passages of text that are listed, as Front-Cover Texts or Back-Cover Texts, in the notice that says that the Document is released under this License. A Front-Cover Text may be at most 5 words, and a Back-Cover Text may be at most 25 words
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 các pixel] sơn chung . A copy made in an otherwise Transparent file format whose markup, or absence of markup, has been arranged to thwart or discourage subsequent modification by readers is not Transparent. An image format is not Transparent if used for any substantial amount of text. A copy that is not “Transparent” is called “Opaque”
Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standard-conforming simple HTML, PostScript or PDF designed for human modification. Examples of transparent image formats include PNG, XCF and JPG. Opaque formats include proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machine-generated HTML, PostScript or PDF produced by some word processors for output purposes only
The “Title Page” means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, “Title Page” means the text near the most prominent appearance of the work’s title, preceding the beginning of the body of the text
The “publisher” means any person or entity that distributes copies of the Document to the public
A section “Entitled XYZ” means a named subunit of the Document whose title either is precisely XYZ or contains XYZ in parentheses following text that translates XYZ in another language. [Here XYZ stands for a specific section name mentioned below, such as “Acknowledgements”, “Dedications”, “Endorsements”, or “History”. ] To “Preserve the Title” of such a section when you modify the Document means that it remains a section “Entitled XYZ” according to this definition
The Document may include Warranty Disclaimers next to the notice which states that this License applies to the Document. These Warranty Disclaimers are considered to be included by reference in this License, but only as regards disclaiming warranties. any other implication that these Warranty Disclaimers may have is void and has no effect on the meaning of this License
- VERBATIM COPYING
You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this License, the copyright notices, and the license notice saying this License applies to the Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to obstruct or control the reading or further copying of the copies you make or distribute. However, you may accept compensation in exchange for copies. If you distribute a large enough number of copies you must also follow the conditions in section 3
You may also lend copies, under the same conditions stated above, and you may publicly display copies
- COPYING IN QUANTITY
If you publish printed copies [or copies in media that commonly have printed covers] of the Document, numbering more than 100, and the Document’s license notice requires Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all these Cover Texts. Front-Cover Texts on the front cover, and Back-Cover Texts on the back cover. Both covers must also clearly and legibly identify you as the publisher of these copies. The front cover must present the full title with all words of the title equally prominent and visible. You may add other material on the covers in addition. Copying with changes limited to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be treated as verbatim copying in other respects
If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed [as many as fit reasonably] on the actual cover, and continue the rest onto adjacent pages
If you publish or distribute Opaque copies of the Document numbering more than 100, you must either include a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a computer-network location from which the general network-using public has access to download using public-standard network protocols a complete Transparent copy of the Document, free of added material. If you use the latter option, you must take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy [directly or through your agents or retailers] of that edition to the public
It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document
- 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. Ngoài ra, bạn phải làm những điều này trong Phiên bản sửa đổi
- 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
- 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
- State on the Title page the name of the publisher of the Modified Version, as the publisher
- Preserve all the copyright notices of the Document
- Add an appropriate copyright notice for your modifications adjacent to the other copyright notices
- 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
- Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document’s license notice
- Include an unaltered copy of this License
- 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. Nếu không có phần Có tiêu đề “Lịch sử” trong Tài liệu, hãy tạo một mục nêu rõ tiêu đề, năm, tác giả và nhà xuất bản của Tài liệu như được đưa ra trên Trang tiêu đề của nó, sau đó thêm một mục mô tả Phiên bản đã sửa đổi như đã nêu trong câu trước
- 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
- 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
- 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
- Delete any section Entitled “Endorsements”. Such a section may not be included in the Modified Version
- Không đặt lại tiêu đề cho bất kỳ phần hiện có nào thành Có tiêu đề là “Chứng thực” hoặc xung đột về tiêu đề với bất kỳ Phần bất biến nào
- Bảo lưu bất kỳ Tuyên bố miễn trừ trách nhiệm bảo hành nào
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. Để thực hiện việc này, hãy thêm tiêu đề của chúng vào danh sách Phần bất biến trong thông báo giấy phép của Phiên bản đã sửa đổi. These titles must be distinct from any other section titles
You may add a section Entitled “Endorsements”, provided it contains nothing but endorsements of your Modified Version by various parties—for example, statements of peer review or that the text has been approved by an organization as the authoritative definition of a standard
You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover Text and one of Back-Cover Text may be added by [or through arrangements made by] any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one
The author[s] and publisher[s] of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modified Version
- 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. ”
- 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
- 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
- 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
- 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
- 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
- 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. Cách sử dụng Giấy phép này cho tài liệu của bạn
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.txt20
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.txt21
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.txt22 commands and command-line options