Quyền được cấp để sao chép, phân phối và/hoặc sửa đổi tài liệu này theo các điều khoản của Giấy phép Tài liệu Tự do GNU, Phiên bản 1. 3 hoặc bất kỳ phiên bản nào mới hơn do Tổ chức Phần mềm Tự do xuất bản; . Một bản sao của giấy phép được bao gồm trong phần có tiêu đề “Giấy phép Tài liệu Miễn phí GNU” Show
1. Giới thiệused -i 's/hello/world/' file.txt22 là trình chỉnh sửa luồng. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as sed -i 's/hello/world/' file.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 sedThis 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 OverviewNormally 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 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt sed -i 's/hello/world/' file.txt22 writes output to standard output. Use -i to edit files in-place instead of printing to standard output. See also the sed -i 's/hello/world/' file.txt33 and sed -i 's/hello/world/' file.txt34 commands for writing output to other files. The following command modifies file. txt and does not produce any output sed -i 's/hello/world/' file.txt By default sed -i 's/hello/world/' file.txt22 prints all processed input (except input that has been modified/deleted by commands such as sed -i 's/hello/world/' file.txt36). Use -n to suppress output, and the sed -i 's/hello/world/' file.txt37 command to print specific lines. The following command prints only line 45 of the input file sed -i 's/hello/world/' file.txt22 treats multiple input files as one long stream. The following example prints the first line of the first file (one. txt) and the last line of the last file (three. txt). Use -s to reverse this behavior sed -n '1p ; $p' one.txt two.txt three.txt Without -e or -f options, sed -i 's/hello/world/' file.txt22 uses the first non-option parameter as the script, and the following non-option parameters as input files. If -e or -f options are used to specify a script, all non-option parameters are taken as input files. Options -e and -f can be combined, and can appear multiple times (in which case the final effective script will be concatenation of all the individual scripts) The following examples are equivalent sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt 2. 2 Command-Line OptionsThe full format for invoking sed -i 's/hello/world/' file.txt22 is sed OPTIONS.. [SCRIPT] [INPUTFILE...] 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 ). These options disable this automatic printing, and sed -i 's/hello/world/' file.txt22 only produces output when explicitly told to via the sed -i 's/hello/world/' file.txt37 command sed -i 's/hello/world/' file.txt52 Print the input sed program in canonical form, and annotate program execution $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 3 sed -i 's/hello/world/' file.txt53 sed -i 's/hello/world/' file.txt54 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 This option implies -s When the end of the file is reached, the temporary file is renamed to the output file’s original name. The extension, if supplied, is used to modify the name of the old file before renaming the temporary file, thereby making a backup copy) This rule is followed. if the extension doesn’t contain a sed -i 's/hello/world/' file.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 # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE sed -i 's/hello/world/' file.txt65 sed -i 's/hello/world/' file.txt66 Specify the default line-wrap length for the sed -i 's/hello/world/' file.txt67 command. A length of 0 (zero) means to never wrap long lines. If not specified, it is taken to be 70 sed -i 's/hello/world/' file.txt68 GNU sed -i 's/hello/world/' file.txt22 includes several extensions to POSIX sed. In order to simplify writing portable scripts, this option disables all the extensions that this manual documents, including additional commands. Most of the extensions accept sed -i 's/hello/world/' file.txt22 programs that are outside the syntax mandated by POSIX, but some of them (such as the behavior of the sed -i 's/hello/world/' file.txt71 command described in ) actually violate the standard. If you want to disable only the latter kind of extension, you can set the sed -i 's/hello/world/' file.txt72 variable to a non-empty value sed -i 's/hello/world/' file.txt73 sed -i 's/hello/world/' file.txt74 This option is available on every platform, but is only effective where the operating system makes a distinction between text files and binary files. When such a distinction is made—as is the case for MS-DOS, Windows, Cygwin—text files are composed of lines separated by a carriage return and a line feed character, and sed -i 's/hello/world/' file.txt22 does not see the ending CR. When this option is specified, sed -i 's/hello/world/' file.txt22 will open input files in binary mode, thus not requesting this special processing and considering lines to end at a line feed sed -i 's/hello/world/' file.txt77 This option is available only on platforms that support symbolic links and has an effect only if option -i is specified. In this case, if the file that is specified on the command line is a symbolic link, sed -i 's/hello/world/' file.txt22 will follow the link and edit the ultimate destination of the link. The default behavior is to break the symbolic link, so that the link destination will not be modified sed -i 's/hello/world/' file.txt79 sed -i 's/hello/world/' file.txt80 sed -i 's/hello/world/' file.txt81 Use extended regular expressions rather than basic regular expressions. Extended regexps are those that sed -i 's/hello/world/' file.txt82 accepts; they can be clearer because they usually have fewer backslashes. Historically this was a GNU extension, but the -E extension has since been added to the POSIX standard (http. //austingroupbugs. lượt xem trên mạng. php?id=528), so use -E for portability. GNU sed has accepted -E as an undocumented option for years, and *BSD seds have accepted -E for years as well, but scripts that use -E might not port to other older systems. Thấy sed -i 's/hello/world/' file.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. phạm vi địa chỉ (chẳng hạn như '/abc/,/def/') không được phép mở rộng trên nhiều tệp, số dòng liên quan đến phần đầu của mỗi tệp, sed -i 's/hello/world/' file.txt87 đề cập đến dòng cuối cùng của mỗi tệp và các tệp được gọi từ sed -i 's/hello/world/' file.txt88 sed -i 's/hello/world/' file.txt89 Trong chế độ hộp cát, sed -i 's/hello/world/' file.txt90 lệnh bị từ chối - các chương trình chứa chúng sẽ bị hủy bỏ mà không được chạy. Chế độ hộp cát đảm bảo sed -i 's/hello/world/' file.txt22 chỉ hoạt động trên các tệp đầu vào được chỉ định trên dòng lệnh và không thể chạy các chương trình bên ngoài sed -i 's/hello/world/' file.txt92______0_______93 Bộ đệm cả đầu vào và đầu ra ở mức tối thiểu nhất có thể. (Điều này đặc biệt hữu ích nếu đầu vào đến từ những thứ như 'tail -f' và bạn muốn xem đầu ra được chuyển đổi càng sớm càng tốt. ) sed -i 's/hello/world/' file.txt94_______0_______95 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ý. Tên tệp '-' đề cập đến luồng đầu vào tiêu chuẩn. Đầu vào tiêu chuẩn sẽ được xử lý nếu không có tên tệp nào được chỉ định 2. 3 Trạng thái thoátTrạ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 -n '1p ; $p' one.txt two.txt three.txt00 và sed -n '1p ; $p' one.txt two.txt three.txt01 có thể được sử dụng để chấm dứt sed -i 's/hello/world/' file.txt22 với giá trị mã thoát tùy chỉnh (đây là tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22) $ echo | sed 'Q42' ; echo $? 42 3 sed -i 's/hello/world/' file.txt 22 kịch bản3. 1 sed -i 's/hello/world/' file.txt 22 tổng quan kịch bảnMộ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 “chữ viết” sed -i 's/hello/world/' file.txt22; . Thấy sed -i 's/hello/world/' file.txt22 lệnh theo cú pháp này X là lệnh một ký tự sed -i 's/hello/world/' file.txt22. sed -n '1p ; $p' one.txt two.txt three.txt11 là một địa chỉ dòng tùy chọn. Nếu sed -n '1p ; $p' one.txt two.txt three.txt11 được chỉ định, lệnh X sẽ chỉ được thực hiện trên các dòng phù hợp. sed -n '1p ; $p' one.txt two.txt three.txt11 có thể là số dòng đơn, biểu thức chính quy hoặc dải dòng (xem phần ). sed -n '1p ; $p' one.txt two.txt three.txt14 bổ sung được sử dụng cho một số lệnh sed -i 's/hello/world/' file.txt22 Ví dụ sau xóa dòng 30 đến 35 trong đầu vào. sed -n '1p ; $p' one.txt two.txt three.txt16 là một dải địa chỉ. sed -i 's/hello/world/' file.txt36 là lệnh xóa sed '30,35d' input.txt > output.txt Ví dụ sau in tất cả đầu vào cho đến khi tìm thấy một dòng bắt đầu bằng từ 'foo'. Nếu dòng đó được tìm thấy, sed -i 's/hello/world/' file.txt22 sẽ chấm dứt với trạng thái thoát 42. Nếu dòng đó không được tìm thấy (và không có lỗi nào khác xảy ra), sed -i 's/hello/world/' file.txt22 sẽ thoát với trạng thái 0. sed -n '1p ; $p' one.txt two.txt three.txt20 là một địa chỉ biểu thức chính quy. sed -n '1p ; $p' one.txt two.txt three.txt00 là lệnh thoát. sed -n '1p ; $p' one.txt two.txt three.txt22 là tùy chọn lệnh sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt0 Các lệnh trong tập lệnh hoặc tệp tập lệnh có thể được phân tách bằng dấu chấm phẩy ( sed -n '1p ; $p' one.txt two.txt three.txt23) hoặc dòng mới (ASCII 10). Nhiều tập lệnh có thể được chỉ định với các tùy chọn -e hoặc -f Các ví dụ sau đây đều tương đương. Họ thực hiện hai hoạt động sed -i 's/hello/world/' file.txt22. xóa bất kỳ dòng nào khớp với biểu thức chính quy sed -n '1p ; $p' one.txt two.txt three.txt20 và thay thế tất cả các lần xuất hiện của chuỗi 'hello' bằng 'world' sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt1 Lệnh sed -n '1p ; $p' one.txt two.txt three.txt26, sed -n '1p ; $p' one.txt two.txt three.txt27, sed -n '1p ; $p' one.txt two.txt three.txt28, do cú pháp của chúng, không thể theo sau dấu chấm phẩy hoạt động như dấu phân cách lệnh và do đó nên được kết thúc bằng dòng mới hoặc được đặt ở cuối tập lệnh hoặc tệp tập lệnh. Các lệnh cũng có thể được đặt trước các ký tự khoảng trắng không quan trọng tùy chọn. Thấy 3. 2 sed -i 's/hello/world/' file.txt 22 lệnh tóm tắtCác lệnh sau được hỗ trợ trong GNU sed -i 's/hello/world/' file.txt22. Một số là các lệnh POSIX tiêu chuẩn, trong khi một số khác là các phần mở rộng của GNU. Chi tiết và ví dụ cho từng lệnh có trong các phần sau. (Mnemonics) được hiển thị trong ngoặc đơn sed -n '1p ; $p' one.txt two.txt three.txt31 sed -n '1p ; $p' one.txt two.txt three.txt32 Nối văn bản sau một dòng sed -n '1p ; $p' one.txt two.txt three.txt33 Nối văn bản sau một dòng (cú pháp thay thế) sed -n '1p ; $p' one.txt two.txt three.txt34 Chi nhánh vô điều kiện để dán nhãn. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu sed -n '1p ; $p' one.txt two.txt three.txt35 sed -n '1p ; $p' one.txt two.txt three.txt32 Thay thế (thay đổi) dòng bằng văn bản sed -n '1p ; $p' one.txt two.txt three.txt37 Thay thế (thay đổi) dòng bằng văn bản (cú pháp thay thế) sed -i 's/hello/world/' file.txt36 Xóa không gian mẫu; sed -n '1p ; $p' one.txt two.txt three.txt39 Nếu không gian mẫu chứa các dòng mới, hãy xóa văn bản trong không gian mẫu cho đến dòng mới đầu tiên và bắt đầu lại chu kỳ với không gian mẫu kết quả mà không đọc dòng đầu vào mới Nếu không gian mẫu không chứa dòng mới, hãy bắt đầu một chu kỳ mới bình thường như thể lệnh sed -i 's/hello/world/' file.txt36 đã được ban hành sed -n '1p ; $p' one.txt two.txt three.txt41 Thực thi lệnh được tìm thấy trong không gian mẫu và thay thế không gian mẫu bằng đầu ra; sed -n '1p ; $p' one.txt two.txt three.txt42 Thực thi lệnh và gửi đầu ra của nó tới luồng đầu ra. Lệnh có thể chạy trên nhiều dòng, trừ dòng cuối cùng có dấu gạch chéo ngược sed -n '1p ; $p' one.txt two.txt three.txt43 (tên tệp) In tên tệp của tệp đầu vào hiện tại (với một dòng mới ở cuối) sed -n '1p ; $p' one.txt two.txt three.txt44 Thay thế nội dung của không gian mẫu bằng nội dung của không gian giữ sed -n '1p ; $p' one.txt two.txt three.txt45 Nối một dòng mới vào nội dung của không gian mẫu, rồi nối thêm nội dung của không gian giữ vào nội dung của không gian mẫu sed -n '1p ; $p' one.txt two.txt three.txt46 (giữ) Thay thế nội dung của không gian giữ bằng nội dung của không gian mẫu sed -n '1p ; $p' one.txt two.txt three.txt47 Nối một dòng mới vào nội dung của không gian giữ, sau đó nối thêm nội dung của không gian mẫu vào nội dung của không gian giữ sed -n '1p ; $p' one.txt two.txt three.txt48 sed -n '1p ; $p' one.txt two.txt three.txt32 chèn văn bản trước một dòng sed -n '1p ; $p' one.txt two.txt three.txt50 chèn văn bản trước một dòng (cú pháp thay thế) sed -i 's/hello/world/' file.txt67 In không gian mẫu ở dạng rõ ràng sed -n '1p ; $p' one.txt two.txt three.txt52 (tiếp theo) Nếu tính năng tự động in không bị tắt, hãy in không gian mẫu, sau đó, bất kể, thay thế không gian mẫu bằng dòng nhập tiếp theo. Nếu không có thêm thông tin đầu vào nào thì sed -i 's/hello/world/' file.txt22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào sed -i 's/hello/world/' file.txt71 Thêm một dòng mới vào không gian mẫu, sau đó nối dòng đầu vào tiếp theo vào không gian mẫu. Nếu không có thêm thông tin đầu vào nào thì sed -i 's/hello/world/' file.txt22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào sed -i 's/hello/world/' file.txt37 In không gian mẫu sed -n '1p ; $p' one.txt two.txt three.txt57 In không gian mẫu, cho đến mẫu đầu tiên sed -n '1p ; $p' one.txt two.txt three.txt58 (thoát) Thoát sed -i 's/hello/world/' file.txt22 mà không xử lý thêm bất kỳ lệnh hoặc đầu vào nào sed -n '1p ; $p' one.txt two.txt three.txt60 (thoát) Lệnh này giống như lệnh sed -n '1p ; $p' one.txt two.txt three.txt00, nhưng sẽ không in nội dung của không gian mẫu. Giống như sed -n '1p ; $p' one.txt two.txt three.txt00, nó cung cấp khả năng trả lại mã thoát cho người gọi sed -n '1p ; $p' one.txt two.txt three.txt63 Đọc tên tập tin sed -n '1p ; $p' one.txt two.txt three.txt64 Xếp hàng một dòng tên tệp sẽ được đọc và chèn vào luồng đầu ra ở cuối chu kỳ hiện tại hoặc khi dòng đầu vào tiếp theo được đọc sed -n '1p ; $p' one.txt two.txt three.txt65 (thay thế) Khớp biểu thức chính quy với nội dung của không gian mẫu. Nếu tìm thấy, hãy thay thế chuỗi phù hợp bằng chuỗi thay thế sed -n '1p ; $p' one.txt two.txt three.txt66 (thử nghiệm) Nhánh để gắn nhãn chỉ khi đã có một sự thay thế thành công sed -n '1p ; $p' one.txt two.txt three.txt67 kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện đã được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu sed -n '1p ; $p' one.txt two.txt three.txt68 (thử nghiệm) Chỉ rẽ nhánh để gắn nhãn nếu không có thay thế sed -n '1p ; $p' one.txt two.txt three.txt67 nào thành công kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu sed -n '1p ; $p' one.txt two.txt three.txt70 (phiên bản) Lệnh này không làm gì, nhưng làm cho sed -i 's/hello/world/' file.txt22 không thành công nếu tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22 không được hỗ trợ hoặc nếu phiên bản được yêu cầu không có sẵn sed -n '1p ; $p' one.txt two.txt three.txt73 Viết không gian mẫu vào tên tệp sed -n '1p ; $p' one.txt two.txt three.txt74 Ghi vào tên tệp đã cho phần không gian mẫu cho đến dòng mới đầu tiên sed -n '1p ; $p' one.txt two.txt three.txt75 Trao đổi nội dung của không gian giữ và mẫu sed -n '1p ; $p' one.txt two.txt three.txt76 Chuyển ngữ bất kỳ ký tự nào trong không gian mẫu khớp với bất kỳ ký tự nguồn nào với ký tự tương ứng trong ký tự đích sed -n '1p ; $p' one.txt two.txt three.txt77 (zap) Lệnh này làm trống nội dung của không gian mẫu sed -n '1p ; $p' one.txt two.txt three.txt78 Một nhận xét, cho đến dòng mới tiếp theo sed -n '1p ; $p' one.txt two.txt three.txt79 Nhóm một số lệnh lại với nhau sed -n '1p ; $p' one.txt two.txt three.txt80 In số dòng đầu vào hiện tại (với một dòng mới ở cuối) sed -n '1p ; $p' one.txt two.txt three.txt81 Chỉ định vị trí của nhãn cho các lệnh rẽ nhánh (_______19_______82, sed -n '1p ; $p' one.txt two.txt three.txt83, sed -n '1p ; $p' one.txt two.txt three.txt84) 3. 3 Lệnh sed -n '1p ; $p' one.txt two.txt three.txt 67Lệnh sed -n '1p ; $p' one.txt two.txt three.txt67 (để thay thế) có lẽ là lệnh quan trọng nhất trong sed -i 's/hello/world/' file.txt22 và có rất nhiều tùy chọn khác nhau. Cú pháp của lệnh sed -n '1p ; $p' one.txt two.txt three.txt67 là 's/regexp/replacement/flags' Khái niệm cơ bản của nó là đơn giản. lệnh sed -n '1p ; $p' one.txt two.txt three.txt67 cố gắng khớp không gian mẫu với biểu thức chính quy được cung cấp; Để biết chi tiết về cú pháp regrec xem Phần thay thế có thể chứa các tham chiếu sed -n '1p ; $p' one.txt two.txt three.txt90 (n là một số từ 1 đến 9), tham chiếu tới phần trùng khớp được chứa giữa sed -n '1p ; $p' one.txt two.txt three.txt91 thứ n và kết quả trùng khớp của nó sed -n '1p ; $p' one.txt two.txt three.txt92. Ngoài ra, phần thay thế có thể chứa _______19_______93 ký tự chưa thoát tham chiếu toàn bộ phần khớp của không gian mẫu Các ký tự sed -n '1p ; $p' one.txt two.txt three.txt94 có thể được thay thế thống nhất bằng bất kỳ ký tự đơn nào khác trong bất kỳ lệnh sed -n '1p ; $p' one.txt two.txt three.txt67 nào. Ký tự sed -n '1p ; $p' one.txt two.txt three.txt94 (hoặc bất kỳ ký tự nào khác được sử dụng thay cho nó) chỉ có thể xuất hiện trong biểu thức chính quy hoặc thay thế nếu nó đứng trước ký tự sed -n '1p ; $p' one.txt two.txt three.txt97 Cuối cùng, với tư cách là tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22, bạn có thể bao gồm một chuỗi đặc biệt được tạo thành từ dấu gạch chéo ngược và một trong các chữ cái sed -n '1p ; $p' one.txt two.txt three.txt99, sed -i 's/hello/world/' file.txt67, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt01, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt02 hoặc sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt03. Ý nghĩa như sau sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt04 Chuyển từ thay thế sang chữ thường cho đến khi tìm thấy sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt05 hoặc sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt06, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt07 Biến ký tự tiếp theo thành chữ thường, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt05 Chuyển từ thay thế sang chữ hoa cho đến khi tìm thấy sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt04 hoặc sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt06, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt11 Chuyển ký tự tiếp theo thành chữ hoa, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt06 Dừng chuyển đổi trường hợp bắt đầu bởi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt04 hoặc sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt05 Khi cờ sed -n '1p ; $p' one.txt two.txt three.txt44 đang được sử dụng, chuyển đổi kiểu chữ không lan truyền từ lần xuất hiện này sang lần xuất hiện khác của biểu thức chính quy. Ví dụ: khi lệnh sau được thực thi với 'a-b-' trong không gian mẫu đầu ra là 'axxB'. Khi thay thế '-' đầu tiên, chuỗi '\ u' chỉ ảnh hưởng đến thay thế trống của '\ 1'. Nó không ảnh hưởng đến ký tự sed -n '1p ; $p' one.txt two.txt three.txt75 được thêm vào không gian mẫu khi thay thế sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt17 bằng sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt18 Mặt khác, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt07 và sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt11 ảnh hưởng đến phần còn lại của văn bản thay thế nếu theo sau chúng là một từ thay thế trống. Với 'a-b-' trong không gian mẫu, lệnh sau sẽ thay thế '-' bằng 'X' (chữ hoa) và 'b-' bằng 'Bx'. Nếu hành vi này là không mong muốn, bạn có thể ngăn chặn nó bằng cách thêm một chuỗi '\E'—sau '\1' trong trường hợp này Để bao gồm một ký tự sed -n '1p ; $p' one.txt two.txt three.txt97, sed -n '1p ; $p' one.txt two.txt three.txt93 hoặc dòng mới trong thay thế cuối cùng, hãy đảm bảo đặt trước sed -n '1p ; $p' one.txt two.txt three.txt97 mong muốn, sed -n '1p ; $p' one.txt two.txt three.txt93 hoặc dòng mới trong thay thế bằng một ____19_______97 Lệnh sed -n '1p ; $p' one.txt two.txt three.txt67 có thể được theo sau bởi 0 hoặc nhiều cờ sau sed -n '1p ; $p' one.txt two.txt three.txt44 Áp dụng thay thế cho tất cả các kết quả phù hợp với biểu thức chính quy, không chỉ lần đầu tiên sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt28 Chỉ thay thế trận đấu số của regrec tương tác trong lệnh sed -n '1p ; $p' one.txt two.txt three.txt67 Lưu ý. tiêu chuẩn POSIX không chỉ định điều gì sẽ xảy ra khi bạn kết hợp công cụ sửa đổi số và số sed -n '1p ; $p' one.txt two.txt three.txt44 và hiện tại không có ý nghĩa thống nhất rộng rãi trong việc triển khai sed -i 's/hello/world/' file.txt22. Đối với GNU sed -i 's/hello/world/' file.txt22, tương tác được định nghĩa là. bỏ qua các kết quả khớp trước số, sau đó khớp và thay thế tất cả các kết quả khớp từ số trở đi sed -i 's/hello/world/' file.txt37 Nếu thay thế đã được thực hiện, sau đó in không gian mẫu mới Ghi chú. khi cả hai tùy chọn sed -i 's/hello/world/' file.txt37 và sed -n '1p ; $p' one.txt two.txt three.txt41 được chỉ định, thứ tự tương đối của hai tùy chọn tạo ra kết quả rất khác nhau. Nói chung, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt36 (đánh giá rồi in) là điều bạn muốn, nhưng thao tác ngược lại có thể hữu ích cho việc gỡ lỗi. Vì lý do này, phiên bản hiện tại của GNU sed -i 's/hello/world/' file.txt22 diễn giải một cách đặc biệt sự hiện diện của các tùy chọn sed -i 's/hello/world/' file.txt37 cả trước và sau sed -n '1p ; $p' one.txt two.txt three.txt41, in không gian mẫu trước và sau khi đánh giá, trong khi nói chung các cờ cho lệnh sed -n '1p ; $p' one.txt two.txt three.txt67 chỉ hiển thị tác dụng của chúng một lần. Hành vi này, mặc dù được ghi lại, có thể thay đổi trong các phiên bản sau sed -n '1p ; $p' one.txt two.txt three.txt73 Nếu thay thế được thực hiện, sau đó ghi kết quả vào tệp được đặt tên. Là một tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22, hai giá trị đặc biệt của tên tệp được hỗ trợ. /dev/stderr, ghi kết quả vào lỗi tiêu chuẩn và /dev/stdout, ghi vào đầu ra tiêu chuẩn sed -n '1p ; $p' one.txt two.txt three.txt41 Lệnh này cho phép một người chuyển đầu vào từ lệnh shell sang không gian mẫu. Nếu thay thế được thực hiện, lệnh được tìm thấy trong không gian mẫu được thực thi và không gian mẫu được thay thế bằng đầu ra của nó. Một dòng mới ở cuối bị chặn; . Đây là tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22 sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt45_______19_______28 Công cụ sửa đổi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt45 cho đối sánh biểu thức chính quy là một tiện ích mở rộng GNU làm cho _________22 đối sánh biểu thức chính quy theo cách không phân biệt chữ hoa chữ thường sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt49 sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt50 Công cụ sửa đổi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt49 để khớp biểu thức chính quy là một tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22 hướng GNU sed -i 's/hello/world/' file.txt22 khớp biểu thức chính quy ở chế độ nhiều dòng. Công cụ sửa đổi làm cho sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 và sed -i 's/hello/world/' file.txt87 khớp tương ứng (ngoài hành vi bình thường) chuỗi trống sau một dòng mới và chuỗi trống trước một dòng mới. Có các chuỗi ký tự đặc biệt ( sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt56 và sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt57) luôn khớp với phần đầu hoặc phần cuối của bộ đệm. Ngoài ra, ký tự dấu chấm không khớp với ký tự dòng mới ở chế độ nhiều dòng 3. 4 lệnh thường dùngNếu bạn hoàn toàn sử dụng sed -i 's/hello/world/' file.txt22, rất có thể bạn sẽ muốn biết các lệnh này sed -n '1p ; $p' one.txt two.txt three.txt78 [Không cho phép địa chỉ. ] Ký tự sed -n '1p ; $p' one.txt two.txt three.txt78 bắt đầu nhận xét; Nếu bạn lo lắng về tính di động, hãy lưu ý rằng một số triển khai của sed -i 's/hello/world/' file.txt22 (không tuân thủ POSIX) chỉ có thể hỗ trợ một nhận xét một dòng và sau đó chỉ khi ký tự đầu tiên của tập lệnh là một sed -n '1p ; $p' one.txt two.txt three.txt78 Cảnh báo. nếu hai ký tự đầu tiên của tập lệnh sed -i 's/hello/world/' file.txt22 là sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt64, thì tùy chọn -n (no-autoprint) là bắt buộc. Nếu bạn muốn đặt nhận xét trong dòng đầu tiên của tập lệnh và nhận xét đó bắt đầu bằng chữ 'n' và bạn không muốn hành vi này, thì hãy đảm bảo sử dụng chữ 'N' viết hoa hoặc đặt ít nhất một khoảng trắng sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt65 Thoát sed -i 's/hello/world/' file.txt22 mà không xử lý thêm bất kỳ lệnh hoặc đầu vào nào Thí dụ. dừng lại sau khi in dòng thứ hai Lệnh này chỉ chấp nhận một địa chỉ. Lưu ý rằng không gian mẫu hiện tại được in nếu tính năng in tự động không bị tắt với tùy chọn -n. Khả năng trả lại mã thoát từ tập lệnh sed -i 's/hello/world/' file.txt22 là một tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22 Xem thêm lệnh GNU sed -i 's/hello/world/' file.txt22 tiện ích mở rộng sed -n '1p ; $p' one.txt two.txt three.txt01 thoát âm thầm mà không in không gian mẫu hiện tại sed -i 's/hello/world/' file.txt36 Xóa không gian mẫu; Thí dụ. xóa dòng đầu vào thứ hai sed -i 's/hello/world/' file.txt37 In ra không gian mẫu (đến đầu ra tiêu chuẩn). Lệnh này thường chỉ được sử dụng cùng với tùy chọn dòng lệnh -n Thí dụ. chỉ in dòng đầu vào thứ hai sed -n '1p ; $p' one.txt two.txt three.txt52 Nếu tính năng tự động in không bị tắt, hãy in không gian mẫu, sau đó, bất kể, thay thế không gian mẫu bằng dòng nhập tiếp theo. Nếu không có thêm thông tin đầu vào nào thì sed -i 's/hello/world/' file.txt22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào Lệnh này rất hữu ích để bỏ qua các dòng (e. g. xử lý mọi dòng thứ N) Thí dụ. thực hiện thay thế trên mỗi dòng thứ 3 (i. e. hai lệnh sed -n '1p ; $p' one.txt two.txt three.txt52 bỏ qua hai dòng) sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt2 GNU sed -i 's/hello/world/' file.txt22 cung cấp cú pháp địa chỉ mở rộng của bước đầu tiên để đạt được kết quả tương tự sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt3 sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt77 Một nhóm lệnh có thể được đặt trong khoảng từ sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt78 đến sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt79 ký tự. Điều này đặc biệt hữu ích khi bạn muốn một nhóm lệnh được kích hoạt bởi một địa chỉ (hoặc dải địa chỉ) phù hợp Thí dụ. thực hiện thay thế sau đó in dòng đầu vào thứ hai sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt4 3. 5 lệnh ít sử dụng hơnMặc dù có lẽ ít được sử dụng hơn so với các lệnh trong phần trước, một số tập lệnh sed -i 's/hello/world/' file.txt22 rất nhỏ nhưng hữu ích có thể được tạo bằng các lệnh này sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt81 Chuyển ngữ bất kỳ ký tự nào trong không gian mẫu khớp với bất kỳ ký tự nguồn nào với ký tự tương ứng trong ký tự đích Thí dụ. phiên âm 'a-j' thành '0-9' sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt5 (Các ký tự sed -n '1p ; $p' one.txt two.txt three.txt94 có thể được thay thế thống nhất bằng bất kỳ ký tự đơn nào khác trong bất kỳ lệnh sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt83 nào. ) Các trường hợp của sed -n '1p ; $p' one.txt two.txt three.txt94 (hoặc bất kỳ ký tự nào khác được sử dụng thay cho nó), sed -n '1p ; $p' one.txt two.txt three.txt97 hoặc các dòng mới có thể xuất hiện trong danh sách ký tự nguồn hoặc ký tự đích, với điều kiện là mỗi trường hợp được thoát bởi một ____19_______97. Danh sách ký tự nguồn và ký tự đích phải chứa cùng số lượng ký tự (sau khi thoát) Xem lệnh sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt87 từ GNU coreutils để biết chức năng tương tự sed -n '1p ; $p' one.txt two.txt three.txt33 Nối văn bản sau một dòng. Đây là phần mở rộng GNU cho lệnh sed -n '1p ; $p' one.txt two.txt three.txt26 tiêu chuẩn - xem bên dưới để biết chi tiết Thí dụ. Thêm từ 'xin chào' sau dòng thứ hai sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt6 Khoảng trắng phía trước sau lệnh sed -n '1p ; $p' one.txt two.txt three.txt26 bị bỏ qua. Văn bản cần thêm được đọc cho đến cuối dòng sed -n '1p ; $p' one.txt two.txt three.txt31 sed -n '1p ; $p' one.txt two.txt three.txt32 Nối văn bản sau một dòng Thí dụ. Thêm 'xin chào' sau dòng thứ hai (-. cho biết các dòng đầu ra được in) sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt7 Lệnh sed -n '1p ; $p' one.txt two.txt three.txt26 xếp hàng các dòng văn bản tuân theo lệnh này (mỗi dòng trừ kết thúc cuối cùng bằng ____19_______97, được xóa khỏi đầu ra) để xuất ra ở cuối chu kỳ hiện tại hoặc khi dòng đầu vào tiếp theo được đọc Là một phần mở rộng GNU, lệnh này chấp nhận hai địa chỉ Trình tự thoát trong văn bản được xử lý, vì vậy bạn nên sử dụng sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt95 trong văn bản để in một dấu gạch chéo ngược Các lệnh tiếp tục sau dòng cuối cùng mà không có dấu gạch chéo ngược ( sed -n '1p ; $p' one.txt two.txt three.txt97) - ‘world’ trong ví dụ sau sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt8 Là một phần mở rộng của GNU, lệnh và văn bản sed -n '1p ; $p' one.txt two.txt three.txt26 có thể được tách thành hai tham số sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt98, cho phép tạo tập lệnh dễ dàng hơn sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt9 sed -n '1p ; $p' one.txt two.txt three.txt50 chèn văn bản trước một dòng. Đây là phần mở rộng GNU cho lệnh sed -n '1p ; $p' one.txt two.txt three.txt28 tiêu chuẩn - xem bên dưới để biết chi tiết Thí dụ. Chèn từ 'xin chào' trước dòng thứ hai sed -i 's/hello/world/' file.txt0 Khoảng trắng phía trước sau lệnh sed -n '1p ; $p' one.txt two.txt three.txt28 bị bỏ qua. Văn bản cần thêm được đọc cho đến cuối dòng sed -n '1p ; $p' one.txt two.txt three.txt48 sed -n '1p ; $p' one.txt two.txt three.txt32 Xuất ngay các dòng văn bản theo lệnh này Thí dụ. Chèn 'xin chào' trước dòng thứ hai (-. cho biết các dòng đầu ra được in) sed -i 's/hello/world/' file.txt1 Là một phần mở rộng GNU, lệnh này chấp nhận hai địa chỉ Trình tự thoát trong văn bản được xử lý, vì vậy bạn nên sử dụng sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt95 trong văn bản để in một dấu gạch chéo ngược Các lệnh tiếp tục sau dòng cuối cùng mà không có dấu gạch chéo ngược ( sed -n '1p ; $p' one.txt two.txt three.txt97) - ‘world’ trong ví dụ sau sed -i 's/hello/world/' file.txt2 Là một phần mở rộng của GNU, lệnh và văn bản sed -n '1p ; $p' one.txt two.txt three.txt28 có thể được tách thành hai tham số sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt98, cho phép tạo tập lệnh dễ dàng hơn sed -i 's/hello/world/' file.txt3 sed -n '1p ; $p' one.txt two.txt three.txt37 Thay thế (các) dòng bằng văn bản. Đây là phần mở rộng GNU cho lệnh sed -n '1p ; $p' one.txt two.txt three.txt27 tiêu chuẩn - xem bên dưới để biết chi tiết Thí dụ. Thay thế các dòng thứ 2 đến thứ 9 bằng từ 'xin chào' sed -i 's/hello/world/' file.txt4 Khoảng trắng phía trước sau lệnh sed -n '1p ; $p' one.txt two.txt three.txt27 bị bỏ qua. Văn bản cần thêm được đọc cho đến cuối dòng sed -n '1p ; $p' one.txt two.txt three.txt35 sed -n '1p ; $p' one.txt two.txt three.txt32 Xóa các dòng khớp với địa chỉ hoặc dải địa chỉ và xuất các dòng văn bản tuân theo lệnh này Thí dụ. Thay thế dòng thứ 2 đến thứ 4 bằng các từ 'xin chào' và 'thế giới' (-. cho biết các dòng đầu ra được in) sed -i 's/hello/world/' file.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. Trong ví dụ sau, sed -n '1p ; $p' one.txt two.txt three.txt27 bắt đầu một chu kỳ mới và lệnh thay thế không được thực hiện trên văn bản đã thay thế sed -i 's/hello/world/' file.txt6 Là một phần mở rộng của GNU, lệnh và văn bản sed -n '1p ; $p' one.txt two.txt three.txt27 có thể được tách thành hai tham số sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt98, cho phép tạo tập lệnh dễ dàng hơn sed -i 's/hello/world/' file.txt7 sed -n '1p ; $p' one.txt two.txt three.txt80 In ra số dòng đầu vào hiện tại (với một dòng mới ở cuối) sed -i 's/hello/world/' file.txt8 Là một phần mở rộng GNU, lệnh này chấp nhận hai địa chỉ sed OPTIONS.. [SCRIPT] [INPUTFILE...]17 In không gian mẫu ở dạng rõ ràng. các ký tự không in được (và ký tự sed -n '1p ; $p' one.txt two.txt three.txt97) được in ở dạng thoát kiểu C; n chỉ định độ dài ngắt dòng mong muốn; . Nếu bỏ qua, giá trị mặc định như được chỉ định trên dòng lệnh sẽ được sử dụng. Tham số n là phần mở rộng GNU sed -i 's/hello/world/' file.txt22 sed -n '1p ; $p' one.txt two.txt three.txt63 Đọc tên tập tin. Thí dụ sed -i 's/hello/world/' file.txt9 Xếp hàng nội dung của tên tệp sẽ được đọc và chèn vào luồng đầu ra ở cuối chu kỳ hiện tại hoặc khi dòng đầu vào tiếp theo được đọc. Lưu ý rằng nếu tên tệp không thể đọc được, nó sẽ được coi như một tệp trống, không có bất kỳ dấu hiệu lỗi nào Là một tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22, giá trị đặc biệt /dev/stdin được hỗ trợ cho tên tệp, đọc nội dung của đầu vào tiêu chuẩn Là một phần mở rộng GNU, lệnh này chấp nhận hai địa chỉ. Sau đó, tệp sẽ được đọc lại và chèn vào từng dòng được giải quyết sed -n '1p ; $p' one.txt two.txt three.txt73 Viết không gian mẫu vào tên tệp. Là một tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22, hai giá trị đặc biệt của tên tệp được hỗ trợ. /dev/stderr, ghi kết quả vào lỗi tiêu chuẩn và /dev/stdout, ghi vào đầu ra tiêu chuẩn Tệp sẽ được tạo (hoặc cắt bớt) trước khi dòng đầu vào đầu tiên được đọc; sed -n '1p ; $p' one.txt two.txt three.txt39 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. Nếu không, hãy xóa văn bản trong không gian mẫu cho đến dòng mới đầu tiên và khởi động lại chu trình với không gian mẫu kết quả mà không cần đọc dòng đầu vào mới sed -i 's/hello/world/' file.txt71 Thêm một dòng mới vào không gian mẫu, sau đó nối dòng đầu vào tiếp theo vào không gian mẫu. Nếu không có thêm thông tin đầu vào nào thì sed -i 's/hello/world/' file.txt22 sẽ thoát mà không xử lý thêm bất kỳ lệnh nào Khi -z được sử dụng, một byte bằng 0 (ký tự ascii ‘NUL’) được thêm vào giữa các dòng (thay vì một dòng mới) Theo mặc định, sed -i 's/hello/world/' file.txt22 không kết thúc nếu không có dòng đầu vào 'tiếp theo'. Đây là tiện ích mở rộng GNU có thể bị tắt bằng --posix. Thấy sed -n '1p ; $p' one.txt two.txt three.txt57 In ra phần không gian mẫu cho đến dòng mới đầu tiên sed -n '1p ; $p' one.txt two.txt three.txt46 Thay thế nội dung của không gian giữ bằng nội dung của không gian mẫu sed -n '1p ; $p' one.txt two.txt three.txt47 Nối một dòng mới vào nội dung của không gian giữ, sau đó nối thêm nội dung của không gian mẫu vào nội dung của không gian giữ sed -n '1p ; $p' one.txt two.txt three.txt44 Thay thế nội dung của không gian mẫu bằng nội dung của không gian giữ sed -n '1p ; $p' one.txt two.txt three.txt45 Nối một dòng mới vào nội dung của không gian mẫu, rồi nối thêm nội dung của không gian giữ vào nội dung của không gian mẫu sed -n '1p ; $p' one.txt two.txt three.txt75 Trao đổi nội dung của không gian giữ và mẫu 3. 6 mệnh lệnh cho sed -i 's/hello/world/' file.txt 22 bậc thầyTrong hầu hết các trường hợp, việc sử dụng các lệnh này cho thấy rằng bạn có thể lập trình tốt hơn trong những thứ như sed OPTIONS.. [SCRIPT] [INPUTFILE...]41 hoặc Perl. Nhưng đôi khi một người cam kết gắn bó với sed -i 's/hello/world/' file.txt22 và các lệnh này có thể cho phép một người viết các tập lệnh khá phức tạp sed -n '1p ; $p' one.txt two.txt three.txt81 [Không cho phép địa chỉ. ] Chỉ định vị trí của nhãn cho các lệnh rẽ nhánh. Trong tất cả các khía cạnh khác, một no-op sed -n '1p ; $p' one.txt two.txt three.txt34 Chi nhánh vô điều kiện để dán nhãn. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu sed -n '1p ; $p' one.txt two.txt three.txt66 Chỉ nhánh để gắn nhãn nếu đã có một sự thay thế sed -n '1p ; $p' one.txt two.txt three.txt67 thành công kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện đã được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu 3. 7 Lệnh dành riêng cho GNU sed -i 's/hello/world/' file.txt 22Các lệnh này dành riêng cho GNU sed -i 's/hello/world/' file.txt22, vì vậy bạn phải cẩn thận khi sử dụng chúng và chỉ khi bạn chắc chắn rằng việc cản trở tính di động không phải là điều xấu. Chúng cho phép bạn kiểm tra các tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22 hoặc thực hiện các tác vụ được yêu cầu khá thường xuyên, nhưng không được hỗ trợ bởi các sed -i 's/hello/world/' file.txt22 tiêu chuẩn sed OPTIONS.. [SCRIPT] [INPUTFILE...]51 Lệnh này cho phép một người chuyển đầu vào từ lệnh shell sang không gian mẫu. Không có tham số, lệnh sed -n '1p ; $p' one.txt two.txt three.txt41 thực thi lệnh được tìm thấy trong không gian mẫu và thay thế không gian mẫu bằng đầu ra; Thay vào đó, nếu một tham số được chỉ định, lệnh sed -n '1p ; $p' one.txt two.txt three.txt41 diễn giải nó thành một lệnh và gửi đầu ra của nó đến luồng đầu ra. Lệnh có thể chạy trên nhiều dòng, trừ dòng cuối cùng có dấu gạch chéo ngược Trong cả hai trường hợp, kết quả không được xác định nếu lệnh được thực thi chứa ký tự NUL Lưu ý rằng, không giống như lệnh sed OPTIONS.. [SCRIPT] [INPUTFILE...]54, đầu ra của lệnh sẽ được in ngay lập tức; sed -n '1p ; $p' one.txt two.txt three.txt43 In ra tên tệp của tệp đầu vào hiện tại (với một dòng mới ở cuối) sed OPTIONS.. [SCRIPT] [INPUTFILE...]57 Lệnh này chỉ chấp nhận một địa chỉ Lệnh này giống như lệnh sed -n '1p ; $p' one.txt two.txt three.txt00, nhưng sẽ không in nội dung của không gian mẫu. Giống như sed -n '1p ; $p' one.txt two.txt three.txt00, nó cung cấp khả năng trả lại mã thoát cho người gọi Lệnh này có thể hữu ích vì các cách thay thế duy nhất để thực hiện chức năng có vẻ tầm thường này là sử dụng tùy chọn -n (có thể làm phức tạp tập lệnh của bạn một cách không cần thiết) hoặc sử dụng đoạn mã sau, điều này làm lãng phí thời gian bằng cách đọc toàn bộ tệp mà không có bất kỳ hiệu ứng rõ ràng nào sed -n '1p ; $p' one.txt two.txt three.txt0 sed -n '1p ; $p' one.txt two.txt three.txt64 Xếp hàng một dòng tên tệp sẽ được đọc và chèn vào luồng đầu ra ở cuối chu kỳ hiện tại hoặc khi dòng đầu vào tiếp theo được đọc. Lưu ý rằng nếu tên tệp không thể đọc được hoặc nếu đã đến cuối thì không có dòng nào được thêm vào mà không có bất kỳ dấu hiệu lỗi nào Như với lệnh sed OPTIONS.. [SCRIPT] [INPUTFILE...]54, giá trị đặc biệt /dev/stdin được hỗ trợ cho tên tệp, đọc một dòng từ đầu vào tiêu chuẩn sed -n '1p ; $p' one.txt two.txt three.txt68 Chỉ phân nhánh để gắn nhãn nếu không có thay thế sed -n '1p ; $p' one.txt two.txt three.txt67 thành công nào kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu sed OPTIONS.. [SCRIPT] [INPUTFILE...]64 Lệnh này không làm gì, nhưng làm cho sed -i 's/hello/world/' file.txt22 không thành công nếu các tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22 không được hỗ trợ, đơn giản vì các phiên bản khác của sed -i 's/hello/world/' file.txt22 không triển khai lệnh này. Ngoài ra, bạn có thể chỉ định phiên bản của sed -i 's/hello/world/' file.txt22 mà tập lệnh của bạn yêu cầu, chẳng hạn như sed OPTIONS.. [SCRIPT] [INPUTFILE...]69. Giá trị mặc định là sed OPTIONS.. [SCRIPT] [INPUTFILE...]70 vì đó là phiên bản đầu tiên triển khai lệnh này Lệnh này cho phép tất cả các tiện ích mở rộng GNU ngay cả khi sed -i 's/hello/world/' file.txt72 được đặt trong môi trường sed -n '1p ; $p' one.txt two.txt three.txt74 Ghi vào tên tệp đã cho phần không gian mẫu cho đến dòng mới đầu tiên. Mọi thứ được nói theo lệnh sed OPTIONS.. [SCRIPT] [INPUTFILE...]26 về xử lý tệp cũng được giữ ở đây sed -n '1p ; $p' one.txt two.txt three.txt77 Lệnh này làm trống nội dung của không gian mẫu. Nó thường giống như ‘s/. *//', nhưng hiệu quả hơn và hoạt động khi có chuỗi nhiều byte không hợp lệ trong luồng đầu vào. POSIX yêu cầu các chuỗi như vậy không khớp với '. ’, do đó không có cách di động nào để xóa bộ đệm của sed -i 's/hello/world/' file.txt22 ở giữa tập lệnh ở hầu hết các ngôn ngữ nhiều byte (bao gồm cả ngôn ngữ UTF-8) 3. 8 Cú pháp nhiều lệnhCó một số phương pháp để chỉ định nhiều lệnh trong chương trình sed -i 's/hello/world/' file.txt22 Sử dụng dòng mới là cách tự nhiên nhất khi chạy tập lệnh sed từ tệp (sử dụng tùy chọn -f) Trên dòng lệnh, tất cả các lệnh sed -i 's/hello/world/' file.txt22 có thể được phân tách bằng dòng mới. Ngoài ra, bạn có thể chỉ định từng lệnh làm đối số cho tùy chọn -e sed -n '1p ; $p' one.txt two.txt three.txt1 Dấu chấm phẩy (‘;’) có thể được sử dụng để phân tách hầu hết các lệnh đơn giản sed -n '1p ; $p' one.txt two.txt three.txt2 Các lệnh sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt78, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt79, sed -n '1p ; $p' one.txt two.txt three.txt82,_______19_______83, sed -n '1p ; $p' one.txt two.txt three.txt84, sed OPTIONS.. [SCRIPT] [INPUTFILE...]83 có thể được phân tách bằng dấu chấm phẩy (đây là tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22 không di động) sed -n '1p ; $p' one.txt two.txt three.txt3 Các nhãn được sử dụng trong các lệnh sed -n '1p ; $p' one.txt two.txt three.txt82, sed -n '1p ; $p' one.txt two.txt three.txt83, sed -n '1p ; $p' one.txt two.txt three.txt84, sed OPTIONS.. [SCRIPT] [INPUTFILE...]83 được đọc cho đến khi có dấu chấm phẩy. Khoảng trắng ở đầu và cuối bị bỏ qua. Trong các ví dụ bên dưới nhãn là 'x'. Ví dụ đầu tiên hoạt động với GNU sed -i 's/hello/world/' file.txt22. Thứ hai là tương đương di động. Để biết thêm thông tin về phân nhánh và nhãn, hãy xem sed -n '1p ; $p' one.txt two.txt three.txt4 3. 8. 1 Lệnh Yêu cầu một dòng mớiCác lệnh sau không thể được phân tách bằng dấu chấm phẩy và yêu cầu một dòng mới sed -n '1p ; $p' one.txt two.txt three.txt26,______19_______27, sed -n '1p ; $p' one.txt two.txt three.txt28 (nối thêm/thay đổi/chèn) Tất cả các ký tự theo sau lệnh sed -n '1p ; $p' one.txt two.txt three.txt26, sed -n '1p ; $p' one.txt two.txt three.txt27, sed -n '1p ; $p' one.txt two.txt three.txt28 được lấy làm văn bản để thêm/thay đổi/chèn. Sử dụng dấu chấm phẩy dẫn đến kết quả không mong muốn sed -n '1p ; $p' one.txt two.txt three.txt5 Tách các lệnh bằng cách sử dụng -e hoặc một dòng mới sed -n '1p ; $p' one.txt two.txt three.txt6 Lưu ý rằng việc chỉ định văn bản để thêm ('Xin chào') ngay sau sed -n '1p ; $p' one.txt two.txt three.txt26, sed -n '1p ; $p' one.txt two.txt three.txt27, sed -n '1p ; $p' one.txt two.txt three.txt28 chính nó là một tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22. Một giải pháp thay thế di động, tuân thủ POSIX là sed -n '1p ; $p' one.txt two.txt three.txt7 sed -n '1p ; $p' one.txt two.txt three.txt78 (bình luận) Tất cả các ký tự theo sau '#' cho đến dòng mới tiếp theo đều bị bỏ qua sed -n '1p ; $p' one.txt two.txt three.txt8 sed OPTIONS.. [SCRIPT] [INPUTFILE...]54,______0_______88, sed OPTIONS.. [SCRIPT] [INPUTFILE...]26, sed -i 's/hello/world/' file.txt33 (đọc và ghi tệp) Các lệnh sed OPTIONS.. [SCRIPT] [INPUTFILE...]54, sed -i 's/hello/world/' file.txt88, sed OPTIONS.. [SCRIPT] [INPUTFILE...]26, sed -i 's/hello/world/' file.txt33 phân tích cú pháp tên tệp cho đến cuối dòng. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào tên tệp, dẫn đến kết quả không mong muốn sed -n '1p ; $p' one.txt two.txt three.txt9 Lưu ý rằng sed -i 's/hello/world/' file.txt22 âm thầm bỏ qua lỗi đọc/ghi trong các lệnh sed OPTIONS.. [SCRIPT] [INPUTFILE...]54, sed -i 's/hello/world/' file.txt88, sed OPTIONS.. [SCRIPT] [INPUTFILE...]26, sed -i 's/hello/world/' file.txt33 (chẳng hạn như các tệp bị thiếu). Trong ví dụ sau, sed -i 's/hello/world/' file.txt22 cố gắng đọc một tệp có tên 'hello. văn bản ; . Tệp bị thiếu và lỗi được âm thầm bỏ qua sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt0 sed -n '1p ; $p' one.txt two.txt three.txt41 (thực thi lệnh) Bất kỳ ký tự nào theo sau lệnh sed -n '1p ; $p' one.txt two.txt three.txt41 cho đến cuối dòng sẽ được gửi tới trình bao. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào lệnh trình bao, dẫn đến kết quả không mong muốn sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt1 $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 317 (thay thế bằng cờ sed -n '1p ; $p' one.txt two.txt three.txt41 hoặc sed OPTIONS.. [SCRIPT] [INPUTFILE...]26) Trong một lệnh thay thế, cờ sed OPTIONS.. [SCRIPT] [INPUTFILE...]26 ghi kết quả thay thế vào một tệp và cờ sed -n '1p ; $p' one.txt two.txt three.txt41 thực thi kết quả thay thế dưới dạng lệnh trình bao. Như với các lệnh $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 322, chúng phải được kết thúc bằng một dòng mới. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào lệnh shell hoặc tên tệp, dẫn đến kết quả không mong muốn sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt2 4 địa chỉ. chọn dòng4. 1 Tổng quan về địa chỉCác địa chỉ xác định (các) dòng lệnh sed -i 's/hello/world/' file.txt22 sẽ được thực thi. Lệnh sau thay thế từ 'hello' bằng 'world' chỉ trên dòng 144 sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt3 Nếu không có địa chỉ nào được đưa ra, lệnh được thực hiện trên tất cả các dòng. Lệnh sau thay thế từ 'xin chào' bằng 'thế giới' trên tất cả các dòng trong tệp đầu vào sed 's/hello/world/' input.txt > output.txt Địa chỉ có thể chứa các biểu thức chính quy để khớp các dòng dựa trên nội dung thay vì số dòng. Lệnh sau chỉ thay thế từ 'hello' bằng 'world' trong các dòng có chứa từ 'apple' sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt5 Một dải địa chỉ được chỉ định với hai địa chỉ được phân tách bằng dấu phẩy ( $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 324). Địa chỉ có thể là số, biểu thức chính quy hoặc kết hợp cả hai. Lệnh sau thay thế từ 'xin chào' bằng 'thế giới' chỉ trong các dòng 4 đến 17 (đã bao gồm) sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt6 Việc thêm ký tự $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 325 vào cuối thông số địa chỉ (trước chữ cái lệnh) sẽ phủ nhận ý nghĩa của sự trùng khớp. Nghĩa là, nếu ký tự $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 325 theo sau một địa chỉ hoặc một dải địa chỉ, thì chỉ những dòng không khớp với địa chỉ mới được chọn. Lệnh sau chỉ thay thế từ 'xin chào' bằng 'thế giới' trong các dòng không chứa từ 'quả táo' sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt7 Lệnh sau chỉ thay thế từ 'hello' bằng 'world' trong các dòng từ 1 đến 3 và 18 cho đến dòng cuối cùng của tệp đầu vào (i. e. trừ dòng 4 đến 17) sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt8 4. 2 Chọn dòng theo sốĐịa chỉ trong tập lệnh sed -i 's/hello/world/' file.txt22 có thể ở bất kỳ dạng nào sau đây sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt28 Chỉ định một số dòng sẽ chỉ khớp với dòng đó trong đầu vào. (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 $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 331 Tiện ích mở rộng GNU này khớp với mọi dòng bước bắt đầu bằng dòng đầu tiên. Cụ thể, các dòng sẽ được chọn khi tồn tại một n không âm sao cho số dòng hiện tại bằng đầu tiên + (n * bước). Do đó, người ta sẽ sử dụng $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 332 để chọn các dòng đánh số lẻ và $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 333 cho các dòng đánh số chẵn; Các lệnh sau minh họa việc sử dụng địa chỉ bước sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt9 4. 3 dòng chọn bằng cách khớp văn bảnGNU sed -i 's/hello/world/' file.txt22 hỗ trợ các địa chỉ biểu thức chính quy sau. Biểu thức chính quy mặc định là. Nếu các tùy chọn -E hoặc -r được sử dụng, Biểu thức chính quy phải theo cú pháp. Thấy $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 336 Điều này sẽ chọn bất kỳ dòng nào khớp với biểu thức chính quy. Nếu chính biểu thức chính quy bao gồm bất kỳ ký tự sed -n '1p ; $p' one.txt two.txt three.txt94 nào, thì mỗi ký tự phải được thoát bằng dấu gạch chéo ngược ( sed -n '1p ; $p' one.txt two.txt three.txt97) Lệnh sau in các dòng trong /etc/passwd kết thúc bằng 'bash' sed OPTIONS.. [SCRIPT] [INPUTFILE...]0 Biểu thức chính quy trống '//' lặp lại kết quả khớp biểu thức chính quy cuối cùng (tương tự nếu biểu thức chính quy trống được truyền cho lệnh sed -n '1p ; $p' one.txt two.txt three.txt67). Lưu ý rằng các công cụ sửa đổi cho biểu thức chính quy được đánh giá khi biểu thức chính quy được biên dịch, do đó, việc chỉ định chúng cùng với biểu thức chính quy trống là không hợp lệ $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 340 (Có thể thay thế $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 341 bằng bất kỳ ký tự đơn nào khác. ) Điều này cũng phù hợp với biểu thức chính quy biểu thức chính quy, nhưng cho phép một người sử dụng một dấu phân cách khác với sed -n '1p ; $p' one.txt two.txt three.txt94. Điều này đặc biệt hữu ích nếu bản thân biểu thức chính quy chứa nhiều dấu gạch chéo, vì nó tránh được việc thoát tẻ nhạt của mỗi sed -n '1p ; $p' one.txt two.txt three.txt94. Nếu bản thân biểu thức chính quy bao gồm bất kỳ ký tự phân cách nào, thì mỗi ký tự đó phải được thoát bằng dấu gạch chéo ngược ( sed -n '1p ; $p' one.txt two.txt three.txt97) Các lệnh sau đây là tương đương. Họ in các dòng bắt đầu bằng '/home/alice/documents/' sed OPTIONS.. [SCRIPT] [INPUTFILE...]1 $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 345_______35_______46 Công cụ sửa đổi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt45 để khớp biểu thức chính quy là một tiện ích mở rộng GNU làm cho biểu thức chính quy được khớp theo cách không phân biệt chữ hoa chữ thường Trong nhiều ngôn ngữ lập trình khác, chữ thường sed -n '1p ; $p' one.txt two.txt three.txt28 được sử dụng để đối sánh biểu thức chính quy không phân biệt chữ hoa chữ thường. Tuy nhiên, trong sed -i 's/hello/world/' file.txt22, sed -n '1p ; $p' one.txt two.txt three.txt28 được sử dụng cho lệnh chèn (xem phần ) Quan sát sự khác biệt giữa các ví dụ sau Trong ví dụ này, $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 351 là địa chỉ. biểu thức chính quy với công cụ sửa đổi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt45. sed -i 's/hello/world/' file.txt36 là lệnh xóa sed OPTIONS.. [SCRIPT] [INPUTFILE...]2 Đây, $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 354 là địa chỉ. một biểu thức chính quy. sed -n '1p ; $p' one.txt two.txt three.txt28 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 OPTIONS.. [SCRIPT] [INPUTFILE...]3 $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 357____35_______58 Công cụ sửa đổi sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt49 để khớp biểu thức chính quy là một tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22 hướng GNU sed -i 's/hello/world/' file.txt22 khớp biểu thức chính quy ở chế độ nhiều dòng. Công cụ sửa đổi làm cho sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 và sed -i 's/hello/world/' file.txt87 khớp tương ứng (ngoài hành vi bình thường) chuỗi trống sau một dòng mới và chuỗi trống trước một dòng mới. Có các chuỗi ký tự đặc biệt ( sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt56 và sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt57) luôn khớp với phần đầu hoặc phần cuối của bộ đệm. Ngoài ra, ký tự dấu chấm không khớp với ký tự dòng mới ở chế độ nhiều dòng Địa chỉ regex hoạt động trên nội dung của không gian mẫu hiện tại. Nếu không gian mẫu bị thay đổi (ví dụ: với lệnh $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 366), khớp biểu thức chính quy sẽ hoạt động trên văn bản đã thay đổi Trong ví dụ sau, in tự động bị tắt với -n. Lệnh $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 367 thay đổi các dòng chứa '2' thành 'X'. Lệnh $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 368 khớp các dòng có chữ số và in chúng. Bởi vì dòng thứ hai được thay đổi trước biểu thức chính quy $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 369, nó sẽ không khớp và sẽ không được in sed OPTIONS.. [SCRIPT] [INPUTFILE...]4 4. 4 dãy địa chỉCó thể chỉ định phạm vi địa chỉ bằng cách chỉ định hai địa chỉ được phân tách bằng dấu phẩy ( $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 324). Một dải địa chỉ khớp với các dòng bắt đầu từ nơi địa chỉ đầu tiên khớp và tiếp tục cho đến khi địa chỉ thứ hai khớp (bao gồm) sed OPTIONS.. [SCRIPT] [INPUTFILE...]5 Nếu địa chỉ thứ hai là biểu thức chính quy, thì việc kiểm tra kết quả khớp sẽ bắt đầu bằng dòng theo sau dòng khớp với địa chỉ đầu tiên. một phạm vi sẽ luôn kéo dài ít nhất hai dòng (tất nhiên là ngoại trừ nếu luồng đầu vào kết thúc) sed OPTIONS.. [SCRIPT] [INPUTFILE...]6 Nếu địa chỉ thứ hai là một số nhỏ hơn (hoặc bằng) dòng khớp với địa chỉ đầu tiên, thì chỉ có một dòng được khớp sed OPTIONS.. [SCRIPT] [INPUTFILE...]7 GNU sed -i 's/hello/world/' file.txt22 cũng hỗ trợ một số biểu mẫu hai địa chỉ đặc biệt; $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 372 Một số dòng của $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 373 có thể được sử dụng trong một đặc tả địa chỉ như $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 372 để sed -i 's/hello/world/' file.txt22 cũng sẽ cố khớp với biểu thức chính quy trong dòng đầu vào đầu tiên. Nói cách khác, $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 372 tương tự như $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 377, ngoại trừ việc nếu addr2 khớp với dòng đầu tiên của đầu vào, biểu mẫu $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 372 sẽ coi nó là kết thúc phạm vi, trong khi biểu mẫu $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 377 sẽ khớp với phần đầu của phạm vi và do đó làm cho phạm vi mở rộng Lưu ý rằng đây là nơi duy nhất mà địa chỉ $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 373 có ý nghĩa; Các ví dụ sau minh họa sự khác biệt giữa bắt đầu bằng địa chỉ 1 và 0 sed OPTIONS.. [SCRIPT] [INPUTFILE...]8 $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 382 So khớp addr1 và N dòng sau addr1 sed OPTIONS.. [SCRIPT] [INPUTFILE...]9 addr1 can be a line number or a regular expression $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 383 So khớp addr1 và các dòng theo sau addr1 cho đến dòng tiếp theo có số dòng đầu vào là bội số của N. Lệnh sau in bắt đầu từ dòng 6, cho đến dòng tiếp theo là bội số của 4 (i. e. dòng 8) $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 30 addr1 can be a line number or a regular expression 5 biểu thức chính quy. chọn văn bản5. 1 Tổng quan về biểu thức chính quy trong sed -i 's/hello/world/' file.txt 22Để biết cách sử dụng sed -i 's/hello/world/' file.txt22, mọi người nên hiểu biểu thức chính quy (viết tắt là regexp). Biểu thức chính quy là một mẫu được khớp với chuỗi chủ đề từ trái sang phải. Hầu hết các nhân vật đều bình thường. chúng đại diện cho chính chúng theo một mẫu và khớp với các ký tự tương ứng. Biểu thức chính quy trong sed -i 's/hello/world/' file.txt22 được chỉ định giữa hai dấu gạch chéo Lệnh sau in các dòng có chứa từ 'hello' Ví dụ trên tương đương với lệnh $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 387 này Sức mạnh của biểu thức chính quy đến từ khả năng bao gồm các lựa chọn thay thế và lặp lại trong mẫu. Chúng được mã hóa trong mẫu bằng cách sử dụng các ký tự đặc biệt, không đại diện cho chính chúng mà thay vào đó được diễn giải theo một cách đặc biệt nào đó Ký tự sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 (dấu mũ) trong biểu thức chính quy khớp với đầu dòng. Ký tự $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 389 (dấu chấm) khớp với bất kỳ ký tự đơn nào. Lệnh sed -i 's/hello/world/' file.txt22 sau khớp và in các dòng bắt đầu bằng chữ 'b', theo sau là bất kỳ ký tự đơn nào, theo sau là chữ 'd' $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 31 Các phần sau giải thích ý nghĩa và cách sử dụng các ký tự đặc biệt trong biểu thức chính quy 5. 2 Biểu thức chính quy cơ bản (BRE) và mở rộng (ERE)Biểu thức chính quy cơ bản và mở rộng là hai biến thể về cú pháp của mẫu đã chỉ định. 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 $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 387). Sử dụng tùy chọn -E do POSIX chỉ định (-r, --regexp-extended) để bật cú pháp Biểu thức chính quy mở rộng (ERE) Trong GNU sed -i 's/hello/world/' file.txt22, sự khác biệt duy nhất giữa biểu thức chính quy cơ bản và mở rộng là ở hành vi của một vài ký tự đặc biệt. ‘?’, ‘+’, dấu ngoặc đơn, dấu ngoặc nhọn (‘{}’) và ‘. ’ Với cú pháp cơ bản (BRE), các ký tự này không có ý nghĩa đặc biệt trừ khi được đặt trước bằng dấu gạch chéo ngược ('\'); . các ký tự này là đặc biệt trừ khi chúng có tiền tố là dấu gạch chéo ngược ('\') Mẫu mong muốn Cơ bản (BRE) Cú phápMở rộng (ERE) Cú phápChữ ‘+’ (dấu cộng)$ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 32 $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 33Một hoặc nhiều ký tự 'a' theo sau là 'b' (dấu cộng là siêu ký tự đặc biệt) $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 34 $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 35 5. 3 Overview of basic regular expression syntaxDưới đây là mô tả ngắn gọn về cú pháp biểu thức chính quy như được sử dụng trong sed -i 's/hello/world/' file.txt22 $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 395 Một ký tự thông thường duy nhất khớp với chính nó sed -i 's/hello/world/' file.txt60 Matches a sequence of zero or more instances of matches for the preceding regular expression, which must be an ordinary character, a special character preceded by sed -n '1p ; $p' one.txt two.txt three.txt97, a $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 389, a grouped regexp (see below), or a bracket expression. As a GNU extension, a postfixed regular expression can also be followed by sed -i 's/hello/world/' file.txt60; for example, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE00 is equivalent to # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE01. POSIX 1003. 1-2001 says that sed -i 's/hello/world/' file.txt60 stands for itself when it appears at the start of a regular expression or subexpression, but many nonGNU implementations do not support this and portable scripts should instead use # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE03 in these contexts $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 389 Matches any character, including newline sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 Matches the null string at beginning of the pattern space, i. e. what appears after the circumflex must appear at the beginning of the pattern space In most scripts, pattern space is initialized to the content of each line (see ). So, it is a useful simplification to think of # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE07 as matching only lines where ‘#include’ is the first thing on line—if there are spaces before, for example, the match fails. This simplification is valid as long as the original content of pattern space is not modified, for example with an sed -n '1p ; $p' one.txt two.txt three.txt67 command sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 acts as a special character only at the beginning of the regular expression or subexpression (that is, after sed -n '1p ; $p' one.txt two.txt three.txt91 or # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE11). Portable scripts should avoid sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 at the beginning of a subexpression, though, as POSIX allows implementations that treat sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 as an ordinary character in that context sed -i 's/hello/world/' file.txt87 It is the same as sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54, but refers to end of pattern space. sed -i 's/hello/world/' file.txt87 also acts as a special character only at the end of the regular expression or subexpression (that is, before sed -n '1p ; $p' one.txt two.txt three.txt92 or # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE11), and its use at the end of a subexpression is not portable # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE19 # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE20 Matches any single character in list. for example, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE21 matches all vowels. A list may include sequences like # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE22, which matches any character between (inclusive) char1 and char2. See # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE23 As sed -i 's/hello/world/' file.txt60, but matches one or more. It is a GNU extension # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE25 As sed -i 's/hello/world/' file.txt60, but only matches zero or one. It is a GNU extension # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE27 As sed -i 's/hello/world/' file.txt60, but matches exactly i sequences (i is a decimal integer; for portability, keep it between 0 and 255 inclusive) # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE29 Matches between i and j, inclusive, sequences # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE30 Matches more than or equal to i sequences # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE31 Groups the inner regexp as a whole, this is used to
# WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE35 Matches either regexp1 or regexp2. Use parentheses to use complex alternative regular expressions. The matching process tries each alternative in turn, from left to right, and the first one that succeeds is used. It is a GNU extension # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE36 Matches the concatenation of regexp1 and regexp2. Concatenation binds more tightly than # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE11, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54, and sed -i 's/hello/world/' file.txt87, but less tightly than the other regular expression operators # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE40 Matches the digit-th # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE41 parenthesized subexpression in the regular expression. This is called a back reference. Subexpressions are implicitly numbered by counting occurrences of sed -n '1p ; $p' one.txt two.txt three.txt91 left-to-right sed -n '1p ; $p' one.txt two.txt three.txt90 Matches the newline character # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE44 Matches char, where char is one of sed -i 's/hello/world/' file.txt87, sed -i 's/hello/world/' file.txt60, $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 389, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE48, sed -n '1p ; $p' one.txt two.txt three.txt97, or sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54. Note that the only C-like backslash sequences that you can portably assume to be interpreted are sed -n '1p ; $p' one.txt two.txt three.txt90 and sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt95; in particular # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE53 is not portable, and matches a ‘t’ under most implementations of sed -i 's/hello/world/' file.txt22, rather than a tab character Note that the regular expression matcher is greedy, i. e. , matches are attempted from left to right and, if two or more matches are possible starting at the same character, it selects the longest Examples ‘abcdef’Matches ‘abcdef’ ‘a*b’Matches zero or more ‘a’s followed by a single ‘b’. For example, ‘b’ or ‘aaaaab’ ‘a\?b’Matches ‘b’ or ‘ab’ ‘a\+b\+’Matches one or more ‘a’s followed by one or more ‘b’s. ‘ab’ is the shortest possible match, but other examples are ‘aaaab’ or ‘abbbbb’ or ‘aaaaaabbbbbbb’ ‘. *’‘. \+’These two both match all the characters in a string; however, the first matches every string (including the empty string), while the second matches only strings containing at least one character ‘^main. *(. *)’This matches a string starting with ‘main’, followed by an opening and closing parenthesis. The ‘n’, ‘(’ and ‘)’ need not be adjacent ‘^#’This matches a string beginning with ‘#’ ‘\\$’This matches a string ending with a single backslash. The regexp contains two backslashes for escaping ‘\$’Instead, this matches a string consisting of a single dollar sign, because it is escaped ‘[a-zA-Z0-9]’In the C locale, this matches any ASCII letters or digits ‘[^ TAB]\+’(Here TAB stands for a single tab character. ) This matches a string of one or more characters, none of which is a space or a tab. Usually this means a word ‘^\(. *\)\n\1$’This matches a string consisting of two equal substrings separated by a newline ‘. \{9\}A$’This matches nine characters followed by an ‘A’ at the end of a line ‘^. \{15\}A’This matches the start of a string that contains 16 characters, the last of which is an ‘A’ 5. 4 Overview of extended regular expression syntaxThe only difference between basic and extended regular expressions is in the behavior of a few characters. ‘?’, ‘+’, parentheses, braces (‘{}’), and ‘. ’. While basic regular expressions require these to be escaped if you want them to behave as special characters, when using extended regular expressions you must escape them if you want them to match a literal character. ‘. ’ is special here because ‘\. ’ is a GNU extension – standard basic regular expressions do not provide its functionality Examples # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE55 trở thành 'abc\?' khi sử dụng các biểu thức chính quy mở rộng. Nó khớp với chuỗi ký tự 'abc?' # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE56 trở thành 'c+' khi sử dụng các biểu thức chính quy mở rộng. Nó khớp với một hoặc nhiều chữ 'c' # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE57 becomes ‘a{3,}’ when using extended regular expressions. It matches three or more ‘a’s # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE58 becomes ‘(abc){2,3}’ when using extended regular expressions. It matches either ‘abcabc’ or ‘abcabcabc’ # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE59 becomes ‘(abc*)\1’ when using extended regular expressions. Backreferences must still be escaped when using extended regular expressions # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE60 becomes ‘a. b’ when using extended regular expressions. It matches ‘a’ or ‘b’ 5. 5 Character Classes and Bracket ExpressionsA bracket expression is a list of characters enclosed by ‘[’ and ‘]’. It matches any single character in that list; if the first character of the list is the caret ‘^’, then it matches any character not in the list. For example, the following command replaces the words ‘gray’ or ‘grey’ with ‘blue’ Bracket expressions can be used in both and regular expressions (that is, with or without the -E/-r options) Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive. In the default C locale, the sorting sequence is the native character order; for example, ‘[a-d]’ is equivalent to ‘[abcd]’ Finally, certain named classes of characters are predefined within bracket expressions, as follows These named classes must be used inside brackets themselves. Correct usage $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 36 Incorrect usage is rejected by newer sed -i 's/hello/world/' file.txt22 versions. Older versions accepted it but treated it as a single bracket expression (which is equivalent to ‘[dgit. ]’, that is, only the characters d/g/i/t/. ) $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 37‘[. alnum. ]’ Alphanumeric characters. ‘[. alpha. ]’ and ‘[. digit. ]’; in the ‘C’ locale and ASCII character encoding, this is the same as ‘[0-9A-Za-z]’ ‘[. alpha. ]’Alphabetic characters. ‘[. lower. ]’ and ‘[. upper. ]’; in the ‘C’ locale and ASCII character encoding, this is the same as ‘[A-Za-z]’ ‘[. blank. ]’Blank characters. space and tab ‘[. cntrl. ]’Control characters. In ASCII, these characters have octal codes 000 through 037, and 177 (DEL). In other character sets, these are the equivalent characters, if any ‘[. digit. ]’Digits. # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE62‘[. graph. ]’ Graphical characters. ‘[. alnum. ]’ and ‘[. punct. ]’ ‘[. lower. ]’Lower-case letters; in the ‘C’ locale and ASCII character encoding, this is # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE63‘[. print. ]’ Printable characters. ‘[. alnum. ]’, ‘[. punct. ]’, and space ‘[. punct. ]’Punctuation characters; in the ‘C’ locale and ASCII character encoding, this is # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE64‘[. space. ]’ Space characters. in the ‘C’ locale, this is tab, newline, vertical tab, form feed, carriage return, and space ‘[. upper. ]’Upper-case letters. in the ‘C’ locale and ASCII character encoding, this is # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE65‘[. xdigit. ]' Hexadecimal digits. # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE66 Note that the brackets in these class names are part of the symbolic names, and must be included in addition to the brackets delimiting the bracket expression Most meta-characters lose their special meaning inside bracket expressions ‘]’kết thúc biểu thức ngoặc nếu nó không phải là mục danh sách đầu tiên. Vì vậy, nếu bạn muốn đặt ký tự ‘]’ thành một mục trong danh sách, bạn phải đặt ký tự này trước '-'đại diện cho phạm vi nếu nó không phải là đầu tiên hoặc cuối cùng trong danh sách hoặc điểm kết thúc của một phạm vi '^'đại diện cho các ký tự không có trong danh sách. If you want to make the ‘^’ character a list item, place it anywhere but first LÀM. kết hợp đoạn này (sao chép nguyên văn từ phần BRE) Các ký tự sed -i 's/hello/world/' file.txt87, sed -i 's/hello/world/' file.txt60, $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 389, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE48, và sed -n '1p ; $p' one.txt two.txt three.txt97 thường không có gì đặc biệt trong danh sách. Ví dụ: # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE72 khớp với ‘\’ hoặc ‘*’, vì sed -n '1p ; $p' one.txt two.txt three.txt97 không có gì đặc biệt ở đây. Tuy nhiên, các chuỗi như # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE74, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE75 và # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE76 là đặc biệt trong danh sách và đại diện cho các ký hiệu đối chiếu, lớp tương đương và lớp ký tự tương ứng, và do đó, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE48 đặc biệt trong danh sách khi nó được theo sau bởi $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 389, sed -n '1p ; $p' one.txt two.txt three.txt80 hoặc sed OPTIONS.. [SCRIPT] [INPUTFILE...]83. Ngoài ra, khi không ở chế độ sed -i 's/hello/world/' file.txt72, các lần thoát đặc biệt như sed -n '1p ; $p' one.txt two.txt three.txt90 và # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE53 được công nhận trong danh sách. Thấy‘[. ’ đại diện cho biểu tượng đối chiếu mở ‘. ]'đại diện cho biểu tượng đối chiếu gần ‘[=’represents the open equivalence class ‘=]’represents the close equivalence class ‘[. ’represents the open character class symbol, and should be followed by a valid character class name ‘. ]’represents the close character class symbol 5. 6 regular expression extensionsThe following sequences have special meaning inside regular expressions (used in and the sed -n '1p ; $p' one.txt two.txt three.txt67 command) These can be used in both and regular expressions (that is, with or without the -E/-r options) # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE85 Matches any “word” character. A “word” character is any letter or digit or the underscore character $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 38 # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE86 Matches any “non-word” character $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 39 # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE87 Matches a word boundary; that is it matches if the character to the left is a “word” character and the character to the right is a “non-word” character, or vice-versa # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE0 # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE88 Matches everywhere but on a word boundary; that is it matches if the character to the left and the character to the right are either both “word” characters or both “non-word” characters # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE1 # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE89 Matches whitespace characters (spaces and tabs). Newlines embedded in the pattern/hold spaces will also match # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE2 # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE90 Matches non-whitespace characters # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE3 # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE91 Matches the beginning of a word # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE4 # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE92 Matches the end of a word # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE5 sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt56 Matches only at the start of pattern space. This is different from sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt54 in multi-line mode Compare the following two examples # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE6 sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt57 Matches only at the end of pattern space. This is different from sed -i 's/hello/world/' file.txt87 in multi-line mode 5. 7 Back-references and Subexpressionsback-references are regular expression commands which refer to a previous part of the matched regular expression. Back-references are specified with backslash and a single digit (e. g. ‘\1’). The part of the regular expression they refer to is called a subexpression, and is designated with parentheses Back-references and subexpressions are used in two cases. in the regular expression search pattern, and in the replacement part of the sed -n '1p ; $p' one.txt two.txt three.txt67 command (see and ) In a regular expression pattern, back-references are used to match the same content as a previously matched subexpression. In the following example, the subexpression is ‘. ’ - any single character (being surrounded by parentheses makes it a subexpression). The back-reference ‘\1’ asks to match the same content (same character) as the sub-expression The command below matches words starting with any character, followed by the letter ‘o’, followed by the same character as the first # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE7 Multiple subexpressions are automatically numbered from left-to-right. This command searches for 6-letter palindromes (the first three letters are 3 subexpressions, followed by 3 back-references in reverse order) # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE8 In the sed -n '1p ; $p' one.txt two.txt three.txt67 command, back-references can be used in the replacement part to refer back to subexpressions in the regexp part The following example uses two subexpressions in the regular expression to match two space-separated words. The back-references in the replacement part prints the words in a different order # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE9 When used with alternation, if the group does not participate in the match then the back-reference makes the whole match fail. For example, ‘a(. ). b\1’ will not match ‘ba’. When multiple regular expressions are given with -e or from a file (‘-f file’), back-references are local to each expression 5. 8 Escape Sequences - specifying special charactersUntil this chapter, we have only encountered escapes of the form ‘\^’, which tell sed -i 's/hello/world/' file.txt22 not to interpret the circumflex as a special character, but rather to take it literally. For example, ‘\*’ matches a single asterisk rather than zero or more backslashes This chapter introduces another kind of escape—that is, escapes that are applied to a character or sequence of characters that ordinarily are taken literally, and that sed -i 's/hello/world/' file.txt22 replaces with a special character. This provides a way of encoding non-printable characters in patterns in a visible manner. There is no restriction on the appearance of non-printing characters in a sed -i 's/hello/world/' file.txt22 script but when a script is being prepared in the shell or by text editing, it is usually easier to use one of the following escape sequences than the binary character it represents The list of these escapes is $ echo | sed 'Q42' ; echo $? 4202 Produces or matches a BEL character, that is an “alert” (ASCII 7) $ echo | sed 'Q42' ; echo $? 4203 Produces or matches a form feed (ASCII 12) sed -n '1p ; $p' one.txt two.txt three.txt90 Produces or matches a newline (ASCII 10) $ echo | sed 'Q42' ; echo $? 4205 Produces or matches a carriage return (ASCII 13) # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE53 Produces or matches a horizontal tab (ASCII 9) $ echo | sed 'Q42' ; echo $? 4207 Produces or matches a so called “vertical tab” (ASCII 11) $ echo | sed 'Q42' ; echo $? 4208 Produces or matches CONTROL-x, where x is any character. Tác dụng chính xác của ‘\cx’ như sau. nếu x là chữ thường, nó được chuyển thành chữ hoa. Sau đó, bit 6 của ký tự (hex 40) được đảo ngược. Do đó, '\cz' trở thành hex 1A, nhưng '\c{' trở thành hex 3B, trong khi '\c;' trở thành hex 7B $ echo | sed 'Q42' ; echo $? 4209 Tạo hoặc khớp một ký tự có giá trị ASCII thập phân là xxx $ echo | sed 'Q42' ; echo $? 4210 Produces or matches a character whose octal ASCII value is xxx $ echo | sed 'Q42' ; echo $? 4211 Produces or matches a character whose hexadecimal ASCII value is xx ‘\b’ (backspace) was omitted because of the conflict with the existing “word boundary” meaning 5. 8. 1 Escaping PrecedenceGNU sed -i 's/hello/world/' file.txt22 processes escape sequences before passing the text onto the regular-expression matching of the $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 366 command and Address matching. Thus the follwing two commands are equivalent (‘0x5e’ is the hexadecimal ASCII value of the character ‘^’) $ echo | sed 'Q42' ; echo $? 420 As are the following (‘0x5b’,‘0x5d’ are the hexadecimal ASCII values of ‘[’,‘]’, respectively) $ echo | sed 'Q42' ; echo $? 421 However it is recommended to avoid such special characters due to unexpected edge-cases. For example, the following are not equivalent $ echo | sed 'Q42' ; echo $? 422 5. 9 Multibyte characters and Locale ConsiderationsGNU sed -i 's/hello/world/' file.txt22 processes valid multibyte characters in multibyte locales (e. g. $ echo | sed 'Q42' ; echo $? 4215). The following example uses the Greek letter Capital Sigma (Σ, Unicode code point $ echo | sed 'Q42' ; echo $? 4216). In a $ echo | sed 'Q42' ; echo $? 4215 locale, sed -i 's/hello/world/' file.txt22 correctly processes the Sigma as one character despite it being 2 octets (bytes) $ echo | sed 'Q42' ; echo $? 423 To force sed -i 's/hello/world/' file.txt22 to process octets separately, use the $ echo | sed 'Q42' ; echo $? 4220 locale (also known as the $ echo | sed 'Q42' ; echo $? 4221 locale) $ echo | sed 'Q42' ; echo $? 424 5. 9. 1 Invalid multibyte characterssed -i 's/hello/world/' file.txt22’s regular expressions do not match invalid multibyte sequences in a multibyte locale In the following examples, the ascii value $ echo | sed 'Q42' ; echo $? 4223 is an incomplete multibyte character (shown here as �). The regular expression ‘. ’ does not match it $ echo | sed 'Q42' ; echo $? 425 Similarly, the ’catch-all’ regular expression ‘. *’ does not match the entire line $ echo | sed 'Q42' ; echo $? 426 GNU sed -i 's/hello/world/' file.txt22 offers the special sed -n '1p ; $p' one.txt two.txt three.txt77 command to clear the current pattern space regardless of invalid multibyte characters (i. e. it works like $ echo | sed 'Q42' ; echo $? 4226 but also removes invalid multibyte characters) $ echo | sed 'Q42' ; echo $? 427 Alternatively, force the $ echo | sed 'Q42' ; echo $? 4220 locale to process each octet separately (every octet is a valid character in the $ echo | sed 'Q42' ; echo $? 4220 locale) $ echo | sed 'Q42' ; echo $? 428 sed -i 's/hello/world/' file.txt22’s inability to process invalid multibyte characters can be used to detect such invalid sequences in a file. In the following examples, the $ echo | sed 'Q42' ; echo $? 4230 is an invalid multibyte sequence, while $ echo | sed 'Q42' ; echo $? 4231 is a valid multibyte sequence (of the Greek Sigma character) The following sed -i 's/hello/world/' file.txt22 program removes all valid characters using $ echo | sed 'Q42' ; echo $? 4233. Any content left in the pattern space (the invalid characters) are added to the hold space using the sed -n '1p ; $p' one.txt two.txt three.txt47 command. On the last line ( sed -i 's/hello/world/' file.txt87), the hold space is retrieved ( sed -n '1p ; $p' one.txt two.txt three.txt75), newlines are removed ( $ echo | sed 'Q42' ; echo $? 4237), and any remaining octets are printed unambiguously ( sed -i 's/hello/world/' file.txt67). Thus, any invalid multibyte sequences are printed as octal values $ echo | sed 'Q42' ; echo $? 429 With a few more commands, sed -i 's/hello/world/' file.txt22 can print the exact line number corresponding to each invalid characters (line 3). These characters can then be removed by forcing the $ echo | sed 'Q42' ; echo $? 4220 locale and using octal escape sequences sed '30,35d' input.txt > output.txt0 5. 9. 2 Upper/Lower case conversionGNU sed -i 's/hello/world/' file.txt22’s substitute command ( sed -n '1p ; $p' one.txt two.txt three.txt67) supports upper/lower case conversions using sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt05, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt04 codes. These conversions support multibyte characters sed '30,35d' input.txt > output.txt1 See 5. 9. 3 Multibyte regexp character classesIn other locales, the sorting sequence is not specified, and ‘[a-d]’ might be equivalent to ‘[abcd]’ or to ‘[aBbCcDd]’, or it might fail to match any character, or the set of characters that it matches might even be erratic. To obtain the traditional interpretation of bracket expressions, you can use the ‘C’ locale by setting the $ echo | sed 'Q42' ; echo $? 4245 environment variable to the value ‘C’ sed '30,35d' input.txt > output.txt2 Their interpretation depends on the $ echo | sed 'Q42' ; echo $? 4246 locale; for example, ‘[[. alnum. ]]’ means the character class of numbers and letters in the current locale TODO. show example of collation sed '30,35d' input.txt > output.txt3 6 Advanced sed -i 's/hello/world/' file.txt 22. cycles and buffers6. 1 How sed -i 's/hello/world/' file.txt 22 Workssed -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. 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 BuffersTODO 6. 3 Multiline techniques - using D,G,H,N,P to process multiple linesMultiple lines can be processed as one buffer using the sed -n '1p ; $p' one.txt two.txt three.txt39, sed -n '1p ; $p' one.txt two.txt three.txt45, sed -n '1p ; $p' one.txt two.txt three.txt47, sed -i 's/hello/world/' file.txt71, sed -n '1p ; $p' one.txt two.txt three.txt57. They are similar to their lowercase counterparts ( sed -i 's/hello/world/' file.txt36, sed -n '1p ; $p' one.txt two.txt three.txt44, sed -n '1p ; $p' one.txt two.txt three.txt46, sed -n '1p ; $p' one.txt two.txt three.txt52, sed -i 's/hello/world/' file.txt37), except that these commands append or subtract data while respecting embedded newlines - allowing adding and removing lines from the pattern and hold spaces They operate as follows sed -n '1p ; $p' one.txt two.txt three.txt39 deletes line from the pattern space until the first newline, and restarts the cycle sed -n '1p ; $p' one.txt two.txt three.txt45 appends line from the hold space to the pattern space, with a newline before it sed -n '1p ; $p' one.txt two.txt three.txt47 appends line from the pattern space to the hold space, with a newline before it sed -i 's/hello/world/' file.txt71 appends line from the input file to the pattern space sed -n '1p ; $p' one.txt two.txt three.txt57 prints line from the pattern space until the first newline The following example illustrates the operation of sed -i 's/hello/world/' file.txt71 and sed -n '1p ; $p' one.txt two.txt three.txt39 commands sed '30,35d' input.txt > output.txt4
A common technique to process blocks of text such as paragraphs (instead of line-by-line) is using the following construct sed '30,35d' input.txt > output.txt5
The following example demonstrates this technique sed '30,35d' input.txt > output.txt6 For more annotated examples, see and 6. 4 Branching and Flow ControlCác lệnh rẽ nhánh sed -n '1p ; $p' one.txt two.txt three.txt82, sed -n '1p ; $p' one.txt two.txt three.txt83 và sed -n '1p ; $p' one.txt two.txt three.txt84 cho phép thay đổi luồng của chương trình sed -i 's/hello/world/' file.txt22 Theo mặc định, sed -i 's/hello/world/' file.txt22 đọc một dòng đầu vào vào bộ đệm mẫu, sau đó tiếp tục xử lý tất cả các lệnh theo thứ tự. Commands without addresses affect all lines. Các lệnh có địa chỉ chỉ ảnh hưởng đến các dòng phù hợp. See and sed -i 's/hello/world/' file.txt22 does not support a typical $ echo | sed 'Q42' ; echo $? 4285 construct. Instead, some commands can be used as conditionals or to change the default flow control sed -i 's/hello/world/' file.txt36 delete (clears) the current pattern space, and restart the program cycle without processing the rest of the commands and without printing the pattern space sed -n '1p ; $p' one.txt two.txt three.txt39 delete the contents of the pattern space up to the first newline, and restart the program cycle without processing the rest of the commands and without printing the pattern space $ echo | sed 'Q42' ; echo $? 4288 $ echo | sed 'Q42' ; echo $? 4289 $ echo | sed 'Q42' ; echo $? 4290 $ echo | sed 'Q42' ; echo $? 4291 Addresses and regular expressions can be used as an $ echo | sed 'Q42' ; echo $? 4285 conditional. If [addr] matches the current pattern space, execute the command(s). For example. The command $ echo | sed 'Q42' ; echo $? 4293 means. if the current pattern matches the regular expression $ echo | sed 'Q42' ; echo $? 4294 (a line starting with a hash), then execute the sed -i 's/hello/world/' file.txt36 command. delete the line without printing it, and restart the program cycle immediately sed -n '1p ; $p' one.txt two.txt three.txt82 branch unconditionally (that is. always jump to a label, skipping or repeating other commands, without restarting a new cycle). Combined with an address, the branch can be conditionally executed on matched lines sed -n '1p ; $p' one.txt two.txt three.txt83 branch conditionally (that is. jump to a label) only if a $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 366 command has succeeded since the last input line was read or another conditional branch was taken sed -n '1p ; $p' one.txt two.txt three.txt84 similar but opposite to the sed -n '1p ; $p' one.txt two.txt three.txt83 command. branch only if there has been no successful substitutions since the last input line was read The following two sed -i 's/hello/world/' file.txt22 programs are equivalent. The first (contrived) example uses the sed -n '1p ; $p' one.txt two.txt three.txt82 command to skip the $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 366 command on lines containing ‘1’. The second example uses an address with negation (‘. ’) to perform substitution only on desired lines. The sed '30,35d' input.txt > output.txt04 command is still executed on all lines sed '30,35d' input.txt > output.txt7 6. 4. 1 Branching and CyclesThe sed -n '1p ; $p' one.txt two.txt three.txt82, sed -n '1p ; $p' one.txt two.txt three.txt83 and sed -n '1p ; $p' one.txt two.txt three.txt84 commands can be followed by a label (typically a single letter). Labels are defined with a colon followed by one or more letters (e. g. ‘. x’). If the label is omitted the branch commands restart the cycle. Note the difference between branching to a label and restarting the cycle. when a cycle is restarted, sed -i 's/hello/world/' file.txt22 first prints the current content of the pattern space, then reads the next input line into the pattern space; Jumping to a label (even if it is at the beginning of the program) does not print the pattern space and does not read the next input line The following program is a no-op. The sed -n '1p ; $p' one.txt two.txt three.txt82 command (the only command in the program) does not have a label, and thus simply restarts the cycle. On each cycle, the pattern space is printed and the next input line is read The following example is an infinite-loop - it doesn’t terminate and doesn’t print anything. The sed -n '1p ; $p' one.txt two.txt three.txt82 command jumps to the ‘x’ label, and a new cycle is never started sed '30,35d' input.txt > output.txt8 Branching is often complemented with the sed -n '1p ; $p' one.txt two.txt three.txt52 or sed -i 's/hello/world/' file.txt71 commands. both commands read the next input line into the pattern space without waiting for the cycle to restart. Before reading the next input line, sed -n '1p ; $p' one.txt two.txt three.txt52 prints the current pattern space then empties it, while sed -i 's/hello/world/' file.txt71 appends a newline and the next input line to the pattern space Consider the following two examples sed '30,35d' input.txt > output.txt9
6. 4. 2 Branching example. joining linesAs a real-world example of using branching, consider the case of quoted-printable files, typically used to encode email messages. In these files long lines are split and marked with a soft line break consisting of a single ‘=’ character at the end of the line sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt01 The following program uses an address match ‘/=$/’ as a conditional. If the current pattern space ends with a ‘=’, it reads the next input line using sed -i 's/hello/world/' file.txt71, replaces all ‘=’ characters which are followed by a newline, and unconditionally branches ( sed -n '1p ; $p' one.txt two.txt three.txt82) to the beginning of the program without restarting a new cycle. If the pattern space does not ends with ‘=’, the default action is performed. the pattern space is printed and a new cycle is started sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt02 Here’s an alternative program with a slightly different approach. On all lines except the last, sed -i 's/hello/world/' file.txt71 appends the line to the pattern space. A substitution command then removes soft line breaks (‘=’ at the end of a line, i. e. followed by a newline) by replacing them with an empty string. if the substitution was successful (meaning the pattern space contained a line which should be joined), The conditional branch command sed -n '1p ; $p' one.txt two.txt three.txt83 jumps to the beginning of the program without completing or restarting the cycle. If the substitution failed (meaning there were no soft line breaks), The sed -n '1p ; $p' one.txt two.txt three.txt83 command will not branch. Then, sed -n '1p ; $p' one.txt two.txt three.txt57 will print the pattern space content until the first newline, and sed -n '1p ; $p' one.txt two.txt three.txt39 will delete the pattern space content until the first new line. (To learn more about sed -i 's/hello/world/' file.txt71, sed -n '1p ; $p' one.txt two.txt three.txt57 and sed -n '1p ; $p' one.txt two.txt three.txt39 commands see ) sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt03 For more line-joining examples see 7 Some Sample ScriptsHere 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 linesThis section uses sed -i 's/hello/world/' file.txt71, sed -n '1p ; $p' one.txt two.txt three.txt39 and sed -n '1p ; $p' one.txt two.txt three.txt57 commands to process multiple lines, and the sed -n '1p ; $p' one.txt two.txt three.txt82 and sed -n '1p ; $p' one.txt two.txt three.txt83 commands for branching. See and Join specific lines (e. g. if lines 2 and 3 need to be joined) sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt04 Join backslash-continued lines sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt05 Join lines that start with whitespace (e. g SMTP headers) sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt06 7. 2 Centering LinesThis script centers all lines of a file on a 80 columns width. To change that width, the number in sed '30,35d' input.txt > output.txt39 must be replaced, and the number of added spaces also must be changed Note how the buffer commands are used to separate parts in the regular expressions to be matched—this is a common technique sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt07 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt08 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt09 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt10 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt11 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt12 7. 3 Increment a NumberThis script is one of a few that demonstrate how to do arithmetic in sed -i 's/hello/world/' file.txt22. This is indeed possible, but must be done manually To increment one number you just add 1 to last digit, replacing it by the following digit. There is one exception. when the digit is a nine the previous digits must be also incremented until you don’t have a nine This solution by Bruno Haible is very clever and smart because it uses a single buffer; if you don’t have this limitation, the algorithm used in , is faster. It works by replacing trailing nines with an underscore, then using multiple sed -n '1p ; $p' one.txt two.txt three.txt67 commands to increment the last digit, and then again substituting underscores with zeros sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt13 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt14 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt15 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt16 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt17 7. 4 Rename Files to Lower CaseThis is a pretty strange use of sed -i 's/hello/world/' file.txt22. We transform text, and transform it to be shell commands, then just feed them to shell. Don’t worry, even worse hacks are done when using sed -i 's/hello/world/' file.txt22; I have seen a script converting the output of sed '30,35d' input.txt > output.txt44 into a sed '30,35d' input.txt > output.txt45 program The main body of this is the sed -i 's/hello/world/' file.txt22 script, which remaps the name from lower to upper (or vice-versa) and even checks out if the remapped name is the same as the original name. Note how the script is parameterized using shell variables and proper quoting sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt18 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt19 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt20 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt21 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt22 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt23 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt24 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt25 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt26 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt27 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt28 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt29 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt30 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt31 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt32 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt33 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt34 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt35 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt36 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt37 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt38 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt39 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt40 7. 5 Print sed '30,35d' input.txt > output.txt 47 EnvironmentThis script strips the definition of the shell functions from the output of the sed '30,35d' input.txt > output.txt48 Bourne-shell command sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt41 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt42 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt43 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt44 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt45 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt46 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt47 7. 6 Reverse Characters of LinesThis script can be used to reverse the position of characters in lines. Kỹ thuật này di chuyển hai ký tự cùng một lúc, do đó nó nhanh hơn các triển khai trực quan hơn Note the sed '30,35d' input.txt > output.txt49 command before the definition of the label. This is often needed to reset the flag that is tested by the sed -n '1p ; $p' one.txt two.txt three.txt83 command Imaginative readers will find uses for this script. An example is reversing the output of sed '30,35d' input.txt > output.txt51 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt48 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt49 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt50 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt51 7. 7 Text search across multiple linesThis section uses sed -i 's/hello/world/' file.txt71 and sed -n '1p ; $p' one.txt two.txt three.txt39 commands to search for consecutive words spanning multiple lines. See These examples deal with finding doubled occurrences of words in a document Finding doubled words in a single line is easy using GNU $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 387 and similarly with GNU sed -i 's/hello/world/' file.txt22 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt52
When the doubled word span two lines the above regular expression will not find them as $ echo 1 | sed '\%1%s21232' 3 $ echo 1 | sed --debug '\%1%s21232' SED PROGRAM: /1/ s/1/3/ INPUT: 'STDIN' line 1 PATTERN: 1 COMMAND: /1/ s/1/3/ PATTERN: 3 END-OF-CYCLE: 387 and sed -i 's/hello/world/' file.txt22 operate line-by-line By using sed -i 's/hello/world/' file.txt71 and sed -n '1p ; $p' one.txt two.txt three.txt39 commands, sed -i 's/hello/world/' file.txt22 can apply regular expressions on multiple lines (that is, multiple lines are stored in the pattern space, and the regular expression works on it) sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt53
See the GNU sed '30,35d' input.txt > output.txt64 manual for an alternative solution using sed '30,35d' input.txt > output.txt65 and sed '30,35d' input.txt > output.txt66 at https. //gnu. org/s/coreutils/manual/html_node/Squeezing-and-deleting. html 7. 8 Line length adjustmentThis section uses sed -i 's/hello/world/' file.txt71 and sed -n '1p ; $p' one.txt two.txt three.txt39 commands to search for consecutive words spanning multiple lines, and the sed -n '1p ; $p' one.txt two.txt three.txt82 command for branching. See and This (somewhat contrived) example deal with formatting and wrapping lines of text of the following input file sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt54 The following sed program wraps lines at 40 characters sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt55 The wrapped output sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt56 7. 9 Reverse Lines of FilesThis one begins a series of totally useless (yet interesting) scripts emulating various Unix commands. This, in particular, is a sed '30,35d' input.txt > output.txt70 workalike Note that on implementations other than GNU sed -i 's/hello/world/' file.txt22 this script might easily overflow internal buffers sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt57 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt58 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt59 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt60 7. 10 Numbering LinesThis script replaces ‘cat -n’; in fact it formats its output exactly like GNU sed '30,35d' input.txt > output.txt72 does Of course this is completely useless and for two reasons. first, because somebody else did it in C, second, because the following Bourne-shell script could be used for the same purpose and would be much faster sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt61 It uses sed -i 's/hello/world/' file.txt22 to print the line number, then groups lines two by two using sed -i 's/hello/world/' file.txt71. Of course, this script does not teach as much as the one presented below The algorithm used for incrementing uses both buffers, so the line is printed as soon as possible and then discarded. The number is split so that changing digits go in a buffer and unchanged ones go in the other; the changed digits are modified in a single step (using a sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt83 command). The line number for the next line is then composed and stored in the hold space, to be used in the next iteration sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt62 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt63 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt64 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt65 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt66 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt67 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt68 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt69 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt70 7. 11 Numbering Non-blank LinesEmulating ‘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 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt62 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt72 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt73 7. 12 Counting CharactersThis 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 -n '1p ; $p' one.txt two.txt three.txt67 commands. first we convert units, removing ‘a’s from the number; then we rotate letters so that tens become ‘a’s, and so on until no more letters remain sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt62 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt75 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt76 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt77 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt78 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt79 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt80 7. 13 Counting WordsThis script is almost the same as the previous one, once each of the words on the line is converted to a single ‘a’ (in the previous script each letter was changed to an ‘a’) It is interesting that real sed '30,35d' input.txt > output.txt80 programs have optimized loops for ‘wc -c’, so they are much slower at counting words rather than characters. This script’s bottleneck, instead, is arithmetic, and hence the word-counting one is faster (it has to manage smaller numbers) Again, the common parts are not commented to show the importance of commenting sed -i 's/hello/world/' file.txt22 scripts sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt62 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt82 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt83 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt84 7. 14 Counting LinesNo strange things are done now, because sed -i 's/hello/world/' file.txt22 gives us ‘wc -l’ functionality for free. Look 7. 15 Printing the First LinesThis script is probably the simplest useful sed -i 's/hello/world/' file.txt22 script. It displays the first 10 lines of input; the number of displayed lines is right before the sed -n '1p ; $p' one.txt two.txt three.txt00 command 7. 16 Printing the Last LinesPrinting the last n lines rather than the first is more complex but indeed possible. n is encoded in the second line, before the bang character This script is similar to the sed '30,35d' input.txt > output.txt70 script in that it keeps the final output in the hold space and prints it at the end sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt62 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt86 Mainly, the scripts keeps a window of 10 lines and slides it by adding a line and deleting the oldest (the substitution command on the second line works like a sed -n '1p ; $p' one.txt two.txt three.txt39 command but does not restart the loop) The “sliding window” technique is a very powerful way to write efficient and complex sed -i 's/hello/world/' file.txt22 scripts, because commands like sed -n '1p ; $p' one.txt two.txt three.txt57 would require a lot of work if implemented manually To introduce the technique, which is fully demonstrated in the rest of this chapter and is based on the sed -i 's/hello/world/' file.txt71, sed -n '1p ; $p' one.txt two.txt three.txt57 and sed -n '1p ; $p' one.txt two.txt three.txt39 commands, here is an implementation of sed '30,35d' input.txt > output.txt92 using a simple “sliding window. ” This looks complicated but in fact the working is the same as the last script. after we have kicked in the appropriate number of lines, however, we stop using the hold space to keep inter-line state, and instead use sed -i 's/hello/world/' file.txt71 and sed -n '1p ; $p' one.txt two.txt three.txt39 to slide pattern space by one line sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt07 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt88 Note how the first, second and fourth line are inactive after the first ten lines of input. After that, all the script does is. exiting on the last line of input, appending the next input line to pattern space, and removing the first line 7. 17 Make Duplicate Lines UniqueThis is an example of the art of using the sed -i 's/hello/world/' file.txt71, sed -n '1p ; $p' one.txt two.txt three.txt57 and sed -n '1p ; $p' one.txt two.txt three.txt39 commands, probably the most difficult to master sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt89 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt90 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt91 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt92 As you can see, we maintain a 2-line window using sed -n '1p ; $p' one.txt two.txt three.txt57 and sed -n '1p ; $p' one.txt two.txt three.txt39. This technique is often used in advanced sed -i 's/hello/world/' file.txt22 scripts 7. 18 Print Duplicated Lines of InputThis script prints only duplicated lines, like ‘uniq -d’ sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt62 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt94 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt95 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt96 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt97 7. 19 Xóa tất cả các dòng trùng lặpThis script prints only unique lines, like ‘uniq -u’ sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt07 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt99 sed -i 's/hello/world/' file.txt00 sed -i 's/hello/world/' file.txt01 sed -i 's/hello/world/' file.txt02 7. 20 Squeezing Blank LinesNhư một ví dụ cuối cùng, đây là ba tập lệnh, với độ phức tạp và tốc độ tăng dần, thực hiện chức năng tương tự như 'cat -s', đó là ép các dòng trống The first leaves a blank line at the beginning and end if there are some already sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt07 sed -i 's/hello/world/' file.txt04 sed -i 's/hello/world/' file.txt05 This one is a bit more complex and removes all empty lines at the beginning. It does leave a single blank line at end if one was there sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt07 sed -i 's/hello/world/' file.txt07 sed -i 's/hello/world/' file.txt08 This removes leading and trailing blank lines. It is also the fastest. Lưu ý rằng các vòng lặp hoàn toàn được thực hiện với sed -n '1p ; $p' one.txt two.txt three.txt52 và sed -n '1p ; $p' one.txt two.txt three.txt82, mà không cần dựa vào sed -i 's/hello/world/' file.txt22 để tự động khởi động lại tập lệnh ở cuối dòng sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt62_______0_______10 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-limitationsFor those who want to write portable sed -i 's/hello/world/' file.txt22 scripts, be aware that some implementations have been known to limit line lengths (for the pattern and hold spaces) to be no more than 4000 bytes. The POSIX standard specifies that conforming sed -i 's/hello/world/' file.txt22 implementations shall support at least 8192 byte line lengths. GNU sed -i 's/hello/world/' file.txt22 has no built-in limit on line length; as long as it can sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt008 more (virtual) memory, you can feed or construct lines as long as you like However, recursion is used to handle subpatterns and indefinite repetition. This means that the available stack space may limit the size of the buffer that can be processed by certain patterns 9 Other Resources for Learning About sed -i 's/hello/world/' file.txt 22For up to date information about GNU sed -i 's/hello/world/' file.txt22 please visit https. //www. gnu. org/software/sed/ Send general questions and suggestions to sed-devel@gnu. org. Visit the mailing list archives for past discussions at https. //lists. gnu. org/archive/html/sed-devel/ The following resources provide information about sed -i 's/hello/world/' file.txt22 (both GNU sed -i 's/hello/world/' file.txt22 and other variations). Note these not maintained by GNU sed -i 's/hello/world/' file.txt22 developers 10 Reporting BugsEmail bug reports to bug-sed@gnu. org. Also, please include the output of ‘sed --version’ in the body of your report if at all possible Please do not send a bug report like this sed -i 's/hello/world/' file.txt15 If GNU sed -i 's/hello/world/' file.txt22 doesn’t configure your favorite package, take a few extra minutes to identify the specific problem and make a stand-alone test case. Unlike other programs such as C compilers, making such test cases for sed -i 's/hello/world/' file.txt22 is quite simple A stand-alone test case includes all the data necessary to perform the test, and the specific invocation of sed -i 's/hello/world/' file.txt22 that causes the problem. The smaller a stand-alone test case is, the better. A test case should not involve something as far removed from sed -i 's/hello/world/' file.txt22 as “try to configure frobme-1. 3. 4”. Yes, that is in principle enough information to look for the bug, but that is not a very practical prospect Here are a few commonly reported bugs that are not bugs sed -i 's/hello/world/' file.txt71 command on the last line Most versions of sed -i 's/hello/world/' file.txt22 exit without printing anything when the sed -i 's/hello/world/' file.txt71 command is issued on the last line of a file. GNU sed -i 's/hello/world/' file.txt22 prints pattern space before exiting unless of course the sed -i 's/hello/world/' file.txt45 command switch has been specified. This choice is by design Default behavior (gnu extension, non-POSIX conforming) To force POSIX-conforming behavior sed -i 's/hello/world/' file.txt16 For example, the behavior of would depend on whether foo has an even or an odd number of lines. 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 instead of just sed -i 's/hello/world/' file.txt18 In any case, the simplest workaround is to use sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt024 in scripts that rely on the traditional behavior, or to set the sed -i 's/hello/world/' file.txt72 variable to a non-empty valueRegex syntax clashes (problems with backslashes) sed -i 's/hello/world/' file.txt22 uses the POSIX basic regular expression syntax. According to the standard, the meaning of some escape sequences is undefined in this syntax; notable in the case of sed -i 's/hello/world/' file.txt22 are # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE11, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE23, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE25, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt56, sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt57, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE91, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE92, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE87, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE88, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE85, and # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE86 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 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt040 matches one or more occurrences of ‘x’. sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt041 matches either ‘abc’ or ‘def’ This syntax may cause problems when running scripts written for other sed -i 's/hello/world/' file.txt22s. Some sed -i 's/hello/world/' file.txt22 programs have been written with the assumption that # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE11 and # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE23 match the literal characters sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt046 and sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt047. 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 Mặt khác, một số tập lệnh sử dụng s. abc\. def. g to remove occurrences of either sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt050 or sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt051. While this worked until sed -i 's/hello/world/' file.txt22 4. 0. x, newer versions interpret this as removing the string sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt053. This is again undefined behavior according to POSIX, and this interpretation is arguably more robust. older sed -i 's/hello/world/' file.txt22s, for example, required that the regex matcher parsed sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt055 as sed -n '1p ; $p' one.txt two.txt three.txt94 in the common case of escaping a slash, which is again undefined behavior; the new behavior avoids this, and this is good because the regex matcher is only partially under our control In addition, this version of sed -i 's/hello/world/' file.txt22 supports several escape characters (some of which are multi-character) to insert non-printable characters in scripts ( $ echo | sed 'Q42' ; echo $? 4202, sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt059, sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt060, sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt061, $ echo | sed 'Q42' ; echo $? 4205, # WRONG USAGE: 'FILE' will be truncated. sed -ni 's/foo/bar/' FILE53, $ echo | sed 'Q42' ; echo $? 4207, sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt065). These can cause similar problems with scripts written for other sed -i 's/hello/world/' file.txt22s-i clobbers read-only files In short, ‘sed -i’ will let you delete the contents of a read-only file, and in general the -i option (see ) lets you clobber protected files. This is not a bug, but rather a consequence of how the Unix file system works The permissions on a file say what can happen to the data in that file, while the permissions on a directory say what can happen to the list of files in that directory. ‘sed -i’ will not ever open for writing a file that is already on disk. Rather, it will work on a temporary file that is finally renamed to the original name. if you rename or delete files, you’re actually modifying the contents of the directory, so the operation depends on the permissions of the directory, not of the file. For this same reason, sed -i 's/hello/world/' file.txt22 does not let you use -i on a writable file in a read-only directory, and will break hard or symbolic links when -i is used on such a file sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt068 does not work (gives an error) There is no line 0. 0 is a special address that is only used to treat addresses like sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt069 as active when the script starts. if you write sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt070 and the first line includes the word ‘abc’, then that match would be ignored because address ranges must span at least two lines (barring the end of the file); but what you probably wanted is to delete every line up to the first one including ‘abc’, and this is obtained with sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt071 sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt072 is case insensitive You are encountering problems with locales. POSIX mandates that sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt072 uses the current locale’s collation order – in C parlance, that means using sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt074 instead of sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt075. Some locales have a case-insensitive collation order, others don’t Another problem is that sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt072 tries to use collation symbols. This only happens if you are on the GNU system, using GNU libc’s regular expression matcher instead of compiling the one supplied with GNU sed. In a Danish locale, for example, the regular expression sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt077 matches the string ‘aa’, because this is a single collating symbol that comes after ‘a’ and before ‘b’; ‘ll’ behaves similarly in Spanish locales, or ‘ij’ in Dutch locales To work around these problems, which may cause bugs in shell scripts, set the sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt078 and $ echo | sed 'Q42' ; echo $? 4246 environment variables to ‘C’ $ echo | sed 'Q42' ; echo $? 4226 does not clear pattern space This happens if your input stream includes invalid multibyte sequences. POSIX mandates that such sequences are not matched by ‘. ’, so that ‘s/. *//’ will not clear pattern space as you would expect. In fact, there is no way to clear sed’s buffers in the middle of the script in most multibyte locales (including UTF-8 locales). For this reason, GNU sed -i 's/hello/world/' file.txt22 provides a ‘z’ command (for ‘zap’) as an extension To work around these problems, which may cause bugs in shell scripts, set the sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt078 and $ echo | sed 'Q42' ; echo $? 4246 environment variables to ‘C’ Appendix A GNU Free Documentation LicenseVersion 1. 3, 3 November 2008 sed -i 's/hello/world/' file.txt19
PHỤ LỤC. How to use this License for your documentsTo 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 Nếu bạn có Phần bất biến không có Văn bản bìa hoặc một số kết hợp khác của cả ba, hãy hợp nhất hai lựa chọn thay thế đó cho phù hợp với tình huống Nếu tài liệu của bạn chứa các ví dụ không tầm thường về mã chương trình, chúng tôi khuyên bạn nên phát hành song song các ví dụ này theo lựa chọn giấy phép phần mềm tự do của bạn, chẳng hạn như Giấy phép Công cộng GNU, để cho phép sử dụng chúng trong phần mềm miễn phí Chỉ mục khái niệmĐây là chỉ mục chung về tất cả các vấn đề được thảo luận trong sách hướng dẫn này, ngoại trừ các lệnh sed -i 's/hello/world/' file.txt22 và các tùy chọn dòng lệnh |