Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1. 3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License”
1 Introduction
sed -i 's/hello/world/' file.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
If you do not specify INPUTFILE, or if INPUTFILE is -,
sed -i 's/hello/world/' file.txt22 filters the contents of the standard input. The following commands are equivalent
sed -i 's/hello/world/' file.txt0
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 -i 's/hello/world/' file.txt9
Không có tùy chọn -e hoặc -f,
sed -i 's/hello/world/' file.txt22 sử dụng tham số không phải tùy chọn đầu tiên làm tập lệnh và các tham số không phải tùy chọn sau làm tệp đầu vào. Nếu các tùy chọn -e hoặc -f được sử dụng để chỉ định tập lệnh, tất cả các tham số không phải tùy chọn sẽ được lấy làm tệp đầu vào. Các tùy chọn -e và -f có thể được kết hợp và có thể xuất hiện nhiều lần [trong trường hợp đó, tập lệnh hiệu quả cuối cùng sẽ là tập lệnh nối của tất cả các tập lệnh riêng lẻ]
Các ví dụ sau là tương đương
sed -i 's/hello/world/' file.txt1
2. 2 tùy chọn dòng lệnh
Định dạng đầy đủ để gọi
sed -i 's/hello/world/' file.txt22 là
sed -i 's/hello/world/' file.txt3
sed -i 's/hello/world/' file.txt22 may be invoked with the following command-line options
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 How
sed -i 's/hello/world/' file.txt22 works]. 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
sed -i 's/hello/world/' file.txt6
sed -i 's/hello/world/' file.txt53
sed -i 's/hello/world/' file.txt54
Add the commands in script to the set of commands to be run while processing the input
sed -i 's/hello/world/' file.txt55
sed -i 's/hello/world/' file.txt56
Add the commands contained in the file script-file to the set of commands to be run while processing the input
sed -i 's/hello/world/' file.txt57
sed -i 's/hello/world/' file.txt58
This option specifies that files are to be edited in-place. GNU
sed -i 's/hello/world/' file.txt22 does this by creating a temporary file and sending output to this file rather than to the standard output. 1
This option implies -s
When the end of the file is reached, the temporary file is renamed to the output file’s original name. The extension, if supplied, is used to modify the name of the old file before renaming the temporary file, thereby making a backup copy2]
This rule is followed. if the extension doesn’t contain a
sed -i 's/hello/world/' file.txt60, then it is appended to the end of the current filename as a suffix; if the extension does contain one or more
sed -i 's/hello/world/' file.txt60 characters, then each asterisk is replaced with the current filename. This allows you to add a prefix to the backup file, instead of [or in addition to] a suffix, or even to place backup copies of the original files into another directory [provided the directory already exists]
If no extension is supplied, the original file is overwritten without making a backup
Because -i takes an optional argument, it should not be followed by other short options
sed -i 's/hello/world/' file.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
sed -i 's/hello/world/' file.txt9
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 Reporting Bugs] 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. Biểu thức chính quy mở rộng là những biểu thức mà
sed -i 's/hello/world/' file.txt82 chấp nhận; . Về mặt lịch sử, đây là một tiện ích mở rộng GNU, nhưng tiện ích mở rộng -E đã được thêm vào tiêu chuẩn POSIX [http. // austingroupbugs. lượt xem trên mạng. php?id=528], vì vậy hãy sử dụng -E cho tính di động. GNU sed đã chấp nhận -E như một tùy chọn không có giấy tờ trong nhiều năm và *BSD seds cũng đã chấp nhận -E trong nhiều năm, nhưng các tập lệnh sử dụng -E có thể không chuyển sang các hệ thống cũ hơn khác. Xem Biểu thức chính quy mở rộng
sed -i 's/hello/world/' file.txt83
sed -i 's/hello/world/' file.txt84
Theo mặc định,
sed -i 's/hello/world/' file.txt22 sẽ coi các tệp được chỉ định trên dòng lệnh là một luồng dài liên tục duy nhất. This GNU
sed -i 's/hello/world/' file.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. [Điều này đặc biệt hữu ích nếu đầu vào đến từ những thứ như 'tail -f' và bạn muốn xem đầu ra được chuyển đổi càng sớm càng tốt. ]
sed -i 's/hello/world/' file.txt94
sed -i 's/hello/world/' file.txt95
sed -i 's/hello/world/' file.txt96
Coi đầu vào là một tập hợp các dòng, mỗi dòng được kết thúc bằng một byte 0 [ký tự ASCII 'NUL'] thay vì một dòng mới. Tùy chọn này có thể được sử dụng với các lệnh như ‘sort -z’ và ‘find -print0’ để xử lý các tên tệp tùy ý
Nếu không có tùy chọn -e, -f, --expression hoặc --file nào được cung cấp trên dòng lệnh, thì đối số không phải tùy chọn đầu tiên trên dòng lệnh sẽ được coi là tập lệnh sẽ được thực thi
Nếu bất kỳ tham số dòng lệnh nào vẫn còn sau khi xử lý ở trên, các tham số này được hiểu là tên của tệp đầu vào sẽ được xử lý. A file name of ‘-’ refers to the standard input stream. Đầu vào tiêu chuẩn sẽ được xử lý nếu không có tên tệp nào được chỉ định
2. 3 Trạng thái thoát
Trạng thái thoát bằng 0 cho biết thành công và giá trị khác không cho biết thất bại. GNU
sed -i 's/hello/world/' file.txt22 trả về các giá trị lỗi trạng thái thoát sau0
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]. Xử lý tiếp tục với các tập tin khác
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 -i 's/hello/world/' file.txt900 và
sed -i 's/hello/world/' file.txt901 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]
sed 's/hello/world/' input.txt > output.txt9
3 tập lệnh sed -i 's/hello/world/' file.txt
22
3. 1 Tổng quan kịch bản sed -i 's/hello/world/' file.txt
22
Một 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 vào bởi một hoặc nhiều tùy chọn -e, -f, --expression và --file hoặc đối số không phải tùy chọn đầu tiên nếu không sử dụng các tùy chọn này. Tài liệu này sẽ đề cập đến “tập lệnh”
sed -i 's/hello/world/' file.txt22; . Xem Tổng quan
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 -i 's/hello/world/' file.txt911 là một địa chỉ dòng tùy chọn. Nếu
sed -i 's/hello/world/' file.txt911 được chỉ định, lệnh X sẽ chỉ được thực hiện trên các dòng phù hợp.
sed -i 's/hello/world/' file.txt911 có thể là số dòng đơn, biểu thức chính quy hoặc dải dòng [xem địa chỉ sed].
sed -i 's/hello/world/' file.txt914 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 -i 's/hello/world/' file.txt916 là một dải địa chỉ.
sed -i 's/hello/world/' file.txt36 là lệnh xóa
sed -i 's/hello/world/' file.txt04
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ẽ kết thúc với trạng thái thoát 42. Nếu dòng đó không được tìm thấy [và không có lỗi nào khác xảy ra],
sed -i 's/hello/world/' file.txt22 sẽ thoát với trạng thái 0.
sed -i 's/hello/world/' file.txt920 là một địa chỉ biểu thức chính quy.
sed -i 's/hello/world/' file.txt900 là lệnh thoát.
sed -i 's/hello/world/' file.txt922 là tùy chọn lệnh
sed -i 's/hello/world/' file.txt00
Các lệnh trong tập lệnh hoặc tệp tập lệnh có thể được phân tách bằng dấu chấm phẩy [
sed -i 's/hello/world/' file.txt923] 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 -i 's/hello/world/' file.txt920 và thay thế tất cả các lần xuất hiện của chuỗi 'xin chào' bằng 'thế giới'
sed -i 's/hello/world/' file.txt01
Các lệnh
sed -i 's/hello/world/' file.txt926,
sed -i 's/hello/world/' file.txt927,
sed -i 's/hello/world/' file.txt928, do cú pháp của chúng, không thể được theo sau bởi dấu chấm phẩy hoạt động như dấu phân cách lệnh và do đó nên được kết thúc bằng dòng mới hoặc được đặt ở cuối tập lệnh hoặc tệp tập lệnh. Các lệnh cũng có thể được đặt trước các ký tự khoảng trắng không quan trọng tùy chọn. Xem Cú pháp nhiều lệnh
3. 2 sed -i 's/hello/world/' file.txt
22 lệnh tóm tắt
Các lệnh sau được hỗ trợ trong GNU
sed -i 's/hello/world/' file.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 GNU. Chi tiết và ví dụ cho từng lệnh có trong các phần sau. [Mnemonics] được hiển thị trong ngoặc đơn
sed -i 's/hello/world/' file.txt931
sed -i 's/hello/world/' file.txt932
Nối văn bản sau một dòng
sed -i 's/hello/world/' file.txt933
Nối văn bản sau một dòng [cú pháp thay thế]
sed -i 's/hello/world/' file.txt934
Chi nhánh vô điều kiện để dán nhãn. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu
sed -i 's/hello/world/' file.txt935
sed -i 's/hello/world/' file.txt932
Thay thế [thay đổi] dòng bằng văn bản
sed -i 's/hello/world/' file.txt937
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 -i 's/hello/world/' file.txt939
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 -i 's/hello/world/' file.txt941
Thực thi lệnh được tìm thấy trong không gian mẫu và thay thế không gian mẫu bằng đầu ra;
sed -i 's/hello/world/' file.txt942
Executes command and sends its output to the output stream. The command can run across multiple lines, all but the last ending with a back-slash
sed -i 's/hello/world/' file.txt943
[filename] Print the file name of the current input file [with a trailing newline]
sed -i 's/hello/world/' file.txt944
Thay thế nội dung của không gian mẫu bằng nội dung của không gian giữ
sed -i 's/hello/world/' file.txt945
Nối một dòng mới vào nội dung của không gian mẫu, sau đó nối thêm nội dung của không gian giữ vào nội dung của không gian mẫu
sed -i 's/hello/world/' file.txt946
[giữ] Thay thế nội dung của không gian giữ bằng nội dung của không gian mẫu
sed -i 's/hello/world/' file.txt947
Nối một dòng mới vào nội dung của không gian giữ, sau đó nối thêm nội dung của không gian mẫu vào nội dung của không gian giữ
sed -i 's/hello/world/' file.txt948
sed -i 's/hello/world/' file.txt932
chèn văn bản trước một dòng
sed -i 's/hello/world/' file.txt950
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 -i 's/hello/world/' file.txt952
[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 dữ liệu đầ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 dữ liệu đầ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 -i 's/hello/world/' file.txt957
In không gian mẫu, cho đến mẫu đầu tiên
sed -i 's/hello/world/' file.txt958
[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 -i 's/hello/world/' file.txt960
[thoát] Lệnh này giống như lệnh
sed -i 's/hello/world/' file.txt900, nhưng sẽ không in nội dung của không gian mẫu. Giống như
sed -i 's/hello/world/' file.txt900, nó cung cấp khả năng trả lại mã thoát cho người gọi
sed -i 's/hello/world/' file.txt963
Đọc tên tập tin
sed -i 's/hello/world/' file.txt964
Xếp hàng một dòng tên tệp sẽ được đọc và chèn vào luồng đầu ra ở cuối chu kỳ hiện tại hoặc khi dòng đầu vào tiếp theo được đọc
sed -i 's/hello/world/' file.txt965
[thay thế] Khớp biểu thức chính quy với nội dung của không gian mẫu. Nếu tìm thấy, hãy thay thế chuỗi phù hợp bằng chuỗi thay thế
sed -i 's/hello/world/' file.txt966
[thử nghiệm] Chỉ nhánh để gắn nhãn nếu đã có một sự thay thế
sed -i 's/hello/world/' file.txt967 thành công kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu
sed -i 's/hello/world/' file.txt968
[thử nghiệm] Chỉ nhánh để gắn nhãn nếu không có thay thế
sed -i 's/hello/world/' file.txt967 thành công nào kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu
sed -i 's/hello/world/' file.txt970
[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 -i 's/hello/world/' file.txt973
Viết không gian mẫu vào tên tệp
sed -i 's/hello/world/' file.txt974
Ghi vào tên tệp đã cho phần không gian mẫu cho đến dòng mới đầu tiên
sed -i 's/hello/world/' file.txt975
Trao đổi nội dung của không gian giữ và mẫu
sed -i 's/hello/world/' file.txt976
Chuyển ngữ bất kỳ ký tự nào trong không gian mẫu khớp với bất kỳ ký tự nguồn nào với ký tự tương ứng trong ký tự đích
sed -i 's/hello/world/' file.txt977
[zap] Lệnh này làm trống nội dung của không gian mẫu
sed -i 's/hello/world/' file.txt978
Một nhận xét, cho đến dòng mới tiếp theo
sed -i 's/hello/world/' file.txt979
Nhóm một số lệnh lại với nhau
sed -i 's/hello/world/' file.txt980
In số dòng đầu vào hiện tại [với một dòng mới ở cuối]
sed -i 's/hello/world/' file.txt981
Chỉ định vị trí của nhãn cho các lệnh nhánh [______1982,
sed -i 's/hello/world/' file.txt983,
sed -i 's/hello/world/' file.txt984]
3. 3 Lệnh sed -i 's/hello/world/' file.txt
967
Lệnh
sed -i 's/hello/world/' file.txt967 [để 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 -i 's/hello/world/' file.txt967 là ‘s/regexp/replacement/flags’
Khái niệm cơ bản của nó là đơn giản. lệnh
sed -i 's/hello/world/' file.txt967 cố gắng khớp không gian mẫu với biểu thức chính quy được cung cấp;
Để biết chi tiết về cú pháp biểu thức chính quy, hãy xem Địa chỉ biểu thức chính quy
Phần thay thế có thể chứa các tham chiếu
sed -i 's/hello/world/' file.txt990 [n là một số từ 1 đến 9], tham chiếu đến phần trùng khớp được chứa giữa
sed -i 's/hello/world/' file.txt991 thứ n và
sed -i 's/hello/world/' file.txt992 trùng khớp của nó. Ngoài ra, phần thay thế có thể chứa các ký tự
sed -i 's/hello/world/' file.txt993 chưa thoát tham chiếu đến toàn bộ phần khớp của không gian mẫu
Các ký tự
sed -i 's/hello/world/' file.txt994 có thể được thay thế thống nhất bằng bất kỳ ký tự đơn nào khác trong bất kỳ lệnh
sed -i 's/hello/world/' file.txt967 nào. Ký tự
sed -i 's/hello/world/' file.txt994 [hoặc bất kỳ ký tự nào khác được sử dụng thay cho nó] chỉ có thể xuất hiện trong biểu thức chính quy hoặc thay thế nếu nó đứng trước ký tự
sed -i 's/hello/world/' file.txt997
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 bằng dấu gạch chéo ngược và một trong các chữ cái
sed -i 's/hello/world/' file.txt999,
sed -i 's/hello/world/' file.txt67,
sed -i 's/hello/world/' file.txt101,
sed -i 's/hello/world/' file.txt102 hoặc
sed -i 's/hello/world/' file.txt103. Ý nghĩa như sau
sed -i 's/hello/world/' file.txt104
Biến thay thế thành chữ thường cho đến khi tìm thấy
sed -i 's/hello/world/' file.txt105 hoặc
sed -i 's/hello/world/' file.txt106,
sed -i 's/hello/world/' file.txt107
Biến ký tự tiếp theo thành chữ thường,
sed -i 's/hello/world/' file.txt105
Chuyển từ thay thế sang chữ hoa cho đến khi tìm thấy
sed -i 's/hello/world/' file.txt104 hoặc
sed -i 's/hello/world/' file.txt106,
sed -i 's/hello/world/' file.txt111
Chuyển ký tự tiếp theo thành chữ hoa,
sed -i 's/hello/world/' file.txt106
Dừng chuyển đổi trường hợp bắt đầu bởi
sed -i 's/hello/world/' file.txt104 hoặc
sed -i 's/hello/world/' file.txt105
Khi cờ
sed -i 's/hello/world/' file.txt944 đang được sử dụng, chuyển đổi trường hợp không lan truyền từ một lần xuất hiện của biểu thức chính quy sang một lần khác. Ví dụ: khi lệnh sau được thực thi với 'a-b-' trong không gian mẫu
đầu ra là 'axxB'. Khi thay thế '-' đầu tiên, chuỗi '\ u' chỉ ảnh hưởng đến thay thế trống của '\ 1'. Nó không ảnh hưởng đến ký tự
sed -i 's/hello/world/' file.txt975 được thêm vào không gian mẫu khi thay thế
sed -i 's/hello/world/' file.txt117 bằng
sed -i 's/hello/world/' file.txt118
Mặt khác,
sed -i 's/hello/world/' file.txt107 và
sed -i 's/hello/world/' file.txt111 ảnh hưởng đến phần còn lại của văn bản thay thế nếu theo sau chúng là một thay thế trống. Với 'a-b-' trong không gian mẫu, lệnh sau
sẽ thay thế '-' bằng 'X' [chữ hoa] và 'b-' bằng 'Bx'. Nếu hành vi này là không mong muốn, bạn có thể ngăn chặn nó bằng cách thêm một chuỗi '\E'—sau '\1' trong trường hợp này
Để bao gồm một
sed -i 's/hello/world/' file.txt997,
sed -i 's/hello/world/' file.txt993 hoặc dòng mới trong phần thay thế cuối cùng, hãy đảm bảo đặt trước
sed -i 's/hello/world/' file.txt997,
sed -i 's/hello/world/' file.txt993 hoặc dòng mới mong muốn trong phần thay thế bằng một
sed -i 's/hello/world/' file.txt997
Lệnh
sed -i 's/hello/world/' file.txt967 có thể được theo sau bởi 0 hoặc nhiều cờ sau
sed -i 's/hello/world/' file.txt944
Áp dụng thay thế cho tất cả các kết quả phù hợp với biểu thức chính quy, không chỉ lần đầu tiên
sed -i 's/hello/world/' file.txt128
Chỉ thay thế trận đấu số của regrec
tương tác trong lệnh
sed -i 's/hello/world/' file.txt967 Lưu ý. tiêu chuẩn POSIX không chỉ định điều gì sẽ xảy ra khi bạn kết hợp các công cụ sửa đổi số và số
sed -i 's/hello/world/' file.txt944 và hiện tại không có ý nghĩa thống nhất rộng rãi trên các triển khai
sed -i 's/hello/world/' file.txt22. For GNU
sed -i 's/hello/world/' file.txt22, the interaction is defined to be. 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 -i 's/hello/world/' file.txt941 đượ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. In general,
sed -i 's/hello/world/' file.txt136 [evaluate then print] is what you want, but operating the other way round can be useful for debugging. For this reason, the current version of GNU
sed -i 's/hello/world/' file.txt22 interprets specially the presence of
sed -i 's/hello/world/' file.txt37 options both before and after
sed -i 's/hello/world/' file.txt941, printing the pattern space before and after evaluation, while in general flags for the
sed -i 's/hello/world/' file.txt967 command show their effect just once. This behavior, although documented, might change in future versions
sed -i 's/hello/world/' file.txt973
If the substitution was made, then write out the result to the named file. As a GNU
sed -i 's/hello/world/' file.txt22 extension, two special values of filename are supported. /dev/stderr, which writes the result to the standard error, and /dev/stdout, which writes to the standard output. 3
sed -i 's/hello/world/' file.txt941
This command allows one to pipe input from a shell command into pattern space. If a substitution was made, the command that is found in pattern space is executed and pattern space is replaced with its output. A trailing newline is suppressed; results are undefined if the command to be executed contains a NUL character. This is a GNU
sed -i 's/hello/world/' file.txt22 extension
sed -i 's/hello/world/' file.txt145
sed -i 's/hello/world/' file.txt928
The
sed -i 's/hello/world/' file.txt145 modifier to regular-expression matching is a GNU extension which makes
sed -i 's/hello/world/' file.txt22 match regexp in a case-insensitive manner
sed -i 's/hello/world/' file.txt149
sed -i 's/hello/world/' file.txt150
The
sed -i 's/hello/world/' file.txt149 modifier to regular-expression matching is a GNU
sed -i 's/hello/world/' file.txt22 extension which directs GNU
sed -i 's/hello/world/' file.txt22 to match the regular expression in multi-line mode. The modifier causes
sed -i 's/hello/world/' file.txt154 and
sed -i 's/hello/world/' file.txt87 to match respectively [in addition to the normal behavior] the empty string after a newline, and the empty string before a newline. There are special character sequences [
sed -i 's/hello/world/' file.txt156 and
sed -i 's/hello/world/' file.txt157] which always match the beginning or the end of the buffer. In addition, the period character does not match a new-line character in multi-line mode
3. 4 Often-Used Commands
If you use
sed -i 's/hello/world/' file.txt22 at all, you will quite likely want to know these commands
sed -i 's/hello/world/' file.txt978
[No addresses allowed. ]
The
sed -i 's/hello/world/' file.txt978 character begins a comment; the comment continues until the next newline
If you are concerned about portability, be aware that some implementations of
sed -i 's/hello/world/' file.txt22 [which are not POSIX conforming] may only support a single one-line comment, and then only when the very first character of the script is a
sed -i 's/hello/world/' file.txt978
Warning. if the first two characters of the
sed -i 's/hello/world/' file.txt22 script are
sed -i 's/hello/world/' file.txt164, then the -n [no-autoprint] option is forced. If you want to put a comment in the first line of your script and that comment begins with the letter ‘n’ and you do not want this behavior, then be sure to either use a capital ‘N’, or place at least one space before the ‘n’
sed -i 's/hello/world/' file.txt165
Exit
sed -i 's/hello/world/' file.txt22 without processing any more commands or input
Example. stop after printing the second line
This command accepts only one address. Note that the current pattern space is printed if auto-print is not disabled with the -n options. The ability to return an exit code from the
sed -i 's/hello/world/' file.txt22 script is a GNU
sed -i 's/hello/world/' file.txt22 extension
See also the GNU
sed -i 's/hello/world/' file.txt22 extension
sed -i 's/hello/world/' file.txt901 command which quits silently without printing the current pattern space
sed -i 's/hello/world/' file.txt36
Xóa không gian mẫu;
Example. delete the second input line
sed -i 's/hello/world/' file.txt37
Print out the pattern space [to the standard output]. This command is usually only used in conjunction with the -n command-line option
Example. print only the second input line
sed -i 's/hello/world/' file.txt952
If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then
sed -i 's/hello/world/' file.txt22 exits without processing any more commands
This command is useful to skip lines [e. g. process every Nth line]
Example. perform substitution on every 3rd line [i. e. two
sed -i 's/hello/world/' file.txt952 commands skip two lines]
sed -i 's/hello/world/' file.txt02
GNU
sed -i 's/hello/world/' file.txt22 provides an extension address syntax of first~step to achieve the same result
sed -i 's/hello/world/' file.txt03
sed -i 's/hello/world/' file.txt177
A group of commands may be enclosed between
sed -i 's/hello/world/' file.txt178 and
sed -i 's/hello/world/' file.txt179 characters. This is particularly useful when you want a group of commands to be triggered by a single address [or address-range] match
Example. perform substitution then print the second input line
sed -i 's/hello/world/' file.txt04
3. 5 Less Frequently-Used Commands
Though perhaps less frequently used than those in the previous section, some very small yet useful
sed -i 's/hello/world/' file.txt22 scripts can be built with these commands
sed -i 's/hello/world/' file.txt181
Chuyển ngữ bất kỳ ký tự nào trong không gian mẫu khớp với bất kỳ ký tự nguồn nào với ký tự tương ứng trong ký tự đích
Example. transliterate ‘a-j’ into ‘0-9’
sed -i 's/hello/world/' file.txt05
[The
sed -i 's/hello/world/' file.txt994 characters may be uniformly replaced by any other single character within any given
sed -i 's/hello/world/' file.txt183 command. ]
Instances of the
sed -i 's/hello/world/' file.txt994 [or whatever other character is used in its stead],
sed -i 's/hello/world/' file.txt997, or newlines can appear in the source-chars or dest-chars lists, provide that each instance is escaped by a
sed -i 's/hello/world/' file.txt997. The source-chars and dest-chars lists must contain the same number of characters [after de-escaping]
See the
sed -i 's/hello/world/' file.txt187 command from GNU coreutils for similar functionality
sed -i 's/hello/world/' file.txt933
Appending text after a line. This is a GNU extension to the standard
sed -i 's/hello/world/' file.txt926 command - see below for details
Example. Add the word ‘hello’ after the second line
sed -i 's/hello/world/' file.txt06
Leading whitespace after the
sed -i 's/hello/world/' file.txt926 command is ignored. The text to add is read until the end of the line
sed -i 's/hello/world/' file.txt931
sed -i 's/hello/world/' file.txt932
Appending text after a line
Example. Add ‘hello’ after the second line [-. indicates printed output lines]
sed -i 's/hello/world/' file.txt07
The
sed -i 's/hello/world/' file.txt926 command queues the lines of text which follow this command [each but the last ending with a
sed -i 's/hello/world/' file.txt997, which are removed from the output] to be output at the end of the current cycle, or when the next input line is read
As a GNU extension, this command accepts two addresses
Escape sequences in text are processed, so you should use
sed -i 's/hello/world/' file.txt195 in text to print a single backslash
The commands resume after the last line without a backslash [
sed -i 's/hello/world/' file.txt997] - ‘world’ in the following example
sed -i 's/hello/world/' file.txt08
As a GNU extension, the
sed -i 's/hello/world/' file.txt926 command and text can be separated into two
sed -i 's/hello/world/' file.txt198 parameters, enabling easier scripting
sed -i 's/hello/world/' file.txt09
sed -i 's/hello/world/' file.txt950
insert text before a line. This is a GNU extension to the standard
sed -i 's/hello/world/' file.txt928 command - see below for details
Example. Insert the word ‘hello’ before the second line
sed -i 's/hello/world/' file.txt0
Leading whitespace after the
sed -i 's/hello/world/' file.txt928 command is ignored. The text to add is read until the end of the line
sed -i 's/hello/world/' file.txt948
sed -i 's/hello/world/' file.txt932
Immediately output the lines of text which follow this command
Example. Insert ‘hello’ before the second line [-. indicates printed output lines]
sed -i 's/hello/world/' file.txt1
As a GNU extension, this command accepts two addresses
Escape sequences in text are processed, so you should use
sed -i 's/hello/world/' file.txt195 in text to print a single backslash
The commands resume after the last line without a backslash [
sed -i 's/hello/world/' file.txt997] - ‘world’ in the following example
sed -i 's/hello/world/' file.txt2
As a GNU extension, the
sed -i 's/hello/world/' file.txt928 command and text can be separated into two
sed -i 's/hello/world/' file.txt198 parameters, enabling easier scripting
sed -i 's/hello/world/' file.txt3
sed -i 's/hello/world/' file.txt937
Replaces the line[s] with text. This is a GNU extension to the standard
sed -i 's/hello/world/' file.txt927 command - see below for details
Example. Replace the 2nd to 9th lines with the word ‘hello’
sed -i 's/hello/world/' file.txt4
Leading whitespace after the
sed -i 's/hello/world/' file.txt927 command is ignored. The text to add is read until the end of the line
sed -i 's/hello/world/' file.txt935
sed -i 's/hello/world/' file.txt932
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’ [-. cho biết các dòng đầu ra được in]
sed -i 's/hello/world/' file.txt5
Nếu không có địa chỉ nào được cung cấp, mỗi dòng sẽ được thay thế
Một chu kỳ mới được bắt đầu sau khi lệnh này được thực hiện, vì không gian mẫu sẽ bị xóa. In the following example, the
sed -i 's/hello/world/' file.txt927 starts a new cycle and the substitution command is not performed on the replaced text
sed -i 's/hello/world/' file.txt6
As a GNU extension, the
sed -i 's/hello/world/' file.txt927 command and text can be separated into two
sed -i 's/hello/world/' file.txt198 parameters, enabling easier scripting
sed -i 's/hello/world/' file.txt7
sed -i 's/hello/world/' file.txt980
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 -i 's/hello/world/' file.txt317
Print the pattern space in an unambiguous form. non-printable characters [and the
sed -i 's/hello/world/' file.txt997 character] are printed in C-style escaped form; long lines are split, with a trailing
sed -i 's/hello/world/' file.txt997 character to indicate the split; the end of each line is marked with a
sed -i 's/hello/world/' file.txt87
n specifies the desired line-wrap length; a length of 0 [zero] means to never wrap long lines. If omitted, the default as specified on the command line is used. The n parameter is a GNU
sed -i 's/hello/world/' file.txt22 extension
sed -i 's/hello/world/' file.txt963
Reads file filename. Example
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 -i 's/hello/world/' file.txt973
Write the pattern space to filename. As a GNU
sed -i 's/hello/world/' file.txt22 extension, two special values of filename are supported. /dev/stderr, which writes the result to the standard error, and /dev/stdout, which writes to the standard output. 4
The file will be created [or truncated] before the first input line is read; all
sed -i 's/hello/world/' file.txt326 commands [including instances of the
sed -i 's/hello/world/' file.txt326 flag on successful
sed -i 's/hello/world/' file.txt967 commands] which refer to the same filename are output without closing and reopening the file
sed -i 's/hello/world/' file.txt939
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 dữ liệu đầ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 N command on the last line
sed -i 's/hello/world/' file.txt957
Print out the portion of the pattern space up to the first newline
sed -i 's/hello/world/' file.txt946
Replace the contents of the hold space with the contents of the pattern space
sed -i 's/hello/world/' file.txt947
Nối một dòng mới vào nội dung của không gian giữ, sau đó nối thêm nội dung của không gian mẫu vào nội dung của không gian giữ
sed -i 's/hello/world/' file.txt944
Thay thế nội dung của không gian mẫu bằng nội dung của không gian giữ
sed -i 's/hello/world/' file.txt945
Nối một dòng mới vào nội dung của không gian mẫu, sau đó nối thêm nội dung của không gian giữ vào nội dung của không gian mẫu
sed -i 's/hello/world/' file.txt975
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 -i 's/hello/world/' file.txt341 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 -i 's/hello/world/' file.txt981
[No addresses allowed. ]
Specify the location of label for branch commands. In all other respects, a no-op
sed -i 's/hello/world/' file.txt934
Unconditionally branch to label. The label may be omitted, in which case the next cycle is started
sed -i 's/hello/world/' file.txt966
Branch to label only if there has been a successful
sed -i 's/hello/world/' file.txt967ubstitution since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started
3. 7 Commands Specific to GNU sed -i 's/hello/world/' file.txt
22
These commands are specific to GNU
sed -i 's/hello/world/' file.txt22, so you must use them with care and only when you are sure that hindering portability is not evil. They allow you to check for GNU
sed -i 's/hello/world/' file.txt22 extensions or to do tasks that are required quite often, yet are unsupported by standard
sed -i 's/hello/world/' file.txt22s
sed -i 's/hello/world/' file.txt351
This command allows one to pipe input from a shell command into pattern space. Without parameters, the
sed -i 's/hello/world/' file.txt941 command executes the command that is found in pattern space and replaces the pattern space with the output; a trailing newline is suppressed
If a parameter is specified, instead, the
sed -i 's/hello/world/' file.txt941 command interprets it as a command and sends its output to the output stream. The command can run across multiple lines, all but the last ending with a back-slash
In both cases, the results are undefined if the command to be executed contains a NUL character
Note that, unlike the
sed -i 's/hello/world/' file.txt354 command, the output of the command will be printed immediately; the
sed -i 's/hello/world/' file.txt354 command instead delays the output to the end of the current cycle
sed -i 's/hello/world/' file.txt943
Print out the file name of the current input file [with a trailing newline]
sed -i 's/hello/world/' file.txt357
This command accepts only one address
This command is the same as
sed -i 's/hello/world/' file.txt900, but will not print the contents of pattern space. Like
sed -i 's/hello/world/' file.txt900, it provides the ability to return an exit code to the caller
This command can be useful because the only alternative ways to accomplish this apparently trivial function are to use the -n option [which can unnecessarily complicate your script] or resorting to the following snippet, which wastes time by reading the whole file without any visible effect
sed -i 's/hello/world/' file.txt90
sed -i 's/hello/world/' file.txt964
Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that if filename cannot be read, or if its end is reached, no line is appended, without any error indication
As with the
sed -i 's/hello/world/' file.txt354 command, the special value /dev/stdin is supported for the file name, which reads a line from the standard input
sed -i 's/hello/world/' file.txt968
Branch to label only if there have been no successful
sed -i 's/hello/world/' file.txt967ubstitutions since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started
sed -i 's/hello/world/' file.txt364
This command does nothing, but makes
sed -i 's/hello/world/' file.txt22 fail if GNU
sed -i 's/hello/world/' file.txt22 extensions are not supported, simply because other versions of
sed -i 's/hello/world/' file.txt22 do not implement it. In addition, you can specify the version of
sed -i 's/hello/world/' file.txt22 that your script requires, such as
sed -i 's/hello/world/' file.txt369. The default is
sed -i 's/hello/world/' file.txt370 because that is the first version that implemented this command
This command enables all GNU extensions even if
sed -i 's/hello/world/' file.txt72 is set in the environment
sed -i 's/hello/world/' file.txt974
Write to the given filename the portion of the pattern space up to the first newline. Everything said under the
sed -i 's/hello/world/' file.txt326 command about file handling holds here too
sed -i 's/hello/world/' file.txt977
This command empties the content of pattern space. It is usually the same as ‘s/. *//’, but is more efficient and works in the presence of invalid multibyte sequences in the input stream. POSIX mandates that such sequences are not matched by ‘. ’, so that there is no portable way to clear
sed -i 's/hello/world/' file.txt22’s buffers in the middle of the script in most multibyte locales [including UTF-8 locales]
3. 8 Multiple commands syntax
There are several methods to specify multiple commands in a
sed -i 's/hello/world/' file.txt22 program
Using newlines is most natural when running a sed script from a file [using the -f option]
On the command line, all
sed -i 's/hello/world/' file.txt22 commands may be separated by newlines. Alternatively, you may specify each command as an argument to an -e option
sed -i 's/hello/world/' file.txt91
A semicolon [‘;’] may be used to separate most simple commands
sed -i 's/hello/world/' file.txt92
The
sed -i 's/hello/world/' file.txt178,
sed -i 's/hello/world/' file.txt179,
sed -i 's/hello/world/' file.txt982,
sed -i 's/hello/world/' file.txt983,
sed -i 's/hello/world/' file.txt984,
sed -i 's/hello/world/' file.txt383 commands can be separated with a semicolon [this is a non-portable GNU
sed -i 's/hello/world/' file.txt22 extension]
sed -i 's/hello/world/' file.txt93
Labels used in
sed -i 's/hello/world/' file.txt982,
sed -i 's/hello/world/' file.txt983,
sed -i 's/hello/world/' file.txt984,
sed -i 's/hello/world/' file.txt383 commands are read until a semicolon. Leading and trailing whitespace is ignored. In the examples below the label is ‘x’. The first example works with GNU
sed -i 's/hello/world/' file.txt22. The second is a portable equivalent. For more information about branching and labels see Branching and flow control
sed -i 's/hello/world/' file.txt94
3. 8. 1 Commands Requiring a newline
The following commands cannot be separated by a semicolon and require a newline
sed -i 's/hello/world/' file.txt926,
sed -i 's/hello/world/' file.txt927,
sed -i 's/hello/world/' file.txt928 [append/change/insert]
All characters following
sed -i 's/hello/world/' file.txt926,
sed -i 's/hello/world/' file.txt927,
sed -i 's/hello/world/' file.txt928 commands are taken as the text to append/change/insert. Using a semicolon leads to undesirable results
sed -i 's/hello/world/' file.txt95
Separate the commands using -e or a newline
sed -i 's/hello/world/' file.txt96
Note that specifying the text to add [‘Hello’] immediately after
sed -i 's/hello/world/' file.txt926,
sed -i 's/hello/world/' file.txt927,
sed -i 's/hello/world/' file.txt928 is itself a GNU
sed -i 's/hello/world/' file.txt22 extension. A portable, POSIX-compliant alternative is
sed -i 's/hello/world/' file.txt97
sed -i 's/hello/world/' file.txt978 [comment]
All characters following ‘#’ until the next newline are ignored
sed -i 's/hello/world/' file.txt98
sed -i 's/hello/world/' file.txt354,
sed -i 's/hello/world/' file.txt88,
sed -i 's/hello/world/' file.txt326,
sed -i 's/hello/world/' file.txt33 [reading and writing files]
Các lệnh
sed -i 's/hello/world/' file.txt354,
sed -i 's/hello/world/' file.txt88,
sed -i 's/hello/world/' file.txt326,
sed -i 's/hello/world/' file.txt33 phân tích tên tệp cho đến cuối dòng. If whitespace, comments or semicolons are found, they will be included in the filename, leading to unexpected results
sed -i 's/hello/world/' file.txt99
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 -i 's/hello/world/' file.txt354,
sed -i 's/hello/world/' file.txt88,
sed -i 's/hello/world/' file.txt326,
sed -i 's/hello/world/' file.txt33 [chẳng hạn như 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. văn bản ; . Tệp bị thiếu và lỗi được âm thầm bỏ qua
sed -i 's/hello/world/' file.txt10
sed -i 's/hello/world/' file.txt941 [command execution]
Bất kỳ ký tự nào sau lệnh
sed -i 's/hello/world/' file.txt941 cho đến cuối dòng sẽ được gửi đến trình bao. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào lệnh trình bao, dẫn đến kết quả không mong muốn
sed -i 's/hello/world/' file.txt11
sed -i 's/hello/world/' file.txt617 [thay thế bằng cờ
sed -i 's/hello/world/' file.txt941 hoặc
sed -i 's/hello/world/' file.txt326]
Trong một lệnh thay thế, cờ
sed -i 's/hello/world/' file.txt326 ghi kết quả thay thế vào một tệp và cờ
sed -i 's/hello/world/' file.txt941 thực thi kết quả thay thế dưới dạng lệnh shell. Như với các lệnh
sed -i 's/hello/world/' file.txt622, chúng phải được kết thúc bằng một dòng mới. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào lệnh shell hoặc tên tệp, dẫn đến kết quả không mong muốn
sed -i 's/hello/world/' file.txt12
4 địa chỉ. chọn dòng
4. 1 Tổng quan về địa chỉ
Địa chỉ xác định [các] dòng lệnh
sed -i 's/hello/world/' file.txt22 sẽ được thực thi. Lệnh sau thay thế từ 'hello' bằng 'world' chỉ trên dòng 144
sed -i 's/hello/world/' file.txt13
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 -i 's/hello/world/' file.txt15
Một dải địa chỉ được chỉ định với hai địa chỉ được phân tách bằng dấu phẩy [
sed -i 's/hello/world/' file.txt624]. Địa chỉ có thể là số, biểu thức chính quy hoặc kết hợp cả hai. Lệnh sau thay thế từ 'xin chào' bằng 'thế giới' chỉ trong các dòng 4 đến 17 [đã bao gồm]
sed -i 's/hello/world/' file.txt16
Việc thêm ký tự
sed -i 's/hello/world/' file.txt625 vào cuối thông số địa chỉ [trước chữ cái lệnh] sẽ phủ nhận ý nghĩa của sự trùng khớp. Nghĩa là, nếu ký tự
sed -i 's/hello/world/' file.txt625 theo sau một địa chỉ hoặc một dải địa chỉ, thì chỉ những dòng không khớp với địa chỉ mới được chọn. Lệnh sau chỉ thay thế từ 'xin chào' bằng 'thế giới' trong các dòng không chứa từ 'quả táo'
sed -i 's/hello/world/' file.txt17
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 -i 's/hello/world/' file.txt18
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 -i 's/hello/world/' file.txt128
Chỉ định một số dòng sẽ chỉ khớp với dòng đó trong đầu vào. [Lưu ý rằng
sed -i 's/hello/world/' file.txt22 đếm các dòng liên tục trên tất cả các tệp đầu vào trừ khi các tùy chọn -i hoặc -s được chỉ định. ]
sed -i 's/hello/world/' file.txt87
Địa chỉ này khớp với dòng cuối cùng của tệp đầu vào cuối cùng hoặc dòng cuối cùng của mỗi tệp khi tùy chọn -i hoặc -s được chỉ định
sed -i 's/hello/world/' file.txt631
Tiện ích mở rộng GNU này khớp với mọi dòng bước bắt đầu bằng dòng đầu tiên. Cụ thể, các dòng sẽ được chọn khi tồn tại một n không âm sao cho số dòng hiện tại bằng đầu tiên + [n * bước]. Do đó, người ta sẽ sử dụng
sed -i 's/hello/world/' file.txt632 để chọn các dòng được đánh số lẻ và
sed -i 's/hello/world/' file.txt633 cho các dòng được đánh số chẵn;
Các lệnh sau minh họa việc sử dụng địa chỉ bước
sed -i 's/hello/world/' file.txt19
4. 3 dòng chọn bằng cách khớp văn bản
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à Biểu thức chính quy cơ bản [BRE]. 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 ở dạng cú pháp Biểu thức chính quy mở rộng [ERE]. Xem BRE so với ERE
sed -i 's/hello/world/' file.txt636
Điều này sẽ chọn bất kỳ dòng nào khớp với biểu thức chính quy. Nếu bản thân biểu thức chính quy bao gồm bất kỳ ký tự
sed -i 's/hello/world/' file.txt994 nào, thì mỗi ký tự đó phải được thoát bằng dấu gạch chéo ngược [
sed -i 's/hello/world/' file.txt997]
Lệnh sau in các dòng trong /etc/passwd kết thúc bằng 'bash'5
sed -i 's/hello/world/' file.txt30
Biểu thức chính quy trống '//' lặp lại kết quả khớp biểu thức chính quy cuối cùng [tương tự nếu biểu thức chính quy trống được truyền cho lệnh
sed -i 's/hello/world/' file.txt967]. 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ệ
sed -i 's/hello/world/' file.txt640
[Có thể thay thế
sed -i 's/hello/world/' file.txt641 bằng bất kỳ ký tự đơn nào khác. ]
Điều này cũng phù hợp với biểu thức chính quy biểu thức chính quy, nhưng cho phép một người sử dụng dấu phân cách khác với
sed -i 's/hello/world/' file.txt994. Điều này đặc biệt hữu ích nếu bản thân biểu thức chính quy chứa nhiều dấu gạch chéo, vì nó tránh được việc thoát tẻ nhạt của mỗi
sed -i 's/hello/world/' file.txt994. Nếu bản thân biểu thức chính quy bao gồm bất kỳ ký tự phân cách nào, thì mỗi ký tự đó phải được thoát bằng dấu gạch chéo ngược [
sed -i 's/hello/world/' file.txt997]
Các lệnh sau đây là tương đương. Họ in các dòng bắt đầu bằng '/home/alice/documents/'
sed -i 's/hello/world/' file.txt31
sed -i 's/hello/world/' file.txt645
sed -i 's/hello/world/' file.txt646
Công cụ sửa đổi
sed -i 's/hello/world/' file.txt145 để khớp biểu thức chính quy là một tiện ích mở rộng GNU khiến biểu thức chính quy được khớp theo cách không phân biệt chữ hoa chữ thường
Trong nhiều ngôn ngữ lập trình khác, chữ thường
sed -i 's/hello/world/' file.txt928 được sử dụng để đối sánh biểu thức chính quy không phân biệt chữ hoa chữ thường. Tuy nhiên, trong
sed -i 's/hello/world/' file.txt22,
sed -i 's/hello/world/' file.txt928 được sử dụng cho lệnh chèn [xem lệnh chèn]
Quan sát sự khác biệt giữa các ví dụ sau
Trong ví dụ này,
sed -i 's/hello/world/' file.txt651 là địa chỉ. biểu thức chính quy với công cụ sửa đổi
sed -i 's/hello/world/' file.txt145.
sed -i 's/hello/world/' file.txt36 là lệnh xóa
sed -i 's/hello/world/' file.txt32
Đây,
sed -i 's/hello/world/' file.txt654 là địa chỉ. một biểu thức chính quy.
sed -i 's/hello/world/' file.txt928 là lệnh chèn.
sed -i 's/hello/world/' file.txt36 là giá trị cần chèn. Sau đó, một dòng có 'd' được chèn phía trên dòng phù hợp
sed -i 's/hello/world/' file.txt33
sed -i 's/hello/world/' file.txt657
sed -i 's/hello/world/' file.txt658
The
sed -i 's/hello/world/' file.txt149 modifier to regular-expression matching is a GNU
sed -i 's/hello/world/' file.txt22 extension which directs GNU
sed -i 's/hello/world/' file.txt22 to match the regular expression in multi-line mode. The modifier causes
sed -i 's/hello/world/' file.txt154 and
sed -i 's/hello/world/' file.txt87 to match respectively [in addition to the normal behavior] the empty string after a newline, and the empty string before a newline. There are special character sequences [
sed -i 's/hello/world/' file.txt156 and
sed -i 's/hello/world/' file.txt157] which always match the beginning or the end of the buffer. In addition, the period character does not match a new-line character in multi-line mode
Địa chỉ regex hoạt động trên nội dung của không gian mẫu hiện tại. Nếu không gian mẫu bị thay đổi [ví dụ: với lệnh
sed -i 's/hello/world/' file.txt666], khớp biểu thức chính quy sẽ hoạt động trên văn bản đã thay đổi
Trong ví dụ sau, in tự động bị tắt với -n. Lệnh
sed -i 's/hello/world/' file.txt667 thay đổi các dòng chứa '2' thành 'X'. Lệnh
sed -i 's/hello/world/' file.txt668 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
sed -i 's/hello/world/' file.txt669, nó sẽ không khớp và sẽ không được in
sed -i 's/hello/world/' file.txt34
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 [
sed -i 's/hello/world/' file.txt624]. Một dải địa chỉ khớp với các dòng bắt đầu từ nơi địa chỉ đầu tiên khớp và tiếp tục cho đến khi địa chỉ thứ hai khớp [bao gồm]
sed -i 's/hello/world/' file.txt35
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 -i 's/hello/world/' file.txt36
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 -i 's/hello/world/' file.txt37
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;
sed -i 's/hello/world/' file.txt672
A line number of
sed -i 's/hello/world/' file.txt673 can be used in an address specification like
sed -i 's/hello/world/' file.txt672 so that
sed -i 's/hello/world/' file.txt22 will try to match regexp in the first input line too. Nói cách khác,
sed -i 's/hello/world/' file.txt672 tương tự như
sed -i 's/hello/world/' file.txt677, 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
sed -i 's/hello/world/' file.txt672 sẽ coi nó là kết thúc phạm vi, trong khi biểu mẫu
sed -i 's/hello/world/' file.txt677 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ỉ
sed -i 's/hello/world/' file.txt673 có ý nghĩa;
The following examples demonstrate the difference between starting with address 1 and 0
sed -i 's/hello/world/' file.txt38
sed -i 's/hello/world/' file.txt682
So khớp addr1 và N dòng sau addr1
sed -i 's/hello/world/' file.txt39
addr1 có thể là số dòng hoặc biểu thức chính quy
sed -i 's/hello/world/' file.txt683
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]
sed -i 's/hello/world/' file.txt60
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
sed -i 's/hello/world/' file.txt687 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 -i 's/hello/world/' file.txt154 [dấu mũ] trong biểu thức chính quy khớp với đầu dòng. Ký tự
sed -i 's/hello/world/' file.txt689 [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'
sed -i 's/hello/world/' file.txt61
Các phần sau giải thích ý nghĩa và cách sử dụng các ký tự đặc biệt trong biểu thức chính quy
5. 2 Biểu thức chính quy cơ bản [BRE] và mở rộng [ERE]
Biểu thức chính quy cơ bản và mở rộng là hai biến thể về cú pháp của mẫu đã chỉ định. Cú pháp Biểu thức chính quy cơ bản [BRE] là mặc định trong
sed -i 's/hello/world/' file.txt22 [và tương tự như vậy trong
sed -i 's/hello/world/' file.txt687]. 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]sed -i 's/hello/world/' file.txt62
sed -i 's/hello/world/' file.txt63Một hoặc nhiều ký tự 'a' theo sau là 'b' [dấu cộng là siêu ký tự đặc biệt]
sed -i 's/hello/world/' file.txt64
sed -i 's/hello/world/' file.txt65
5. 3 Tổng quan về cú pháp biểu thức chính quy cơ bản
Đây là một 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
sed -i 's/hello/world/' file.txt695
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
So 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à ký tự thông thường, ký tự đặc biệt trước
sed -i 's/hello/world/' file.txt997,
sed -i 's/hello/world/' file.txt689, biểu thức chính quy được nhóm [xem bên dưới] hoặc biểu thức dấu 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; . POSIX 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
sed -i 's/hello/world/' file.txt903 trong các ngữ cảnh này
sed -i 's/hello/world/' file.txt689
Khớp với bất kỳ ký tự nào, bao gồm cả dòng mới
sed -i 's/hello/world/' file.txt154
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 cho nội dung của mỗi dòng [xem Cách thức hoạt động của
sed -i 's/hello/world/' file.txt22]. Vì vậy, sẽ là một cách đơn giản hóa hữu ích khi coi
sed -i 's/hello/world/' file.txt907 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 -i 's/hello/world/' file.txt967
sed -i 's/hello/world/' file.txt154 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 -i 's/hello/world/' file.txt991 hoặc
sed -i 's/hello/world/' file.txt911]. Tuy nhiên, các tập lệnh di động nên tránh
sed -i 's/hello/world/' file.txt154 khi bắt đầu một biểu thức con, vì POSIX cho phép triển khai coi
sed -i 's/hello/world/' file.txt154 là một ký tự thông thường trong ngữ cảnh đó
sed -i 's/hello/world/' file.txt87
Nó giống như
sed -i 's/hello/world/' file.txt154, 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 -i 's/hello/world/' file.txt992 hoặc
sed -i 's/hello/world/' file.txt911] và việc sử dụng nó ở cuối biểu thức con là không khả dụng
sed -i 's/hello/world/' file.txt919
sed -i 's/hello/world/' file.txt920
Khớp với bất kỳ ký tự đơn nào trong danh sách. ví dụ:
sed -i 's/hello/world/' file.txt921 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ư
sed -i 's/hello/world/' file.txt922, khớp với bất kỳ ký tự nào giữa [bao gồm] char1 và char2. Xem Các lớp ký tự và Biểu thức khung
sed -i 's/hello/world/' file.txt923
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
sed -i 's/hello/world/' file.txt925
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
sed -i 's/hello/world/' file.txt927
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]
sed -i 's/hello/world/' file.txt929
Các trận đấu giữa i và j, bao gồm, trình tự
sed -i 's/hello/world/' file.txt930
So khớp nhiều hơn hoặc bằng i chuỗi
sed -i 's/hello/world/' file.txt931
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ư
sed -i 's/hello/world/' file.txt
932. điều này sẽ tìm kiếm không hoặc nhiều hơn toàn bộ chuỗi 'abcd', trong khised -i 's/hello/world/' file.txt
933 sẽ tìm kiếm 'abc' theo sau là không hoặc nhiều lần xuất hiện của 'd'. Lưu ý rằng POSIX 1003 yêu cầu hỗ trợ chosed -i 's/hello/world/' file.txt
932. 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 - Sử dụng tài liệu tham khảo trở lại [xem bên dưới]
sed -i 's/hello/world/' file.txt935
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
sed -i 's/hello/world/' file.txt936
So khớp nối của regexp1 và regexp2. Phép nối liên kết chặt chẽ hơn
sed -i 's/hello/world/' file.txt911,
sed -i 's/hello/world/' file.txt154 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
sed -i 's/hello/world/' file.txt940
So khớp với biểu thức con được đặt trong ngoặc đơn thứ
sed -i 's/hello/world/' file.txt941 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 -i 's/hello/world/' file.txt991 từ trái sang phải
sed -i 's/hello/world/' file.txt990
Khớp với ký tự xuống dòng
sed -i 's/hello/world/' file.txt944
Khớp char, trong đó char là một trong số ________ 087, ________ 060, ________ 3689, ________ 4948,
sed -i 's/hello/world/' file.txt997 hoặc
sed -i 's/hello/world/' file.txt154. Lưu ý rằng các chuỗi dấu gạch chéo ngược giống như chữ C duy nhất mà bạn có thể cho là được diễn giải là
sed -i 's/hello/world/' file.txt990 và
sed -i 's/hello/world/' file.txt195;
Note that the regular expression matcher is greedy, 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$’Điều này khớp với một chuỗi bao gồm hai chuỗi con bằng nhau được phân tách bằng một dòng mới
‘. \{9\}A$’Điều này khớp với chín ký tự theo sau là chữ 'A' ở cuối dòng
‘^. \{15\}A’Điều này khớp với phần đầu của một chuỗi chứa 16 ký tự, ký tự cuối cùng là 'A'
5. 4 Tổng quan về cú pháp biểu thức chính quy mở rộng
Sự khác biệt duy nhất giữa biểu thức chính quy cơ bản và mở rộng là trong hành vi của một vài ký tự. ‘?’, ‘+’, dấu ngoặc đơn, dấu ngoặc nhọn [‘{}’] và ‘. ’. Mặc dù các biểu thức chính quy cơ bản yêu cầu chúng phải được thoát nếu bạn muốn chúng hoạt động như các ký tự đặc biệt, nhưng khi sử dụng các biểu thức chính quy mở rộng, bạn phải thoát chúng nếu bạn muốn chúng khớp với một ký tự chữ. ‘. ’ đặc biệt ở đây vì ‘\. ' là một phần mở rộng GNU - các biểu thức chính quy cơ bản tiêu chuẩn không cung cấp chức năng của nó
ví dụ
sed -i 's/hello/world/' file.txt955
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?'
sed -i 's/hello/world/' file.txt956
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'
sed -i 's/hello/world/' file.txt957
becomes ‘a{3,}’ when using extended regular expressions. Nó phù hợp với ba hoặc nhiều chữ 'a'
sed -i 's/hello/world/' file.txt958
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'
sed -i 's/hello/world/' file.txt959
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
sed -i 's/hello/world/' file.txt960
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 cơ bản và mở rộng [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
sed -i 's/hello/world/' file.txt66
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/. ]
sed -i 's/hello/world/' file.txt67‘[. hàng năm. ]'
Ký tự chữ và số. ‘[. chữ cái. ]' và '[. chữ số. ]’;
‘[. chữ cái. ]'Ký tự chữ cái. ‘[. thấp hơn. ]' và '[. phía trên. ]’;
‘[. trống. ]'Ký tự trống. không gian và tab
‘[. cntrl. ]'Control characters. 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ố.
sed -i 's/hello/world/' file.txt962‘[. đồ thị. ]'
ký tự đồ họa. ‘[. hàng năm. ]' và '[. dấu câu. ]'
‘[. thấp hơn. ]'Chữ viết thường;
‘[. in. ]'Ký tự in được. ‘[. hàng năm. ]’, ‘[. dấu câu. ]', và dấu cách
‘[. dấu câu. ]'Ký tự dấu câu;
‘[. không gian. ]'Ký tự khoảng trắng. trong ngôn ngữ 'C', đây là tab, dòng mới, tab dọc, nguồn cấp dữ liệu biểu mẫu, xuống dòng và dấu cách
‘[. phía trên. ]'Chữ viết hoa. trong ngôn ngữ 'C' và mã hóa ký tự ASCII, đây là
sed -i 's/hello/world/' file.txt965‘[. x chữ số. ]'
chữ số thập lục phân.
sed -i 's/hello/world/' file.txt966
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
'-'represents the range if it’s not first or last in a list or the ending point of a range
'^'đạ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,
sed -i 's/hello/world/' file.txt689,
sed -i 's/hello/world/' file.txt948 và
sed -i 's/hello/world/' file.txt997 thường không có gì đặc biệt trong danh sách. Ví dụ:
sed -i 's/hello/world/' file.txt972 khớp với ‘\’ hoặc ‘*’, vì
sed -i 's/hello/world/' file.txt997 không có gì đặc biệt ở đây. Tuy nhiên, các chuỗi như
sed -i 's/hello/world/' file.txt974,
sed -i 's/hello/world/' file.txt975 và
sed -i 's/hello/world/' file.txt976 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 đó,
sed -i 's/hello/world/' file.txt948 là đặc biệt trong danh sách khi nó được theo sau bởi
sed -i 's/hello/world/' file.txt689,
sed -i 's/hello/world/' file.txt980 hoặc
sed -i 's/hello/world/' file.txt383. 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 -i 's/hello/world/' file.txt990 và
sed -i 's/hello/world/' file.txt953 được công nhận trong danh sách. Xem Thoát‘[. ’
đạ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 địa chỉ và lệnh
sed -i 's/hello/world/' file.txt967]
Chúng có thể được sử dụng trong cả biểu thức chính quy cơ bản và mở rộng [nghĩa là có hoặc không có tùy chọn -E/-r]
sed -i 's/hello/world/' file.txt985
Khớp với bất kỳ ký tự "từ" nào. Một ký tự "từ" là bất kỳ chữ cái hoặc chữ số hoặc ký tự gạch dưới
sed -i 's/hello/world/' file.txt68
sed -i 's/hello/world/' file.txt986
Khớp với bất kỳ ký tự "không phải từ" nào
sed -i 's/hello/world/' file.txt69
sed -i 's/hello/world/' file.txt987
Khớp với ranh giới từ;
sed -i 's/hello/world/' file.txt90
sed -i 's/hello/world/' file.txt988
Khớp ở mọi nơi nhưng trên một ranh giới từ;
sed -i 's/hello/world/' file.txt91
sed -i 's/hello/world/' file.txt989
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
sed -i 's/hello/world/' file.txt92
sed -i 's/hello/world/' file.txt990
So khớp các ký tự không phải khoảng trắng
sed -i 's/hello/world/' file.txt93
sed -i 's/hello/world/' file.txt991
Khớp với phần đầu của một từ
sed -i 's/hello/world/' file.txt94
sed -i 's/hello/world/' file.txt992
Khớp với phần cuối của một từ
sed -i 's/hello/world/' file.txt95
sed -i 's/hello/world/' file.txt156
Chỉ khớp khi bắt đầu không gian mẫu. Điều này khác với
sed -i 's/hello/world/' file.txt154 ở chế độ nhiều dòng
Compare the following two examples
sed -i 's/hello/world/' file.txt96
sed -i 's/hello/world/' file.txt157
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
tham chiếu ngược là các lệnh biểu thức chính quy đề cập đến một phần trước đó của biểu thức chính quy phù hợp. Back-references are specified with backslash and a single digit [e. g. ‘\1’]. Phần của biểu thức chính quy mà chúng đề cập đến được gọi là biểu thức con và được chỉ định bằng dấu ngoặc đơn
Tham chiếu ngược và biểu thức con được sử dụng trong hai trường hợp. trong mẫu tìm kiếm biểu thức chính quy và trong phần thay thế của lệnh
sed -i 's/hello/world/' file.txt967 [xem Địa chỉ biểu thức chính quy và Lệnh "s"]
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à '. ’ - any single character [being surrounded by parentheses makes it a subexpression]. The back-reference ‘\1’ asks to match the same content [same character] as the sub-expression
The command below matches words starting with any character, followed by the letter ‘o’, followed by the same character as the first
sed -i 's/hello/world/' file.txt97
Multiple subexpressions are automatically numbered from left-to-right. This command searches for 6-letter palindromes [the first three letters are 3 subexpressions, followed by 3 back-references in reverse order]
sed -i 's/hello/world/' file.txt98
Trong lệnh
sed -i 's/hello/world/' file.txt967, các tham chiếu ngược có thể được sử dụng trong phần thay thế để tham chiếu lại các biểu thức con trong phần biểu thức chính quy
The following example uses two subexpressions in the regular expression to match two space-separated words. The back-references in the replacement part prints the words in a different order
sed -i 's/hello/world/' file.txt99
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 escape6—that is, escapes that are applied to a character or sequence of characters that ordinarily are taken literally, and that
sed -i 's/hello/world/' file.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
sed 's/hello/world/' input.txt > output.txt902
Produces or matches a BEL character, that is an “alert” [ASCII 7]
sed 's/hello/world/' input.txt > output.txt903
Produces or matches a form feed [ASCII 12]
sed -i 's/hello/world/' file.txt990
Produces or matches a newline [ASCII 10]
sed 's/hello/world/' input.txt > output.txt905
Produces or matches a carriage return [ASCII 13]
sed -i 's/hello/world/' file.txt953
Produces or matches a horizontal tab [ASCII 9]
sed 's/hello/world/' input.txt > output.txt907
Produces or matches a so called “vertical tab” [ASCII 11]
sed 's/hello/world/' input.txt > output.txt908
Produces or matches CONTROL-x, where x is any character. The precise effect of ‘\cx’ is as follows. if x is a lower case letter, it is converted to upper case. Then bit 6 of the character [hex 40] is inverted. Thus ‘\cz’ becomes hex 1A, but ‘\c{’ becomes hex 3B, while ‘\c;’ becomes hex 7B
sed 's/hello/world/' input.txt > output.txt909
Produces or matches a character whose decimal ASCII value is xxx
sed 's/hello/world/' input.txt > output.txt910
Tạo hoặc khớp một ký tự có giá trị ASCII bát phân là xxx
sed 's/hello/world/' input.txt > output.txt911
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
sed -i 's/hello/world/' file.txt666 command and Address matching. Thus the follwing two commands are equivalent [‘0x5e’ is the hexadecimal ASCII value of the character ‘^’]
sed 's/hello/world/' input.txt > output.txt90
As are the following [‘0x5b’,‘0x5d’ are the hexadecimal ASCII values of ‘[’,‘]’, respectively]
sed 's/hello/world/' input.txt > output.txt91
However it is recommended to avoid such special characters due to unexpected edge-cases. For example, the following are not equivalent
sed 's/hello/world/' input.txt > output.txt92
5. 9 Multibyte characters and Locale Considerations
GNU
sed -i 's/hello/world/' file.txt22 processes valid multibyte characters in multibyte locales [e. g.
sed 's/hello/world/' input.txt > output.txt915]. 7
The following example uses the Greek letter Capital Sigma [Σ, Unicode code point
sed 's/hello/world/' input.txt > output.txt916]. In a
sed 's/hello/world/' input.txt > output.txt915 locale,
sed -i 's/hello/world/' file.txt22 correctly processes the Sigma as one character despite it being 2 octets [bytes]
sed 's/hello/world/' input.txt > output.txt93
To force
sed -i 's/hello/world/' file.txt22 to process octets separately, use the
sed 's/hello/world/' input.txt > output.txt920 locale [also known as the
sed 's/hello/world/' input.txt > output.txt921 locale]
sed 's/hello/world/' input.txt > output.txt94
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
sed 's/hello/world/' input.txt > output.txt923 is an incomplete multibyte character [shown here as �]. The regular expression ‘. ’ does not match it
sed 's/hello/world/' input.txt > output.txt95
Similarly, the ’catch-all’ regular expression ‘. *’ does not match the entire line
sed 's/hello/world/' input.txt > output.txt96
GNU
sed -i 's/hello/world/' file.txt22 offers the special
sed -i 's/hello/world/' file.txt977 command to clear the current pattern space regardless of invalid multibyte characters [i. e. it works like
sed 's/hello/world/' input.txt > output.txt926 but also removes invalid multibyte characters]
sed 's/hello/world/' input.txt > output.txt97
Alternatively, force the
sed 's/hello/world/' input.txt > output.txt920 locale to process each octet separately [every octet is a valid character in the
sed 's/hello/world/' input.txt > output.txt920 locale]
sed 's/hello/world/' input.txt > output.txt98
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
sed 's/hello/world/' input.txt > output.txt930 is an invalid multibyte sequence, while
sed 's/hello/world/' input.txt > output.txt931 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
sed 's/hello/world/' input.txt > output.txt933. Any content left in the pattern space [the invalid characters] are added to the hold space using the
sed -i 's/hello/world/' file.txt947 command. On the last line [
sed -i 's/hello/world/' file.txt87], the hold space is retrieved [
sed -i 's/hello/world/' file.txt975], newlines are removed [
sed 's/hello/world/' input.txt > output.txt937], and any remaining octets are printed unambiguously [
sed -i 's/hello/world/' file.txt67]. Thus, any invalid multibyte sequences are printed as octal values
sed 's/hello/world/' input.txt > output.txt99
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
sed 's/hello/world/' input.txt > output.txt920 locale and using octal escape sequences
sed -i 's/hello/world/' file.txt040
5. 9. 2 Upper/Lower case conversion
GNU
sed -i 's/hello/world/' file.txt22’s substitute command [
sed -i 's/hello/world/' file.txt967] supports upper/lower case conversions using
sed -i 's/hello/world/' file.txt105,
sed -i 's/hello/world/' file.txt104 codes. These conversions support multibyte characters
sed -i 's/hello/world/' file.txt041
See The "s" Command
5. 9. 3 Multibyte regexp character classes
In other locales, the sorting sequence is not specified, and ‘[a-d]’ might be equivalent to ‘[abcd]’ or to ‘[aBbCcDd]’, or it might fail to match any character, or the set of characters that it matches might even be erratic. To obtain the traditional interpretation of bracket expressions, you can use the ‘C’ locale by setting the
sed 's/hello/world/' input.txt > output.txt945 environment variable to the value ‘C’
sed -i 's/hello/world/' file.txt042
Their interpretation depends on the
sed 's/hello/world/' input.txt > output.txt946 locale; for example, ‘[[. hàng năm. ]]’ means the character class of numbers and letters in the current locale
TODO. hiển thị ví dụ về đối chiếu
sed -i 's/hello/world/' file.txt043
6 nâng cao sed -i 's/hello/world/' file.txt
22. chu kỳ và bộ đệm
6. 1 How sed -i 's/hello/world/' file.txt
22 Works
sed -i 's/hello/world/' file.txt22 maintains two data buffers. the active pattern space, and the auxiliary hold space. Both are initially empty
sed -i 's/hello/world/' file.txt22 operates by performing the following cycle on each line of input. 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. 8 Then the next cycle starts for the next input line
Unless special commands [like ‘D’] are used, the pattern space is deleted between two cycles. The hold space, on the other hand, keeps its data between cycles [see commands ‘h’, ‘H’, ‘x’, ‘g’, ‘G’ to move data between both buffers]
6. 2 Hold and Pattern Buffers
TODO
6. 3 Multiline techniques - using D,G,H,N,P to process multiple lines
Multiple lines can be processed as one buffer using the
sed -i 's/hello/world/' file.txt939,
sed -i 's/hello/world/' file.txt945,
sed -i 's/hello/world/' file.txt947,
sed -i 's/hello/world/' file.txt71,
sed -i 's/hello/world/' file.txt957. They are similar to their lowercase counterparts [
sed -i 's/hello/world/' file.txt36,
sed -i 's/hello/world/' file.txt944,
sed -i 's/hello/world/' file.txt946,
sed -i 's/hello/world/' file.txt952,
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 -i 's/hello/world/' file.txt939
deletes line from the pattern space until the first newline, and restarts the cycle
sed -i 's/hello/world/' file.txt945
appends line from the hold space to the pattern space, with a newline before it
sed -i 's/hello/world/' file.txt947
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 -i 's/hello/world/' file.txt957
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 -i 's/hello/world/' file.txt939 commands
sed -i 's/hello/world/' file.txt044
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 -i 's/hello/world/' file.txt
939 command then removes the content of pattern space up to the first newline [leaving ‘2’ at the end of the first cycle] - At the next cycle the
sed -i 's/hello/world/' file.txt
71 command appends a newline and the next input line to the pattern space [e. g. ‘2’, ‘\n’, ‘3’]
A common technique to process blocks of text such as paragraphs [instead of line-by-line] is using the following construct
sed -i 's/hello/world/' file.txt045
- The first expression,
sed 's/hello/world/' input.txt > output.txt
974 operates on all non-empty lines, and adds the current line [in the pattern space] to the hold space. On all lines except the last, the pattern space is deleted and the cycle is restarted - The other expressions
sed -i 's/hello/world/' file.txt
975 andsed -i 's/hello/world/' file.txt
967 are executed only on empty lines [i. e. paragraph separators]. Thesed -i 's/hello/world/' file.txt
975 command fetches the accumulated lines from the hold space back to the pattern space. Thesed -i 's/hello/world/' file.txt
666 command then operates on all the text in the paragraph [including the embedded newlines]
The following example demonstrates this technique
sed -i 's/hello/world/' file.txt046
For more annotated examples, see Text search across multiple lines and Line length adjustment
6. 4 Branching and Flow Control
The branching commands
sed -i 's/hello/world/' file.txt982,
sed -i 's/hello/world/' file.txt983, and
sed -i 's/hello/world/' file.txt984 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 Execution Cycle and Addresses overview
sed -i 's/hello/world/' file.txt22 does not support a typical
sed 's/hello/world/' input.txt > output.txt985 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 -i 's/hello/world/' file.txt939
delete the contents of the pattern space up to the first newline, and restart the program cycle without processing the rest of the commands and without printing the pattern space
sed 's/hello/world/' input.txt > output.txt988
sed 's/hello/world/' input.txt > output.txt989
sed 's/hello/world/' input.txt > output.txt990
sed 's/hello/world/' input.txt > output.txt991
Addresses and regular expressions can be used as an
sed 's/hello/world/' input.txt > output.txt985 conditional. If [addr] matches the current pattern space, execute the command[s]. For example. The command
sed 's/hello/world/' input.txt > output.txt993 means. if the current pattern matches the regular expression
sed 's/hello/world/' input.txt > output.txt994 [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 -i 's/hello/world/' file.txt982
branch unconditionally [that is. always jump to a label, skipping or repeating other commands, without restarting a new cycle]. Combined with an address, the branch can be conditionally executed on matched lines
sed -i 's/hello/world/' file.txt983
branch conditionally [that is. jump to a label] only if a
sed -i 's/hello/world/' file.txt666 command has succeeded since the last input line was read or another conditional branch was taken
sed -i 's/hello/world/' file.txt984
similar but opposite to the
sed -i 's/hello/world/' file.txt983 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 -i 's/hello/world/' file.txt982 command to skip the
sed -i 's/hello/world/' file.txt666 command on lines containing ‘1’. The second example uses an address with negation [‘. ’] to perform substitution only on desired lines. The
sed -i 's/hello/world/' file.txt0404 command is still executed on all lines
sed -i 's/hello/world/' file.txt047
6. 4. 1 Branching and Cycles
The
sed -i 's/hello/world/' file.txt982,
sed -i 's/hello/world/' file.txt983 and
sed -i 's/hello/world/' file.txt984 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 -i 's/hello/world/' file.txt982 command [the only command in the program] does not have a label, and thus simply restarts the cycle. On each cycle, the pattern space is printed and the next input line is read
The following example is an infinite-loop - it doesn’t terminate and doesn’t print anything. The
sed -i 's/hello/world/' file.txt982 command jumps to the ‘x’ label, and a new cycle is never started
sed -i 's/hello/world/' file.txt048
Branching is often complemented with the
sed -i 's/hello/world/' file.txt952 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. Trước khi đọc dòng đầu vào tiếp theo,
sed -i 's/hello/world/' file.txt952 in không gian mẫu hiện tại rồi làm trống nó, trong khi
sed -i 's/hello/world/' file.txt71 nối một dòng mới và dòng đầu vào tiếp theo vào không gian mẫu
Consider the following two examples
sed -i 's/hello/world/' file.txt049
- Both examples do not inf-loop, despite never starting a new cycle
- In the first example, the
sed -i 's/hello/world/' file.txt
952 commands first prints the content of the pattern space, empties the pattern space then reads the next input line - In the second example, the
sed -i 's/hello/world/' file.txt
71 commands appends the next input line to the pattern space [with a newline]. 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 Reporting Bugs - To further examine the difference between the two examples, try the following commands
sed -i 's/hello/world/' file.txt
000
6. 4. 2 Branching example. joining lines
As a real-world example of using branching, consider the case of quoted-printable files, typically used to encode email messages. In these files long lines are split and marked with a soft line break consisting of a single ‘=’ character at the end of the line
sed -i 's/hello/world/' file.txt001
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 -i 's/hello/world/' file.txt982] to the beginning of the program without restarting a new cycle. If the pattern space does not ends with ‘=’, the default action is performed. không gian mẫu được in và một chu kỳ mới được bắt đầu
sed -i 's/hello/world/' file.txt002
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 -i 's/hello/world/' file.txt983 jumps to the beginning of the program without completing or restarting the cycle. If the substitution failed [meaning there were no soft line breaks], The
sed -i 's/hello/world/' file.txt983 command will not branch. Then,
sed -i 's/hello/world/' file.txt957 will print the pattern space content until the first newline, and
sed -i 's/hello/world/' file.txt939 will delete the pattern space content until the first new line. [To learn more about
sed -i 's/hello/world/' file.txt71,
sed -i 's/hello/world/' file.txt957 and
sed -i 's/hello/world/' file.txt939 commands see Multiline techniques]
sed -i 's/hello/world/' file.txt003
For more line-joining examples see Joining lines
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 -i 's/hello/world/' file.txt939 and
sed -i 's/hello/world/' file.txt957 commands to process multiple lines, and the
sed -i 's/hello/world/' file.txt982 and
sed -i 's/hello/world/' file.txt983 commands for branching. See Multiline techniques and Branching and flow control
Join specific lines [e. g. if lines 2 and 3 need to be joined]
sed -i 's/hello/world/' file.txt004
Join backslash-continued lines
sed -i 's/hello/world/' file.txt005
Join lines that start with whitespace [e. g SMTP headers]
sed -i 's/hello/world/' file.txt006
7. 2 Centering Lines
This script centers all lines of a file on a 80 columns width. To change that width, the number in
sed -i 's/hello/world/' file.txt0439 must be replaced, and the number of added spaces also must be changed
Note how the buffer commands are used to separate parts in the regular expressions to be matched—this is a common technique
sed -i 's/hello/world/' file.txt007
sed -i 's/hello/world/' file.txt008
sed -i 's/hello/world/' file.txt009
sed -i 's/hello/world/' file.txt010
sed -i 's/hello/world/' file.txt011
sed -i 's/hello/world/' file.txt012
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,9 but must be done manually
To increment one number you just add 1 to last digit, replacing it by the following digit. There is one exception. when the digit is a nine the previous digits must be also incremented until you don’t have a nine
This solution by Bruno Haible is very clever and smart because it uses a single buffer; if you don’t have this limitation, the algorithm used in Numbering lines, is faster. Nó hoạt động bằng cách thay thế các số 9 ở cuối bằng dấu gạch dưới, sau đó sử dụng nhiều lệnh
sed -i 's/hello/world/' file.txt967 để tăng chữ số cuối cùng, sau đó lại thay thế dấu gạch dưới bằng số không
sed -i 's/hello/world/' file.txt013
sed -i 's/hello/world/' file.txt014
sed -i 's/hello/world/' file.txt015
sed -i 's/hello/world/' file.txt016
sed -i 's/hello/world/' file.txt017
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 -i 's/hello/world/' file.txt0444 into a
sed -i 's/hello/world/' file.txt0445 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 -i 's/hello/world/' file.txt018
sed -i 's/hello/world/' file.txt019
sed -i 's/hello/world/' file.txt020
sed -i 's/hello/world/' file.txt021
sed -i 's/hello/world/' file.txt022
sed -i 's/hello/world/' file.txt023
sed -i 's/hello/world/' file.txt024
sed -i 's/hello/world/' file.txt025
sed -i 's/hello/world/' file.txt026
sed -i 's/hello/world/' file.txt027
sed -i 's/hello/world/' file.txt028
sed -i 's/hello/world/' file.txt029
sed -i 's/hello/world/' file.txt030
sed -i 's/hello/world/' file.txt031
sed -i 's/hello/world/' file.txt032
sed -i 's/hello/world/' file.txt033
sed -i 's/hello/world/' file.txt034
sed -i 's/hello/world/' file.txt035
sed -i 's/hello/world/' file.txt036
sed -i 's/hello/world/' file.txt037
sed -i 's/hello/world/' file.txt038
sed -i 's/hello/world/' file.txt039
sed -i 's/hello/world/' file.txt040
7. 5 Print sed -i 's/hello/world/' file.txt
0447 Environment
This script strips the definition of the shell functions from the output of the
sed -i 's/hello/world/' file.txt0448 Bourne-shell command
sed -i 's/hello/world/' file.txt041
sed -i 's/hello/world/' file.txt042
sed -i 's/hello/world/' file.txt043
sed -i 's/hello/world/' file.txt044
sed -i 's/hello/world/' file.txt045
sed -i 's/hello/world/' file.txt046
sed -i 's/hello/world/' file.txt047
7. 6 Reverse Characters of Lines
This script can be used to reverse the position of characters in lines. The technique moves two characters at a time, hence it is faster than more intuitive implementations
Note the
sed -i 's/hello/world/' file.txt0449 command before the definition of the label. This is often needed to reset the flag that is tested by the
sed -i 's/hello/world/' file.txt983 command
Imaginative readers will find uses for this script. An example is reversing the output of
sed -i 's/hello/world/' file.txt0451. 10
sed -i 's/hello/world/' file.txt048
sed -i 's/hello/world/' file.txt049
sed -i 's/hello/world/' file.txt050
sed -i 's/hello/world/' file.txt051
7. 7 Text search across multiple lines
This section uses
sed -i 's/hello/world/' file.txt71 and
sed -i 's/hello/world/' file.txt939 commands to search for consecutive words spanning multiple lines. See Multiline techniques
These examples deal with finding doubled occurrences of words in a document
Finding doubled words in a single line is easy using GNU
sed -i 's/hello/world/' file.txt687 and similarly with GNU
sed -i 's/hello/world/' file.txt22
sed -i 's/hello/world/' file.txt052
- The regular expression ‘\b\w+\s+’ searches for word-boundary [‘\b’], followed by one-or-more word-characters [‘\w+’], followed by whitespace [‘\s+’]. See regexp extensions
- Adding parentheses around the ‘[\w+]’ expression creates a subexpression. The regular expression pattern ‘[PATTERN]\s+\1’ defines a subexpression [in the parentheses] followed by a back-reference, separated by whitespace. A successful match means the PATTERN was repeated twice in succession. See Back-references and Subexpressions
- The word-boundery expression [‘\b’] at both ends ensures partial words are not matched [e. g. ‘the then’ is not a desired match]
- The -E option enables extended regular expression syntax, alleviating the need to add backslashes before the parenthesis. See ERE syntax
When the doubled word span two lines the above regular expression will not find them as
sed -i 's/hello/world/' file.txt687 and
sed -i 's/hello/world/' file.txt22 operate line-by-line
By using
sed -i 's/hello/world/' file.txt71 and
sed -i 's/hello/world/' file.txt939 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 -i 's/hello/world/' file.txt053
- The
sed -i 's/hello/world/' file.txt
71 command appends the next line to the pattern space [thus ensuring it contains two consecutive lines in every cycle] - The regular expression uses ‘\s+’ for word separator which matches both spaces and newlines
- The regular expression matches, the entire pattern space is printed with
sed -i 's/hello/world/' file.txt
37. No lines are printed by default due to the -n option - The
sed -i 's/hello/world/' file.txt
939 removes the first line from the pattern space [up until the first newline], readying it for the next cycle
See the GNU
sed -i 's/hello/world/' file.txt0464 manual for an alternative solution using
sed -i 's/hello/world/' file.txt0465 and
sed -i 's/hello/world/' file.txt0466 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 -i 's/hello/world/' file.txt939 commands to search for consecutive words spanning multiple lines, and the
sed -i 's/hello/world/' file.txt982 command for branching. See Multiline techniques and Branching and flow control
This [somewhat contrived] example deal with formatting and wrapping lines of text of the following input file
sed -i 's/hello/world/' file.txt054
The following sed program wraps lines at 40 characters
sed -i 's/hello/world/' file.txt055
đầu ra được bọc
sed -i 's/hello/world/' file.txt056
7. 9 Reverse Lines of Files
This one begins a series of totally useless [yet interesting] scripts emulating various Unix commands. This, in particular, is a
sed -i 's/hello/world/' file.txt0470 workalike
Note that on implementations other than GNU
sed -i 's/hello/world/' file.txt22 this script might easily overflow internal buffers
sed -i 's/hello/world/' file.txt057
sed -i 's/hello/world/' file.txt058
sed -i 's/hello/world/' file.txt059
sed -i 's/hello/world/' file.txt060
7. 10 Numbering Lines
This script replaces ‘cat -n’; in fact it formats its output exactly like GNU
sed -i 's/hello/world/' file.txt0472 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 -i 's/hello/world/' file.txt061
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 -i 's/hello/world/' file.txt183 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 -i 's/hello/world/' file.txt062
sed -i 's/hello/world/' file.txt063
sed -i 's/hello/world/' file.txt064
sed -i 's/hello/world/' file.txt065
sed -i 's/hello/world/' file.txt066
sed -i 's/hello/world/' file.txt067
sed -i 's/hello/world/' file.txt068
sed -i 's/hello/world/' file.txt069
sed -i 's/hello/world/' file.txt070
7. 11 Numbering Non-blank Lines
Emulating ‘cat -b’ is almost the same as ‘cat -n’—we only have to select which lines are to be numbered and which are not
The part that is common to this script and the previous one is not commented to show how important it is to comment
sed -i 's/hello/world/' file.txt22 scripts properly
sed -i 's/hello/world/' file.txt062
sed -i 's/hello/world/' file.txt072
sed -i 's/hello/world/' file.txt073
7. 12 Counting Characters
This script shows another way to do arithmetic with
sed -i 's/hello/world/' file.txt22. In this case we have to add possibly large numbers, so implementing this by successive increments would not be feasible [and possibly even more complicated to contrive than this script]
The approach is to map numbers to letters, kind of an abacus implemented with
sed -i 's/hello/world/' file.txt22. ‘a’s are units, ‘b’s are tens and so on. we simply add the number of characters on the current line as units, and then propagate the carry to tens, hundreds, and so on
As usual, running totals are kept in hold space
On the last line, we convert the abacus form back to decimal. For the sake of variety, this is done with a loop rather than with some 80
sed -i 's/hello/world/' file.txt967 commands11. 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 -i 's/hello/world/' file.txt062
sed -i 's/hello/world/' file.txt075
sed -i 's/hello/world/' file.txt076
sed -i 's/hello/world/' file.txt077
sed -i 's/hello/world/' file.txt078
sed -i 's/hello/world/' file.txt079
sed -i 's/hello/world/' file.txt080
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 -i 's/hello/world/' file.txt0480 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 -i 's/hello/world/' file.txt062
sed -i 's/hello/world/' file.txt082
sed -i 's/hello/world/' file.txt083
sed -i 's/hello/world/' file.txt084
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. Nhìn
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 -i 's/hello/world/' file.txt900 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 -i 's/hello/world/' file.txt0470 script in that it keeps the final output in the hold space and prints it at the end
sed -i 's/hello/world/' file.txt062
sed -i 's/hello/world/' file.txt086
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 -i 's/hello/world/' file.txt939 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 -i 's/hello/world/' file.txt957 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 -i 's/hello/world/' file.txt957 and
sed -i 's/hello/world/' file.txt939 commands, here is an implementation of
sed -i 's/hello/world/' file.txt0492 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 -i 's/hello/world/' file.txt939 to slide pattern space by one line
sed -i 's/hello/world/' file.txt007
sed -i 's/hello/world/' file.txt088
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 -i 's/hello/world/' file.txt957 and
sed -i 's/hello/world/' file.txt939 commands, probably the most difficult to master
sed -i 's/hello/world/' file.txt089
sed -i 's/hello/world/' file.txt090
sed -i 's/hello/world/' file.txt091
sed -i 's/hello/world/' file.txt092
As you can see, we maintain a 2-line window using
sed -i 's/hello/world/' file.txt957 and
sed -i 's/hello/world/' file.txt939. 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 -i 's/hello/world/' file.txt062
sed -i 's/hello/world/' file.txt094
sed -i 's/hello/world/' file.txt095
sed -i 's/hello/world/' file.txt096
sed -i 's/hello/world/' file.txt097
7. 19 Remove All Duplicated Lines
This script prints only unique lines, like ‘uniq -u’
sed -i 's/hello/world/' file.txt007
sed -i 's/hello/world/' file.txt099
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 -i 's/hello/world/' file.txt007
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 -i 's/hello/world/' file.txt007
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 -i 's/hello/world/' file.txt952 and
sed -i 's/hello/world/' file.txt982, without relying on
sed -i 's/hello/world/' file.txt22 to restart the script automatically at the end of a line
sed -i 's/hello/world/' file.txt062
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 -i 's/hello/world/' file.txt0008 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 lỗi báo cáo
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 in không gian mẫu trước khi thoát, tất nhiên trừ khi công tắc lệnh
sed -i 's/hello/world/' file.txt45 đã được chỉ định. This choice is by design
Default behavior [gnu extension, non-POSIX conforming]
To force POSIX-conforming behavior
sed -i 's/hello/world/' file.txt16
Ví dụ, hành vi của
would depend on whether foo has an even or an odd number of lines12. Or, when writing a script to read the next few lines following a pattern match, traditional implementations of
sed -i 's/hello/world/' file.txt22 would force you to write something like
sed -i 's/hello/world/' file.txt17
Thay vì chỉ
sed -i 's/hello/world/' file.txt18
In any case, the simplest workaround is to use
sed -i 's/hello/world/' file.txt0024 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
sed -i 's/hello/world/' file.txt911,
sed -i 's/hello/world/' file.txt923,
sed -i 's/hello/world/' file.txt925,
sed -i 's/hello/world/' file.txt156,
sed -i 's/hello/world/' file.txt157,
sed -i 's/hello/world/' file.txt991,
sed -i 's/hello/world/' file.txt992,
sed -i 's/hello/world/' file.txt987,
sed -i 's/hello/world/' file.txt988,
sed -i 's/hello/world/' file.txt985, and
sed -i 's/hello/world/' file.txt986
As in all GNU programs that use POSIX basic regular expressions,
sed -i 's/hello/world/' file.txt22 interprets these escape sequences as special characters. So,
sed -i 's/hello/world/' file.txt0040 matches one or more occurrences of ‘x’.
sed -i 's/hello/world/' file.txt0041 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
sed -i 's/hello/world/' file.txt911 and
sed -i 's/hello/world/' file.txt923 match the literal characters
sed -i 's/hello/world/' file.txt0046 and
sed -i 's/hello/world/' file.txt0047. Such scripts must be modified by removing the spurious backslashes if they are to be used with modern implementations of
sed -i 's/hello/world/' file.txt22, like 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 -i 's/hello/world/' file.txt0050 or
sed -i 's/hello/world/' file.txt0051. While this worked until
sed -i 's/hello/world/' file.txt22 4. 0. x, newer versions interpret this as removing the string
sed -i 's/hello/world/' file.txt0053. 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 -i 's/hello/world/' file.txt0055 as
sed -i 's/hello/world/' file.txt994 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 [
sed 's/hello/world/' input.txt > output.txt902,
sed -i 's/hello/world/' file.txt0059,
sed -i 's/hello/world/' file.txt0060,
sed -i 's/hello/world/' file.txt0061,
sed 's/hello/world/' input.txt > output.txt905,
sed -i 's/hello/world/' file.txt953,
sed 's/hello/world/' input.txt > output.txt907,
sed -i 's/hello/world/' file.txt0065]. 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 Invocation] 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 -i 's/hello/world/' file.txt0068 does not work [gives an error]
Không có dòng 0. 0 is a special address that is only used to treat addresses like
sed -i 's/hello/world/' file.txt0069 as active when the script starts. if you write
sed -i 's/hello/world/' file.txt0070 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 -i 's/hello/world/' file.txt0071
sed -i 's/hello/world/' file.txt0072 is case insensitive
You are encountering problems with locales. POSIX mandates that
sed -i 's/hello/world/' file.txt0072 uses the current locale’s collation order – in C parlance, that means using
sed -i 's/hello/world/' file.txt0074 instead of
sed -i 's/hello/world/' file.txt0075. Some locales have a case-insensitive collation order, others don’t
Another problem is that
sed -i 's/hello/world/' file.txt0072 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 -i 's/hello/world/' file.txt0077 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 -i 's/hello/world/' file.txt0078 and
sed 's/hello/world/' input.txt > output.txt946 environment variables to ‘C’
sed 's/hello/world/' input.txt > output.txt926 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 -i 's/hello/world/' file.txt0078 and
sed 's/hello/world/' input.txt > output.txt946 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
A “Transparent” copy of the Document means a machine-readable copy, represented in a format whose specification is available to the general public, that is suitable for revising the document straightforwardly with generic text editors or [for images composed of pixels] generic paint programs or [for drawings] some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup, or absence of markup, has been arranged to thwart or discourage subsequent modification by readers is not Transparent. An image format is not Transparent if used for any substantial amount of text. A copy that is not “Transparent” is called “Opaque”
Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standard-conforming simple HTML, PostScript or PDF designed for human modification. Examples of transparent image formats include PNG, XCF and JPG. Opaque formats include proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machine-generated HTML, PostScript or PDF produced by some word processors for output purposes only
“Trang tiêu đề” có nghĩa là, đối với một cuốn sách in, chính trang tiêu đề đó, cộng với các trang tiếp theo cần thiết để giữ một cách rõ ràng, tài liệu mà Giấy phép này yêu cầu xuất hiện trong trang tiêu đề. 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
Nếu bạn xuất bản các bản in [hoặc các bản sao trên phương tiện thường có bìa in] của Tài liệu, đánh số hơn 100 và thông báo cấp phép của Tài liệu yêu cầu phải có Văn bản bìa, thì bạn phải gửi kèm các bản sao trong bìa có tất cả những nội dung này một cách rõ ràng và dễ đọc. . 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
- SỬA ĐỔI
You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modified Version under precisely this License, with the Modified Version filling the role of the Document, thus licensing distribution and modification of the Modified Version to whoever possesses a copy of it. In addition, you must do these things in the Modified Version
- 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. If there is no section Entitled “History” in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modified Version as stated in the previous sentence
- 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”. Một phần như vậy có thể không được bao gồm trong Phiên bản sửa đổi
- Do not retitle any existing section to be Entitled “Endorsements” or to conflict in title with any Invariant Section
- Preserve any Warranty Disclaimers
If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document, you may at your option designate some or all of these sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version’s license notice. These titles must be distinct from any other section titles
You may add a section Entitled “Endorsements”, provided it contains nothing but endorsements of your Modified Version by various parties—for example, statements of peer review or that the text has been approved by an organization as the authoritative definition of a standard
You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover Text and one of Back-Cover Text may be added by [or through arrangements made by] any one entity. Nếu Tài liệu đã bao gồm một văn bản trang bìa cho cùng một trang bìa, do bạn thêm trước đó hoặc theo thỏa thuận của cùng một thực thể mà bạn đang đại diện, thì bạn không thể thêm văn bản khác;
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. How to use this License for your documents
To use this License in a document you have written, include a copy of the License in the document and put the following copyright and license notices just after the title page
sed -i 's/hello/world/' file.txt20
If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the “with…Texts. ” phù hợp với điều này
sed -i 's/hello/world/' file.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