Python regex bất kỳ chữ cái nào
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. Trình chỉnh sửa luồng được sử dụng để thực hiện các chuyển đổi văn bản cơ bản trên luồng đầu vào (tệp hoặc đầu vào từ đường dẫn). Mặc dù theo một số cách tương tự như một trình soạn thảo cho phép chỉnh sửa theo kịch bản (chẳng hạn như sed -i 's/hello/world/' file.txt24), sed -i 's/hello/world/' file.txt22 hoạt động bằng cách chỉ thực hiện một lần chuyển qua (các) đầu vào và do đó hiệu quả hơn. Nhưng khả năng lọc văn bản theo đường dẫn của sed -i 's/hello/world/' file.txt22 đặc biệt phân biệt nó với các loại trình chỉnh sửa khác 2 Chạy sedChương này trình bày cách chạy sed -i 's/hello/world/' file.txt22. Chi tiết về các tập lệnh sed -i 's/hello/world/' file.txt22 và các lệnh sed -i 's/hello/world/' file.txt22 riêng lẻ sẽ được thảo luận trong chương tiếp theo 2. 1. Tổng quanThông thường sed -i 's/hello/world/' file.txt22 được gọi như thế này Ví dụ: để thay thế tất cả các lần xuất hiện của 'xin chào' thành 'thế giới' trong đầu vào tệp. txt ________số 8 Nếu bạn không chỉ định INPUTFILE hoặc nếu INPUTFILE là -, thì sed -i 's/hello/world/' file.txt22 sẽ lọc nội dung của đầu vào tiêu chuẩn. Các lệnh sau đây là tương đương sed -i 's/hello/world/' file.txt0 sed -i 's/hello/world/' file.txt22 ghi đầu ra thành đầu ra tiêu chuẩn. Sử dụng -i để chỉnh sửa tệp tại chỗ thay vì in thành đầu ra tiêu chuẩn. Xem thêm các lệnh sed -i 's/hello/world/' file.txt33 và sed -i 's/hello/world/' file.txt34 để ghi đầu ra vào các tệp khác. Lệnh sau sửa đổi tệp. txt và không tạo ra bất kỳ đầu ra nào sed -i 's/hello/world/' file.txt Theo mặc định, sed -i 's/hello/world/' file.txt22 in tất cả đầu vào đã xử lý (ngoại trừ đầu vào đã bị sửa đổi/xóa bởi các lệnh như sed -i 's/hello/world/' file.txt36). Sử dụng -n để chặn đầu ra và lệnh sed -i 's/hello/world/' file.txt37 để in các dòng cụ thể. Lệnh sau chỉ in dòng 45 của tệp đầu vào sed -i 's/hello/world/' file.txt22 coi nhiều tệp đầu vào là một luồng dài. Ví dụ sau in dòng đầu tiên của tệp đầu tiên (một. txt) và dòng cuối cùng của tệp cuối cùng (ba. txt). Sử dụng -s để đảo ngược hành vi này sed -i 's/hello/world/' file.txt9 Không có tùy chọn -e hoặc -f, sed -i 's/hello/world/' file.txt22 sử dụng tham số không phải tùy chọn đầu tiên làm tập lệnh và các tham số không phải tùy chọn sau làm tệp đầu vào. Nếu các tùy chọn -e hoặc -f được sử dụng để chỉ định tập lệnh, tất cả các tham số không phải tùy chọn sẽ được lấy làm tệp đầu vào. Các tùy chọn -e và -f có thể được kết hợp và có thể xuất hiện nhiều lần (trong trường hợp đó, tập lệnh hiệu quả cuối cùng sẽ là tập lệnh nối của tất cả các tập lệnh riêng lẻ) Các ví dụ sau là tương đương sed -i 's/hello/world/' file.txt1 2. 2 tùy chọn dòng lệnhĐịnh dạng đầy đủ để gọi sed -i 's/hello/world/' file.txt22 là sed -i 's/hello/world/' file.txt3 sed -i 's/hello/world/' file.txt22 có thể được gọi với các tùy chọn dòng lệnh sau sed -i 's/hello/world/' file.txt42 In ra phiên bản sed -i 's/hello/world/' file.txt22 đang chạy và thông báo bản quyền, sau đó thoát ra sed -i 's/hello/world/' file.txt44 In thông báo sử dụng tóm tắt ngắn gọn các tùy chọn dòng lệnh này và địa chỉ báo cáo lỗi, sau đó thoát _______045____046____047Theo mặc định, sed -i 's/hello/world/' file.txt22 in ra không gian mẫu ở cuối mỗi chu kỳ thông qua tập lệnh (xem Cách thức hoạt động của sed -i 's/hello/world/' file.txt22). Các tùy chọn này vô hiệu hóa tính năng in tự động này và sed -i 's/hello/world/' file.txt22 chỉ tạo đầu ra khi được thông báo rõ ràng thông qua lệnh sed -i 's/hello/world/' file.txt37 sed -i 's/hello/world/' file.txt52 In chương trình sed đầu vào ở dạng chuẩn và chú thích việc thực thi chương trình sed -i 's/hello/world/' file.txt6 sed -i 's/hello/world/' file.txt53 sed -i 's/hello/world/' file.txt54 Thêm các lệnh trong tập lệnh vào tập hợp các lệnh sẽ được chạy trong khi xử lý đầu vào sed -i 's/hello/world/' file.txt55 sed -i 's/hello/world/' file.txt56 Thêm các lệnh có trong tệp script-file vào tập hợp các lệnh sẽ được chạy trong khi xử lý đầu vào sed -i 's/hello/world/' file.txt57 sed -i 's/hello/world/' file.txt58 Tùy chọn này chỉ định rằng các tệp sẽ được chỉnh sửa tại chỗ. GNU sed -i 's/hello/world/' file.txt22 thực hiện điều này bằng cách tạo một tệp tạm thời và gửi đầu ra tới tệp này thay vì tới đầu ra tiêu chuẩn. 1 Tùy chọn này ngụ ý -s Khi đến cuối tệp, tệp tạm thời được đổi tên thành tên gốc của tệp đầu ra. Phần mở rộng, nếu được cung cấp, được sử dụng để sửa đổi tên của tệp cũ trước khi đổi tên tệp tạm thời, do đó tạo bản sao lưu2) Quy tắc này được tuân theo. nếu phần mở rộng không chứa sed -i 's/hello/world/' file.txt60, thì phần mở rộng đó sẽ được thêm vào cuối tên tệp hiện tại dưới dạng hậu tố; . Điều này cho phép bạn thêm tiền tố vào tệp sao lưu, thay vì (hoặc thêm vào) hậu tố hoặc thậm chí đặt các bản sao lưu của tệp gốc vào một thư mục khác (miễn là thư mục đó đã tồn tại) 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 Tương tự như -E -i không có hậu tố sao lưu - FILE sẽ được chỉnh sửa tại chỗ mà không cần tạo bản sao lưu sed -i 's/hello/world/' file.txt63 This is equivalent to --in-place=E, creating FILEE as backup of FILE Be cautious of using -n with -i. cái trước tắt tự động in các dòng và cái sau thay đổi tệp tại chỗ mà không cần sao lưu. Được sử dụng bất cẩn (và không có lệnh sed -i 's/hello/world/' file.txt37 rõ ràng), tệp đầu ra sẽ trống sed -i 's/hello/world/' file.txt9 sed -i 's/hello/world/' file.txt65 sed -i 's/hello/world/' file.txt66 Specify the default line-wrap length for the sed -i 's/hello/world/' file.txt67 command. Độ dài bằng 0 (không) có nghĩa là không bao giờ ngắt dòng dài. Nếu không được chỉ định, nó được lấy là 70 sed -i 's/hello/world/' file.txt68 GNU sed -i 's/hello/world/' file.txt22 bao gồm một số tiện ích mở rộng cho POSIX sed. In order to simplify writing portable scripts, this option disables all the extensions that this manual documents, including additional commands. Hầu hết các tiện ích mở rộng đều chấp nhận các chương trình sed -i 's/hello/world/' file.txt22 nằm ngoài cú pháp do POSIX quy định, nhưng một số trong số chúng (chẳng hạn như hành vi của lệnh sed -i 's/hello/world/' file.txt71 được mô tả trong Báo cáo lỗi) thực sự vi phạm tiêu chuẩn. 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 Tùy chọn này khả dụng trên mọi nền tảng, nhưng chỉ hiệu quả khi hệ điều hành phân biệt giữa tệp văn bản và tệp nhị phân. 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. Khi tùy chọn này được chỉ định, sed -i 's/hello/world/' file.txt22 sẽ mở các tệp đầu vào ở chế độ nhị phân, do đó không yêu cầu xử lý đặc biệt này và xem xét các dòng kết thúc tại một nguồn cấp dữ liệu sed -i 's/hello/world/' file.txt77 Tùy chọn này chỉ khả dụng trên các nền tảng hỗ trợ liên kết tượng trưng và chỉ có tác dụng nếu tùy chọn -i được chỉ định. Trong trường hợp này, nếu tệp được chỉ định trên dòng lệnh là một liên kết tượng trưng, thì sed -i 's/hello/world/' file.txt22 sẽ theo liên kết và chỉnh sửa đích cuối cùng của liên kết. Hành vi mặc định là ngắt liên kết tượng trưng để đích liên kết sẽ không bị sửa đổi sed -i 's/hello/world/' file.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. Về mặt lịch sử, đây là một phần mở rộng GNU, nhưng phần mở rộng -E đã được thêm vào tiêu chuẩn POSIX (http. //austingroupbugs. lượt xem trên mạng. php?id=528), vì vậy hãy sử dụng -E cho tính di động. GNU sed đã chấp nhận -E như một tùy chọn không có giấy tờ trong nhiều năm và *BSD seds cũng đã chấp nhận -E trong nhiều năm, nhưng các tập lệnh sử dụng -E có thể không chuyển sang các hệ thống cũ hơn khác. Xem Biểu thức chính quy mở rộng sed -i 's/hello/world/' file.txt83 sed -i 's/hello/world/' file.txt84 Theo mặc định, sed -i 's/hello/world/' file.txt22 sẽ coi các tệp được chỉ định trên dòng lệnh là một luồng dài liên tục duy nhất. Tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22 này cho phép người dùng coi chúng là các tệp riêng biệt. 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 có 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, các lệnh sed -i 's/hello/world/' file.txt90 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 sed -i 's/hello/world/' file.txt93 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. ) ________ 094 ________ 095 ________ 096Coi đầ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. The standard input will be processed if no file names are specified 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 -i 's/hello/world/' file.txt900 và sed -i 's/hello/world/' file.txt901 có thể được sử dụng để chấm dứt sed -i 's/hello/world/' file.txt22 với giá trị mã thoát tùy chỉnh (đây là tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22) sed 's/hello/world/' input.txt > output.txt9 3 sed -i 's/hello/world/' file.txt 22 scripts3. 1 sed -i 's/hello/world/' file.txt 22 script overviewMột chương trình sed -i 's/hello/world/' file.txt22 bao gồm một hoặc nhiều lệnh sed -i 's/hello/world/' file.txt22, được truyền vào bởi một hoặc nhiều tùy chọn -e, -f, --expression và --file hoặc đối số không phải tùy chọn đầu tiên nếu không sử dụng các tùy chọn này. Tài liệu này sẽ đề cập đến “tập lệnh” sed -i 's/hello/world/' file.txt22; . See Overview sed -i 's/hello/world/' file.txt22 lệnh theo cú pháp này X là lệnh một ký tự sed -i 's/hello/world/' file.txt22. sed -i 's/hello/world/' file.txt911 là một địa chỉ dòng tùy chọn. If sed -i 's/hello/world/' file.txt911 is specified, the command X will be executed only on the matched lines. sed -i 's/hello/world/' file.txt911 có thể là số dòng đơn, biểu thức chính quy hoặc dải dòng (xem địa chỉ sed). sed -i 's/hello/world/' file.txt914 bổ sung được sử dụng cho một số lệnh sed -i 's/hello/world/' file.txt22 The following example deletes lines 30 to 35 in the input. sed -i 's/hello/world/' file.txt916 là một dải địa chỉ. sed -i 's/hello/world/' file.txt36 là lệnh xóa sed -i 's/hello/world/' file.txt04 The following example prints all input until a line starting with the word ‘foo’ is found. If such line is found, sed -i 's/hello/world/' file.txt22 will terminate with exit status 42. If such line was not found (and no other error occurred), sed -i 's/hello/world/' file.txt22 will exit with status 0. sed -i 's/hello/world/' file.txt920 is a regular-expression address. sed -i 's/hello/world/' file.txt900 is the quit command. sed -i 's/hello/world/' file.txt922 is the command option sed -i 's/hello/world/' file.txt00 Commands within a script or script-file can be separated by semicolons ( sed -i 's/hello/world/' file.txt923) or newlines (ASCII 10). Multiple scripts can be specified with -e or -f options The following examples are all equivalent. They perform two sed -i 's/hello/world/' file.txt22 operations. deleting any lines matching the regular expression sed -i 's/hello/world/' file.txt920, and replacing all occurrences of the string ‘hello’ with ‘world’ sed -i 's/hello/world/' file.txt01 Commands sed -i 's/hello/world/' file.txt926, sed -i 's/hello/world/' file.txt927, sed -i 's/hello/world/' file.txt928, due to their syntax, cannot be followed by semicolons working as command separators and thus should be terminated with newlines or be placed at the end of a script or script-file. Commands can also be preceded with optional non-significant whitespace characters. See Multiple commands syntax 3. 2 sed -i 's/hello/world/' file.txt 22 commands summaryThe following commands are supported in GNU sed -i 's/hello/world/' file.txt22. Some are standard POSIX commands, while other are GNU extensions. Details and examples for each command are in the following sections. (Mnemonics) are shown in parentheses sed -i 's/hello/world/' file.txt931 sed -i 's/hello/world/' file.txt932 Append text after a line sed -i 's/hello/world/' file.txt933 Append text after a line (alternative syntax) sed -i 's/hello/world/' file.txt934 Branch unconditionally to label. The label may be omitted, in which case the next cycle is started sed -i 's/hello/world/' file.txt935 sed -i 's/hello/world/' file.txt932 Replace (change) lines with text sed -i 's/hello/world/' file.txt937 Replace (change) lines with text (alternative syntax) sed -i 's/hello/world/' file.txt36 Delete the pattern space; immediately start next cycle sed -i 's/hello/world/' file.txt939 If pattern space contains newlines, delete text in the pattern space up to the first newline, and restart cycle with the resultant pattern space, without reading a new line of input If pattern space contains no newline, start a normal new cycle as if the sed -i 's/hello/world/' file.txt36 command was issued sed -i 's/hello/world/' file.txt941 Executes the command that is found in pattern space and replaces the pattern space with the output; a trailing newline is suppressed sed -i 's/hello/world/' file.txt942 Executes command and sends its output to the output stream. The command can run across multiple lines, all but the last ending with a back-slash sed -i 's/hello/world/' file.txt943 (filename) Print the file name of the current input file (with a trailing newline) sed -i 's/hello/world/' file.txt944 Replace the contents of the pattern space with the contents of the hold space sed -i 's/hello/world/' file.txt945 Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space sed -i 's/hello/world/' file.txt946 (hold) Replace the contents of the hold space with the contents of the pattern space sed -i 's/hello/world/' file.txt947 Append a newline to the contents of the hold space, and then append the contents of the pattern space to that of the hold space sed -i 's/hello/world/' file.txt948 sed -i 's/hello/world/' file.txt932 insert text before a line sed -i 's/hello/world/' file.txt950 insert text before a line (alternative syntax) sed -i 's/hello/world/' file.txt67 Print the pattern space in an unambiguous form sed -i 's/hello/world/' file.txt952 (next) If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then sed -i 's/hello/world/' file.txt22 exits without processing any more commands sed -i 's/hello/world/' file.txt71 Add a newline to the pattern space, then append the next line of input to the pattern space. If there is no more input then sed -i 's/hello/world/' file.txt22 exits without processing any more commands sed -i 's/hello/world/' file.txt37 Print the pattern space sed -i 's/hello/world/' file.txt957 Print the pattern space, up to the first sed -i 's/hello/world/' file.txt958 (quit) Exit sed -i 's/hello/world/' file.txt22 without processing any more commands or input sed -i 's/hello/world/' file.txt960 (quit) This command is the same as sed -i 's/hello/world/' file.txt900, but will not print the contents of pattern space. Like sed -i 's/hello/world/' file.txt900, it provides the ability to return an exit code to the caller sed -i 's/hello/world/' file.txt963 Reads file filename sed -i 's/hello/world/' file.txt964 Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read sed -i 's/hello/world/' file.txt965 (thay thế) Khớp biểu thức chính quy với nội dung của không gian mẫu. If found, replace matched string with replacement sed -i 's/hello/world/' file.txt966 (test) Branch to label only if there has been a successful sed -i 's/hello/world/' file.txt967ubstitution since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started sed -i 's/hello/world/' file.txt968 (test) Branch to label only if there have been no successful sed -i 's/hello/world/' file.txt967ubstitutions since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started sed -i 's/hello/world/' file.txt970 (version) This command does nothing, but makes sed -i 's/hello/world/' file.txt22 fail if GNU sed -i 's/hello/world/' file.txt22 extensions are not supported, or if the requested version is not available sed -i 's/hello/world/' file.txt973 Write the pattern space to filename sed -i 's/hello/world/' file.txt974 Write to the given filename the portion of the pattern space up to the first newline sed -i 's/hello/world/' file.txt975 Trao đổi nội dung của không gian giữ và mẫu sed -i 's/hello/world/' file.txt976 Transliterate any characters in the pattern space which match any of the source-chars with the corresponding character in dest-chars sed -i 's/hello/world/' file.txt977 (zap) This command empties the content of pattern space sed -i 's/hello/world/' file.txt978 A comment, until the next newline sed -i 's/hello/world/' file.txt979 Group several commands together sed -i 's/hello/world/' file.txt980 Print the current input line number (with a trailing newline) sed -i 's/hello/world/' file.txt981 Specify the location of label for branch commands ( sed -i 's/hello/world/' file.txt982, sed -i 's/hello/world/' file.txt983, sed -i 's/hello/world/' file.txt984) 3. 3 The sed -i 's/hello/world/' file.txt 967 CommandThe sed -i 's/hello/world/' file.txt967 command (as in substitute) is probably the most important in sed -i 's/hello/world/' file.txt22 and has a lot of different options. The syntax of the sed -i 's/hello/world/' file.txt967 command is ‘s/regexp/replacement/flags’ Its basic concept is simple. the sed -i 's/hello/world/' file.txt967 command attempts to match the pattern space against the supplied regular expression regexp; if the match is successful, then that portion of the pattern space which was matched is replaced with replacement For details about regexp syntax see Regular Expression Addresses The replacement can contain sed -i 's/hello/world/' file.txt990 (n being a number from 1 to 9, inclusive) references, which refer to the portion of the match which is contained between the nth sed -i 's/hello/world/' file.txt991 and its matching sed -i 's/hello/world/' file.txt992. Also, the replacement can contain unescaped sed -i 's/hello/world/' file.txt993 characters which reference the whole matched portion of the pattern space The sed -i 's/hello/world/' file.txt994 characters may be uniformly replaced by any other single character within any given sed -i 's/hello/world/' file.txt967 command. The sed -i 's/hello/world/' file.txt994 character (or whatever other character is used in its stead) can appear in the regexp or replacement only if it is preceded by a sed -i 's/hello/world/' file.txt997 character Finally, as a GNU sed -i 's/hello/world/' file.txt22 extension, you can include a special sequence made of a backslash and one of the letters sed -i 's/hello/world/' file.txt999, sed -i 's/hello/world/' file.txt67, sed -i 's/hello/world/' file.txt101, sed -i 's/hello/world/' file.txt102, or sed -i 's/hello/world/' file.txt103. The meaning is as follows sed -i 's/hello/world/' file.txt104 Turn the replacement to lowercase until a sed -i 's/hello/world/' file.txt105 or sed -i 's/hello/world/' file.txt106 is found, sed -i 's/hello/world/' file.txt107 Biến ký tự tiếp theo thành chữ thường, sed -i 's/hello/world/' file.txt105 Chuyển từ thay thế sang chữ hoa cho đến khi tìm thấy sed -i 's/hello/world/' file.txt104 hoặc sed -i 's/hello/world/' file.txt106, sed -i 's/hello/world/' file.txt111 Chuyển ký tự tiếp theo thành chữ hoa, sed -i 's/hello/world/' file.txt106 Dừng chuyển đổi trường hợp bắt đầu bởi sed -i 's/hello/world/' file.txt104 hoặc sed -i 's/hello/world/' file.txt105 Khi cờ sed -i 's/hello/world/' file.txt944 đang được sử dụng, chuyển đổi trường hợp không lan truyền từ một lần xuất hiện của biểu thức chính quy sang một lần khác. Ví dụ: khi lệnh sau được thực thi với 'a-b-' trong không gian mẫu đầu ra là 'axxB'. Khi thay thế '-' đầu tiên, chuỗi '\ u' chỉ ảnh hưởng đến thay thế trống của '\ 1'. Nó không ảnh hưởng đến ký tự sed -i 's/hello/world/' file.txt975 được thêm vào không gian mẫu khi thay thế sed -i 's/hello/world/' file.txt117 bằng sed -i 's/hello/world/' file.txt118 Mặt khác, sed -i 's/hello/world/' file.txt107 và sed -i 's/hello/world/' file.txt111 ảnh hưởng đến phần còn lại của văn bản thay thế nếu theo sau chúng là một thay thế trống. Với 'a-b-' trong không gian mẫu, lệnh sau sẽ thay thế '-' bằng 'X' (chữ hoa) và 'b-' bằng 'Bx'. Nếu hành vi này là không mong muốn, bạn có thể ngăn chặn nó bằng cách thêm một chuỗi '\E'—sau '\1' trong trường hợp này Để bao gồm một sed -i 's/hello/world/' file.txt997, sed -i 's/hello/world/' file.txt993 hoặc dòng mới trong phần thay thế cuối cùng, hãy đảm bảo đặt trước sed -i 's/hello/world/' file.txt997, sed -i 's/hello/world/' file.txt993 hoặc dòng mới mong muốn trong phần thay thế bằng một sed -i 's/hello/world/' file.txt997 Lệnh sed -i 's/hello/world/' file.txt967 có thể được theo sau bởi 0 hoặc nhiều cờ sau sed -i 's/hello/world/' file.txt944 Áp dụng thay thế cho tất cả các kết quả phù hợp với biểu thức chính quy, không chỉ lần đầu tiên sed -i 's/hello/world/' file.txt128 Chỉ thay thế trận đấu số của regrec tương tác trong lệnh sed -i 's/hello/world/' file.txt967 Lưu ý. tiêu chuẩn POSIX không chỉ định điều gì sẽ xảy ra khi bạn kết hợp công cụ sửa đổi số và số sed -i 's/hello/world/' file.txt944 và hiện tại không có ý nghĩa thống nhất rộng rãi trên toàn bộ 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 xác định 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 -i 's/hello/world/' file.txt941 được chỉ định, thứ tự tương đối của hai tùy chọn tạo ra kết quả rất khác nhau. Nói chung, sed -i 's/hello/world/' file.txt136 (đá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 đặ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 -i 's/hello/world/' file.txt941, 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 -i 's/hello/world/' file.txt967 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 -i 's/hello/world/' file.txt973 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. 3 sed -i 's/hello/world/' file.txt941 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 -i 's/hello/world/' file.txt145 sed -i 's/hello/world/' file.txt928 Công cụ sửa đổi sed -i 's/hello/world/' file.txt145 để so khớp biểu thức chính quy là một tiện ích mở rộng GNU làm cho sed -i 's/hello/world/' file.txt22 khớp với biểu thức chính quy theo cách không phân biệt chữ hoa chữ thường sed -i 's/hello/world/' file.txt149 sed -i 's/hello/world/' file.txt150 Công cụ sửa đổi sed -i 's/hello/world/' file.txt149 để 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 -i 's/hello/world/' file.txt154 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 -i 's/hello/world/' file.txt156 và sed -i 's/hello/world/' file.txt157) 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 -i 's/hello/world/' file.txt978 [Không cho phép địa chỉ. ] Nhân vật sed -i 's/hello/world/' file.txt978 bắt đầu bình luận; 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 -i 's/hello/world/' file.txt978 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 -i 's/hello/world/' file.txt164, 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 -i 's/hello/world/' file.txt165 Thoát sed -i 's/hello/world/' file.txt22 mà không cần xử lý thêm bất kỳ lệnh hoặc đầu vào nào Ví 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à phần 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 -i 's/hello/world/' file.txt901 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 Delete the pattern space; immediately start next cycle Ví 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 Ví dụ. chỉ in dòng đầu vào thứ hai sed -i 's/hello/world/' file.txt952 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 đầu và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) Ví dụ. thực hiện thay thế trên mỗi dòng thứ 3 (i. e. hai lệnh sed -i 's/hello/world/' file.txt952 bỏ qua hai dòng) sed -i 's/hello/world/' file.txt02 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 -i 's/hello/world/' file.txt03 sed -i 's/hello/world/' file.txt177 Một nhóm lệnh có thể được đặt giữa các ký tự sed -i 's/hello/world/' file.txt178 và sed -i 's/hello/world/' file.txt179. Đ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 Ví dụ. thực hiện thay thế sau đó in dòng đầu vào thứ hai sed -i 's/hello/world/' file.txt04 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 tập 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 -i 's/hello/world/' file.txt181 Transliterate any characters in the pattern space which match any of the source-chars with the corresponding character in dest-chars Ví dụ. phiên âm 'a-j' thành '0-9' sed -i 's/hello/world/' file.txt05 (Các ký tự sed -i 's/hello/world/' file.txt994 có thể được thay thế thống nhất bằng bất kỳ ký tự đơn nào khác trong bất kỳ lệnh sed -i 's/hello/world/' file.txt183 đã cho nào. ) Các trường hợp của sed -i 's/hello/world/' file.txt994 (hoặc bất kỳ ký tự nào khác được sử dụng thay cho nó), sed -i 's/hello/world/' file.txt997 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 sed -i 's/hello/world/' file.txt997. 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 -i 's/hello/world/' file.txt187 từ GNU coreutils để biết chức năng tương tự sed -i 's/hello/world/' file.txt933 Nối văn bản sau một dòng. Đây là phần mở rộng GNU cho lệnh sed -i 's/hello/world/' file.txt926 tiêu chuẩn - xem bên dưới để biết chi tiết Ví dụ. Thêm từ 'xin chào' sau dòng thứ hai sed -i 's/hello/world/' file.txt06 Khoảng trắng hàng đầu sau lệnh sed -i 's/hello/world/' file.txt926 bị bỏ qua. Văn bản cần thêm được đọc cho đến cuối dòng sed -i 's/hello/world/' file.txt931 sed -i 's/hello/world/' file.txt932 Nối văn bản sau một dòng Ví dụ. Thêm 'xin chào' sau dòng thứ hai (-. cho biết các dòng đầu ra được in) sed -i 's/hello/world/' file.txt07 Lệnh sed -i 's/hello/world/' file.txt926 xếp hàng đợi các dòng văn bản tuân theo lệnh này (mỗi dòng nhưng kết thúc cuối cùng bằng sed -i 's/hello/world/' file.txt997, đượ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ỉ Escape sequences in text are processed, so you should use sed -i 's/hello/world/' file.txt195 in text to print a single backslash 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 -i 's/hello/world/' file.txt997) - ‘world’ trong ví dụ sau sed -i 's/hello/world/' file.txt08 Là một tiện ích mở rộng GNU, lệnh và văn bản sed -i 's/hello/world/' file.txt926 có thể được tách thành hai tham số sed -i 's/hello/world/' file.txt198, cho phép tạo tập lệnh dễ dàng hơn sed -i 's/hello/world/' file.txt09 sed -i 's/hello/world/' file.txt950 chèn văn bản trước một dòng. Đây là phần mở rộng GNU cho lệnh sed -i 's/hello/world/' file.txt928 tiêu chuẩn - xem bên dưới để biết chi tiết Ví dụ. Chèn từ 'xin chào' trước dòng thứ hai sed -i 's/hello/world/' file.txt0 Leading whitespace after the sed -i 's/hello/world/' file.txt928 command is ignored. The text to add is read until the end of the line sed -i 's/hello/world/' file.txt948 sed -i 's/hello/world/' file.txt932 Immediately output the lines of text which follow this command Example. 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ỉ Escape sequences in text are processed, so you should use sed -i 's/hello/world/' file.txt195 in text to print a single backslash 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 -i 's/hello/world/' file.txt997) - ‘world’ trong ví dụ sau sed -i 's/hello/world/' file.txt2 Là một tiện ích mở rộng GNU, lệnh và văn bản sed -i 's/hello/world/' file.txt928 có thể được tách thành hai tham số sed -i 's/hello/world/' file.txt198, cho phép tạo tập lệnh dễ dàng hơn sed -i 's/hello/world/' file.txt3 sed -i 's/hello/world/' file.txt937 Replaces the line(s) with text. Đây là phần mở rộng GNU cho lệnh sed -i 's/hello/world/' file.txt927 tiêu chuẩn - xem bên dưới để biết chi tiết Ví dụ. Replace the 2nd to 9th lines with the word ‘hello’ sed -i 's/hello/world/' file.txt4 Leading whitespace after the sed -i 's/hello/world/' file.txt927 command is ignored. Văn bản cần thêm được đọc cho đến cuối dòng sed -i 's/hello/world/' file.txt935 sed -i 's/hello/world/' file.txt932 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 Example. 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 If no addresses are given, each line is replaced Một chu kỳ mới được bắt đầu sau khi lệnh này được thực hiện, vì không gian mẫu sẽ bị xóa. Trong ví dụ sau, sed -i 's/hello/world/' file.txt927 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 As a GNU extension, the sed -i 's/hello/world/' file.txt927 command and text can be separated into two sed -i 's/hello/world/' file.txt198 parameters, enabling easier scripting sed -i 's/hello/world/' file.txt7 sed -i 's/hello/world/' file.txt980 Print out the current input line number (with a trailing newline) sed -i 's/hello/world/' file.txt8 Là một phần mở rộng GNU, lệnh này chấp nhận hai địa chỉ sed -i 's/hello/world/' file.txt317 In không gian mẫu ở dạng rõ ràng. các ký tự không in được (và ký tự sed -i 's/hello/world/' file.txt997) được in ở dạng thoát kiểu C; n specifies the desired line-wrap length; a length of 0 (zero) means to never wrap long lines. 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 -i 's/hello/world/' file.txt963 Reads file filename. Ví 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. Note that if filename cannot be read, it is treated as if it were an empty file, without any error indication 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 -i 's/hello/world/' file.txt973 Viết không gian mẫu vào tên tệp. As a GNU sed -i 's/hello/world/' file.txt22 extension, two special values of filename are supported. /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. 4 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 -i 's/hello/world/' file.txt939 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 Add a newline to the pattern space, then append the next line of input to the pattern space. If there is no more input then sed -i 's/hello/world/' file.txt22 exits without processing any more commands 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. Xem lệnh N trên dòng cuối cùng sed -i 's/hello/world/' file.txt957 In ra phần không gian mẫu cho đến dòng mới đầu tiên sed -i 's/hello/world/' file.txt946 Thay thế nội dung của không gian giữ bằng nội dung của không gian mẫu sed -i 's/hello/world/' file.txt947 Append a newline to the contents of the hold space, and then append the contents of the pattern space to that of the hold space sed -i 's/hello/world/' file.txt944 Replace the contents of the pattern space with the contents of the hold space sed -i 's/hello/world/' file.txt945 Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space sed -i 's/hello/world/' file.txt975 Trao đổi nội dung của không gian giữ và mẫu 3. 6 Lệnh dành cho chuyên gia sed -i 's/hello/world/' file.txt 22Trong 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 bằng thứ gì đó như sed -i 's/hello/world/' file.txt341 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 -i 's/hello/world/' file.txt981 [Không cho phép địa chỉ. ] Specify the location of label for branch commands. In all other respects, a no-op sed -i 's/hello/world/' file.txt934 Chi nhánh vô điều kiện để dán nhãn. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu sed -i 's/hello/world/' file.txt966 Chỉ nhánh để gắn nhãn nếu đã có một sự thay thế sed -i 's/hello/world/' file.txt967 thành công kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện đã được thực hiện. The label may be omitted, in which case the next cycle is started 3. 7 Lệnh dành riêng cho GNU sed -i 's/hello/world/' file.txt 22These commands are specific to GNU sed -i 's/hello/world/' file.txt22, so you must use them with care and only when you are sure that hindering portability is not evil. They allow you to check for GNU sed -i 's/hello/world/' file.txt22 extensions or to do tasks that are required quite often, yet are unsupported by standard sed -i 's/hello/world/' file.txt22s sed -i 's/hello/world/' file.txt351 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 -i 's/hello/world/' file.txt941 thực thi lệnh được tìm thấy trong không gian mẫu và thay thế không gian mẫu bằng đầu ra; Thay vào đó, nếu một tham số được chỉ định, lệnh sed -i 's/hello/world/' file.txt941 sẽ diễn giải nó thành một 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 In both cases, the results are undefined if the command to be executed contains a NUL character Note that, unlike the sed -i 's/hello/world/' file.txt354 command, the output of the command will be printed immediately; the sed -i 's/hello/world/' file.txt354 command instead delays the output to the end of the current cycle sed -i 's/hello/world/' file.txt943 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 -i 's/hello/world/' file.txt357 This command accepts only one address This command is the same as sed -i 's/hello/world/' file.txt900, but will not print the contents of pattern space. Giống như sed -i 's/hello/world/' file.txt900, nó cung cấp khả năng trả lại mã thoát cho người gọi 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 -i 's/hello/world/' file.txt90 sed -i 's/hello/world/' file.txt964 Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. 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 As with the sed -i 's/hello/world/' file.txt354 command, the special value /dev/stdin is supported for the file name, which reads a line from the standard input sed -i 's/hello/world/' file.txt968 Chỉ phân nhánh để gắn nhãn nếu không có thay thế sed -i 's/hello/world/' file.txt967 thành công nào kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu sed -i 's/hello/world/' file.txt364 This command does nothing, but makes sed -i 's/hello/world/' file.txt22 fail if GNU sed -i 's/hello/world/' file.txt22 extensions are not supported, simply because other versions of sed -i 's/hello/world/' file.txt22 do not implement it. Ngoài ra, bạn có thể chỉ định phiên bản 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 -i 's/hello/world/' file.txt369. The default is sed -i 's/hello/world/' file.txt370 because that is the first version that implemented this command 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 -i 's/hello/world/' file.txt974 Ghi vào tên tệp đã cho phần không gian mẫu cho đến dòng mới đầu tiên. Mọi thứ được nói dưới lệnh sed -i 's/hello/world/' file.txt326 về việc xử lý tệp cũng được giữ ở đây sed -i 's/hello/world/' file.txt977 Lệnh này làm trống nội dung của không gian mẫu. It is usually the same as ‘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 mandates that such sequences are not matched by ‘. ’, vì vậy 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 Multiple commands syntaxCó 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 Using newlines is most natural when running a sed script from a file (using the -f option) 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. Alternatively, you may specify each command as an argument to an -e option sed -i 's/hello/world/' file.txt91 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 -i 's/hello/world/' file.txt92 The sed -i 's/hello/world/' file.txt178, sed -i 's/hello/world/' file.txt179, sed -i 's/hello/world/' file.txt982, sed -i 's/hello/world/' file.txt983, sed -i 's/hello/world/' file.txt984, sed -i 's/hello/world/' file.txt383 commands can be separated with a semicolon (this is a non-portable GNU sed -i 's/hello/world/' file.txt22 extension) sed -i 's/hello/world/' file.txt93 Labels used in sed -i 's/hello/world/' file.txt982, sed -i 's/hello/world/' file.txt983, sed -i 's/hello/world/' file.txt984, sed -i 's/hello/world/' file.txt383 commands are read until a semicolon. 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. The second is a portable equivalent. For more information about branching and labels see Branching and flow control sed -i 's/hello/world/' file.txt94 3. 8. 1 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 -i 's/hello/world/' file.txt926, sed -i 's/hello/world/' file.txt927, sed -i 's/hello/world/' file.txt928 (append/change/insert) All characters following sed -i 's/hello/world/' file.txt926, sed -i 's/hello/world/' file.txt927, sed -i 's/hello/world/' file.txt928 commands are taken as the text to append/change/insert. Sử dụng dấu chấm phẩy dẫn đến kết quả không mong muốn sed -i 's/hello/world/' file.txt95 Separate the commands using -e or a newline sed -i 's/hello/world/' file.txt96 Lưu ý rằng việc chỉ định văn bản để thêm ('Xin chào') ngay sau sed -i 's/hello/world/' file.txt926, sed -i 's/hello/world/' file.txt927, sed -i 's/hello/world/' file.txt928 chính nó là một tiện ích mở rộng GNU sed -i 's/hello/world/' file.txt22. A portable, POSIX-compliant alternative is sed -i 's/hello/world/' file.txt97 sed -i 's/hello/world/' file.txt978 (comment) Tất cả các ký tự theo sau '#' cho đến dòng mới tiếp theo đều bị bỏ qua sed -i 's/hello/world/' file.txt98 sed -i 's/hello/world/' file.txt354, sed -i 's/hello/world/' file.txt88, sed -i 's/hello/world/' file.txt326, sed -i 's/hello/world/' file.txt33 (reading and writing files) The sed -i 's/hello/world/' file.txt354, sed -i 's/hello/world/' file.txt88, sed -i 's/hello/world/' file.txt326, sed -i 's/hello/world/' file.txt33 commands parse the filename until end of the line. 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 -i 's/hello/world/' file.txt99 Lưu ý rằng sed -i 's/hello/world/' file.txt22 âm thầm bỏ qua các lỗi đọc/ghi trong các lệnh sed -i 's/hello/world/' file.txt354, sed -i 's/hello/world/' file.txt88, sed -i 's/hello/world/' file.txt326, sed -i 's/hello/world/' file.txt33 (chẳng hạn như 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 ; . The file is missing, and the error is silently ignored sed -i 's/hello/world/' file.txt10 sed -i 's/hello/world/' file.txt941 (command execution) Bất kỳ ký tự nào sau lệnh sed -i 's/hello/world/' file.txt941 cho đến cuối dòng sẽ được gửi đến trình bao. If whitespace, comments or semicolons are found, they will be included in the shell command, leading to unexpected results sed -i 's/hello/world/' file.txt11 sed -i 's/hello/world/' file.txt617 (thay thế bằng cờ sed -i 's/hello/world/' file.txt941 hoặc sed -i 's/hello/world/' file.txt326) Trong một lệnh thay thế, cờ sed -i 's/hello/world/' file.txt326 ghi kết quả thay thế vào một tệp và cờ sed -i 's/hello/world/' file.txt941 thực thi kết quả thay thế dưới dạng lệnh trình bao. Như với các lệnh sed -i 's/hello/world/' file.txt622, chúng phải được kết thúc bằng một dòng mới. Nếu tìm thấy khoảng trắng, nhận xét hoặc dấu chấm phẩy, chúng sẽ được đưa vào lệnh shell hoặc tên tệp, dẫn đến kết quả không mong muốn sed -i 's/hello/world/' file.txt12 4 địa chỉ. chọn dòng4. 1 Addresses overviewĐịa chỉ xác định (các) dòng lệnh sed -i 's/hello/world/' file.txt22 sẽ được thực thi. Lệnh sau thay thế từ 'hello' bằng 'world' chỉ trên dòng 144 sed -i 's/hello/world/' file.txt13 Nếu không có địa chỉ nào được đưa ra, lệnh được thực hiện trên tất cả các dòng. The following command replaces the word ‘hello’ with ‘world’ on all lines in the input file ________số 8 Addresses can contain regular expressions to match lines based on content instead of line numbers. Lệnh sau chỉ thay thế từ 'hello' bằng 'world' trong các dòng có chứa từ 'apple' sed -i 's/hello/world/' file.txt15 Một dải địa chỉ được chỉ định với hai địa chỉ được phân tách bằng dấu phẩy ( sed -i 's/hello/world/' file.txt624). Địa chỉ có thể là số, biểu thức chính quy hoặc kết hợp cả hai. Lệnh sau thay thế từ 'xin chào' bằng 'thế giới' chỉ trong các dòng 4 đến 17 (đã bao gồm) sed -i 's/hello/world/' file.txt16 Việc thêm ký tự sed -i 's/hello/world/' file.txt625 vào cuối thông số địa chỉ (trước chữ cái lệnh) sẽ phủ nhận ý nghĩa của sự trùng khớp. Nghĩa là, nếu ký tự sed -i 's/hello/world/' file.txt625 theo sau một địa chỉ hoặc một dải địa chỉ, thì chỉ những dòng không khớp với địa chỉ mới được chọn. The following command replaces the word ‘hello’ with ‘world’ only in lines not containing the word ‘apple’ sed -i 's/hello/world/' file.txt17 The following command replaces the word ‘hello’ with ‘world’ only in lines 1 to 3 and 18 till the last line of the input file (i. e. trừ dòng 4 đến 17) sed -i 's/hello/world/' file.txt18 4. 2 Selecting lines by numbersĐịa chỉ trong tập lệnh sed -i 's/hello/world/' file.txt22 có thể ở bất kỳ dạng nào sau đây sed -i 's/hello/world/' file.txt128 Specifying a line number will match only that line in the input. (Note that sed -i 's/hello/world/' file.txt22 counts lines continuously across all input files unless -i or -s options are specified. ) sed -i 's/hello/world/' file.txt87 Địa chỉ này khớp với dòng cuối cùng của tệp đầu vào cuối cùng hoặc dòng cuối cùng của mỗi tệp khi tùy chọn -i hoặc -s được chỉ định sed -i 's/hello/world/' file.txt631 This GNU extension matches every stepth line starting with line first. In particular, lines will be selected when there exists a non-negative n such that the current line-number equals first + (n * step). Thus, one would use sed -i 's/hello/world/' file.txt632 to select the odd-numbered lines and sed -i 's/hello/world/' file.txt633 for even-numbered lines; to pick every third line starting with the second, ‘2~3’ would be used; to pick every fifth line starting with the tenth, use ‘10~5’; and ‘50~0’ is just an obscure way of saying sed -i 's/hello/world/' file.txt634 Các lệnh sau minh họa việc sử dụng địa chỉ bước sed -i 's/hello/world/' file.txt19 4. 3 dòng chọn bằng cách khớp văn bả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à Biểu thức chính quy cơ bản (BRE). If -E or -r options are used, The regular expression should be in Extended Regular Expression (ERE) syntax. Xem BRE so với ERE sed -i 's/hello/world/' file.txt636 Điều này sẽ chọn bất kỳ dòng nào khớp với biểu thức chính quy. Nếu bản thân biểu thức chính quy bao gồm bất kỳ ký tự sed -i 's/hello/world/' file.txt994 nào, thì mỗi ký tự đó phải được thoát bằng dấu gạch chéo ngược ( sed -i 's/hello/world/' file.txt997) Lệnh sau in các dòng trong /etc/passwd kết thúc bằng 'bash'5 sed -i 's/hello/world/' file.txt30 Biểu thức chính quy trống '//' lặp lại kết quả khớp biểu thức chính quy cuối cùng (tương tự nếu biểu thức chính quy trống được truyền cho lệnh sed -i 's/hello/world/' file.txt967). Note that modifiers to regular expressions are evaluated when the regular expression is compiled, thus it is invalid to specify them together with the empty regular expression sed -i 's/hello/world/' file.txt640 (Có thể thay thế sed -i 's/hello/world/' file.txt641 bằng bất kỳ ký tự đơn nào khác. ) Điều này cũng phù hợp với biểu thức chính quy biểu thức chính quy, nhưng cho phép một người sử dụng dấu phân cách khác với sed -i 's/hello/world/' file.txt994. This is particularly useful if the regexp itself contains a lot of slashes, since it avoids the tedious escaping of every sed -i 's/hello/world/' file.txt994. Nếu bản thân biểu thức chính quy bao gồm bất kỳ ký tự phân cách nào, thì mỗi ký tự đó phải được thoát bằng dấu gạch chéo ngược ( sed -i 's/hello/world/' file.txt997) The following commands are equivalent. Họ in các dòng bắt đầu bằng '/home/alice/documents/' sed -i 's/hello/world/' file.txt31 sed -i 's/hello/world/' file.txt645 sed -i 's/hello/world/' file.txt646 Công cụ sửa đổi sed -i 's/hello/world/' file.txt145 để khớp biểu thức chính quy là một tiện ích mở rộng GNU khiến biểu thức chính quy được khớp theo cách không phân biệt chữ hoa chữ thường Trong nhiều ngôn ngữ lập trình khác, chữ thường sed -i 's/hello/world/' file.txt928 được sử dụng để đối sánh biểu thức chính quy không phân biệt chữ hoa chữ thường. Tuy nhiên, trong sed -i 's/hello/world/' file.txt22, sed -i 's/hello/world/' file.txt928 được sử dụng cho lệnh chèn (xem lệnh chèn) Quan sát sự khác biệt giữa các ví dụ sau Trong ví dụ này, sed -i 's/hello/world/' file.txt651 là địa chỉ. regular expression with sed -i 's/hello/world/' file.txt145 modifier. sed -i 's/hello/world/' file.txt36 là lệnh xóa sed -i 's/hello/world/' file.txt32 Here, sed -i 's/hello/world/' file.txt654 is the address. a regular expression. sed -i 's/hello/world/' file.txt928 is the insert command. sed -i 's/hello/world/' file.txt36 is the value to insert. A line with ‘d’ is then inserted above the matched line sed -i 's/hello/world/' file.txt33 sed -i 's/hello/world/' file.txt657 sed -i 's/hello/world/' file.txt658 Công cụ sửa đổi sed -i 's/hello/world/' file.txt149 để 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 -i 's/hello/world/' file.txt154 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 -i 's/hello/world/' file.txt156 và sed -i 's/hello/world/' file.txt157) 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 Regex addresses operate on the content of the current pattern space. If the pattern space is changed (for example with sed -i 's/hello/world/' file.txt666 command) the regular expression matching will operate on the changed text In the following example, automatic printing is disabled with -n. The sed -i 's/hello/world/' file.txt667 command changes lines containing ‘2’ to ‘X’. The command sed -i 's/hello/world/' file.txt668 matches lines with digits and prints them. Because the second line is changed before the sed -i 's/hello/world/' file.txt669 regex, it will not match and will not be printed sed -i 's/hello/world/' file.txt34 4. 4 Range AddressesAn address range can be specified by specifying two addresses separated by a comma ( sed -i 's/hello/world/' file.txt624). An address range matches lines starting from where the first address matches, and continues until the second address matches (inclusively) sed -i 's/hello/world/' file.txt35 If the second address is a regexp, then checking for the ending match will start with the line following the line which matched the first address. a range will always span at least two lines (except of course if the input stream ends) sed -i 's/hello/world/' file.txt36 If the second address is a number less than (or equal to) the line matching the first address, then only the one line is matched sed -i 's/hello/world/' file.txt37 GNU sed -i 's/hello/world/' file.txt22 also supports some special two-address forms; all these are GNU extensions sed -i 's/hello/world/' file.txt672 A line number of sed -i 's/hello/world/' file.txt673 can be used in an address specification like sed -i 's/hello/world/' file.txt672 so that sed -i 's/hello/world/' file.txt22 will try to match regexp in the first input line too. In other words, sed -i 's/hello/world/' file.txt672 is similar to sed -i 's/hello/world/' file.txt677, except that if addr2 matches the very first line of input the sed -i 's/hello/world/' file.txt672 form will consider it to end the range, whereas the sed -i 's/hello/world/' file.txt677 form will match the beginning of its range and hence make the range span up to the second occurrence of the regular expression Note that this is the only place where the sed -i 's/hello/world/' file.txt673 address makes sense; there is no 0-th line and commands which are given the sed -i 's/hello/world/' file.txt673 address in any other way will give an error The following examples demonstrate the difference between starting with address 1 and 0 sed -i 's/hello/world/' file.txt38 sed -i 's/hello/world/' file.txt682 Matches addr1 and the N lines following addr1 sed -i 's/hello/world/' file.txt39 addr1 can be a line number or a regular expression sed -i 's/hello/world/' file.txt683 Matches addr1 and the lines following addr1 until the next line whose input line number is a multiple of N. The following command prints starting at line 6, until the next line which is a multiple of 4 (i. e. line 8) sed -i 's/hello/world/' file.txt60 addr1 can be a line number or a regular expression 5 Regular Expressions. selecting text5. 1 Overview of regular expression in sed -i 's/hello/world/' file.txt 22To know how to use sed -i 's/hello/world/' file.txt22, people should understand regular expressions (regexp for short). A regular expression is a pattern that is matched against a subject string from left to right. Most characters are ordinary. they stand for themselves in a pattern, and match the corresponding characters. Regular expressions in sed -i 's/hello/world/' file.txt22 are specified between two slashes The following command prints lines containing the word ‘hello’ The above example is equivalent to this sed -i 's/hello/world/' file.txt687 command The power of regular expressions comes from the ability to include alternatives and repetitions in the pattern. These are encoded in the pattern by the use of special characters, which do not stand for themselves but instead are interpreted in some special way The character sed -i 's/hello/world/' file.txt154 (caret) in a regular expression matches the beginning of the line. The character sed -i 's/hello/world/' file.txt689 (dot) matches any single character. The following sed -i 's/hello/world/' file.txt22 command matches and prints lines which start with the letter ‘b’, followed by any single character, followed by the letter ‘d’ sed -i 's/hello/world/' file.txt61 The following sections explain the meaning and usage of special characters in regular expressions 5. 2 Basic (BRE) and extended (ERE) regular expressionBasic and extended regular expressions are two variations on the syntax of the specified pattern. Basic Regular Expression (BRE) syntax is the default in sed -i 's/hello/world/' file.txt22 (and similarly in sed -i 's/hello/world/' file.txt687). Use the POSIX-specified -E option (-r, --regexp-extended) to enable Extended Regular Expression (ERE) syntax In GNU sed -i 's/hello/world/' file.txt22, the only difference between basic and extended regular expressions is in the behavior of a few special characters. ‘?’, ‘+’, parentheses, braces (‘{}’), and ‘. ’ With basic (BRE) syntax, these characters do not have special meaning unless prefixed with a backslash (‘\’); While with extended (ERE) syntax it is reversed. these characters are special unless they are prefixed with backslash (‘\’) Desired patternBasic (BRE) SyntaxExtended (ERE) Syntaxliteral ‘+’ (plus sign)sed -i 's/hello/world/' file.txt62 sed -i 's/hello/world/' file.txt63One or more ‘a’ characters followed by ‘b’ (plus sign as special meta-character) sed -i 's/hello/world/' file.txt64 sed -i 's/hello/world/' file.txt65 5. 3 Overview of basic regular expression syntaxHere is a brief description of regular expression syntax as used in sed -i 's/hello/world/' file.txt22 sed -i 's/hello/world/' file.txt695 A single ordinary character matches itself 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 -i 's/hello/world/' file.txt997, a sed -i 's/hello/world/' file.txt689, 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, sed -i 's/hello/world/' file.txt900 is equivalent to sed -i 's/hello/world/' file.txt901. 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 sed -i 's/hello/world/' file.txt903 in these contexts sed -i 's/hello/world/' file.txt689 Matches any character, including newline sed -i 's/hello/world/' file.txt154 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 How sed -i 's/hello/world/' file.txt22 works). So, it is a useful simplification to think of sed -i 's/hello/world/' file.txt907 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 -i 's/hello/world/' file.txt967 command sed -i 's/hello/world/' file.txt154 acts as a special character only at the beginning of the regular expression or subexpression (that is, after sed -i 's/hello/world/' file.txt991 or sed -i 's/hello/world/' file.txt911). Portable scripts should avoid sed -i 's/hello/world/' file.txt154 at the beginning of a subexpression, though, as POSIX allows implementations that treat sed -i 's/hello/world/' file.txt154 as an ordinary character in that context sed -i 's/hello/world/' file.txt87 It is the same as sed -i 's/hello/world/' file.txt154, 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 -i 's/hello/world/' file.txt992 or sed -i 's/hello/world/' file.txt911), and its use at the end of a subexpression is not portable sed -i 's/hello/world/' file.txt919 sed -i 's/hello/world/' file.txt920 Matches any single character in list. for example, sed -i 's/hello/world/' file.txt921 matches all vowels. A list may include sequences like sed -i 's/hello/world/' file.txt922, which matches any character between (inclusive) char1 and char2. See Character Classes and Bracket Expressions sed -i 's/hello/world/' file.txt923 As sed -i 's/hello/world/' file.txt60, but matches one or more. It is a GNU extension sed -i 's/hello/world/' file.txt925 As sed -i 's/hello/world/' file.txt60, but only matches zero or one. It is a GNU extension sed -i 's/hello/world/' file.txt927 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) sed -i 's/hello/world/' file.txt929 Matches between i and j, inclusive, sequences sed -i 's/hello/world/' file.txt930 Matches more than or equal to i sequences sed -i 's/hello/world/' file.txt931 Groups the inner regexp as a whole, this is used to
sed -i 's/hello/world/' file.txt935 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 sed -i 's/hello/world/' file.txt936 Matches the concatenation of regexp1 and regexp2. Concatenation binds more tightly than sed -i 's/hello/world/' file.txt911, sed -i 's/hello/world/' file.txt154, and sed -i 's/hello/world/' file.txt87, but less tightly than the other regular expression operators sed -i 's/hello/world/' file.txt940 Matches the digit-th sed -i 's/hello/world/' file.txt941 parenthesized subexpression in the regular expression. This is called a back reference. Subexpressions are implicitly numbered by counting occurrences of sed -i 's/hello/world/' file.txt991 left-to-right sed -i 's/hello/world/' file.txt990 Matches the newline character sed -i 's/hello/world/' file.txt944 Matches char, where char is one of sed -i 's/hello/world/' file.txt87, sed -i 's/hello/world/' file.txt60, sed -i 's/hello/world/' file.txt689, sed -i 's/hello/world/' file.txt948, sed -i 's/hello/world/' file.txt997, or sed -i 's/hello/world/' file.txt154. Note that the only C-like backslash sequences that you can portably assume to be interpreted are sed -i 's/hello/world/' file.txt990 and sed -i 's/hello/world/' file.txt195; in particular sed -i 's/hello/world/' file.txt953 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 sed -i 's/hello/world/' file.txt955 becomes ‘abc\?’ when using extended regular expressions. It matches the literal string ‘abc?’ sed -i 's/hello/world/' file.txt956 becomes ‘c+’ when using extended regular expressions. It matches one or more ‘c’s sed -i 's/hello/world/' file.txt957 becomes ‘a{3,}’ when using extended regular expressions. It matches three or more ‘a’s sed -i 's/hello/world/' file.txt958 becomes ‘(abc){2,3}’ when using extended regular expressions. It matches either ‘abcabc’ or ‘abcabcabc’ sed -i 's/hello/world/' file.txt959 becomes ‘(abc*)\1’ when using extended regular expressions. Backreferences must still be escaped when using extended regular expressions sed -i 's/hello/world/' file.txt960 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 basic and extended 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 sed -i 's/hello/world/' file.txt66 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/. ) sed -i 's/hello/world/' file.txt67‘[. 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. ‘[. thấp hơn. ]’ and ‘[. phía trên. ]’; 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. sed -i 's/hello/world/' file.txt962‘[. graph. ]’ Graphical characters. ‘[. alnum. ]’ and ‘[. punct. ]’ ‘[. lower. ]’Lower-case letters; in the ‘C’ locale and ASCII character encoding, this is sed -i 's/hello/world/' file.txt963‘[. print. ]’ Printable characters. ‘[. alnum. ]’, ‘[. punct. ]’, and space ‘[. punct. ]’Punctuation characters; in the ‘C’ locale and ASCII character encoding, this is sed -i 's/hello/world/' file.txt964‘[. 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 sed -i 's/hello/world/' file.txt965‘[. xdigit. ]’ Hexadecimal digits. sed -i 's/hello/world/' file.txt966 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 ‘]’ends the bracket expression if it’s not the first list item. So, if you want to make the ‘]’ character a list item, you must put it first ‘-’represents the range if it’s not first or last in a list or the ending point of a range ‘^’represents the characters not in the list. If you want to make the ‘^’ character a list item, place it anywhere but first TODO. incorporate this paragraph (copied verbatim from BRE section) The characters sed -i 's/hello/world/' file.txt87, sed -i 's/hello/world/' file.txt60, sed -i 's/hello/world/' file.txt689, sed -i 's/hello/world/' file.txt948, and sed -i 's/hello/world/' file.txt997 are normally not special within list. For example, sed -i 's/hello/world/' file.txt972 matches either ‘\’ or ‘*’, because the sed -i 's/hello/world/' file.txt997 is not special here. However, strings like sed -i 's/hello/world/' file.txt974, sed -i 's/hello/world/' file.txt975, and sed -i 's/hello/world/' file.txt976 are special within list and represent collating symbols, equivalence classes, and character classes, respectively, and sed -i 's/hello/world/' file.txt948 is therefore special within list when it is followed by sed -i 's/hello/world/' file.txt689, sed -i 's/hello/world/' file.txt980, or sed -i 's/hello/world/' file.txt383. Also, when not in sed -i 's/hello/world/' file.txt72 mode, special escapes like sed -i 's/hello/world/' file.txt990 and sed -i 's/hello/world/' file.txt953 are recognized within list. See Escapes‘[. ’ represents the open collating symbol ‘. ]’represents the close collating symbol ‘[=’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 addresses and the sed -i 's/hello/world/' file.txt967 command) These can be used in both basic and extended regular expressions (that is, with or without the -E/-r options) sed -i 's/hello/world/' file.txt985 Matches any “word” character. A “word” character is any letter or digit or the underscore character sed -i 's/hello/world/' file.txt68 sed -i 's/hello/world/' file.txt986 Matches any “non-word” character sed -i 's/hello/world/' file.txt69 sed -i 's/hello/world/' file.txt987 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 sed -i 's/hello/world/' file.txt90 sed -i 's/hello/world/' file.txt988 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 sed -i 's/hello/world/' file.txt91 sed -i 's/hello/world/' file.txt989 Matches whitespace characters (spaces and tabs). Newlines embedded in the pattern/hold spaces will also match sed -i 's/hello/world/' file.txt92 sed -i 's/hello/world/' file.txt990 Matches non-whitespace characters sed -i 's/hello/world/' file.txt93 sed -i 's/hello/world/' file.txt991 Matches the beginning of a word sed -i 's/hello/world/' file.txt94 sed -i 's/hello/world/' file.txt992 Matches the end of a word sed -i 's/hello/world/' file.txt95 sed -i 's/hello/world/' file.txt156 Matches only at the start of pattern space. Điều này khác với sed -i 's/hello/world/' file.txt154 ở chế độ nhiều dòng Compare the following two examples sed -i 's/hello/world/' file.txt96 sed -i 's/hello/world/' file.txt157 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 -i 's/hello/world/' file.txt967 command (see Regular Expression Addresses and The "s" Command) 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 sed -i 's/hello/world/' file.txt97 Multiple subexpressions are automatically numbered from left-to-right. This command searches for 6-letter palindromes (the first three letters are 3 subexpressions, followed by 3 back-references in reverse order) sed -i 's/hello/world/' file.txt98 In the sed -i 's/hello/world/' file.txt967 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 sed -i 's/hello/world/' file.txt99 When used with alternation, if the group does not participate in the match then the back-reference makes the whole match fail. For example, ‘a(. ). b\1’ will not match ‘ba’. When multiple regular expressions are given with -e or from a file (‘-f file’), back-references are local to each expression 5. 8 Escape Sequences - specifying special 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 escape6—that is, escapes that are applied to a character or sequence of characters that ordinarily are taken literally, and that sed -i 's/hello/world/' file.txt22 replaces with a special character. This provides a way of encoding non-printable characters in patterns in a visible manner. There is no restriction on the appearance of non-printing characters in a sed -i 's/hello/world/' file.txt22 script but when a script is being prepared in the shell or by text editing, it is usually easier to use one of the following escape sequences than the binary character it represents The list of these escapes is sed 's/hello/world/' input.txt > output.txt902 Produces or matches a BEL character, that is an “alert” (ASCII 7) sed 's/hello/world/' input.txt > output.txt903 Produces or matches a form feed (ASCII 12) sed -i 's/hello/world/' file.txt990 Produces or matches a newline (ASCII 10) sed 's/hello/world/' input.txt > output.txt905 Produces or matches a carriage return (ASCII 13) sed -i 's/hello/world/' file.txt953 Produces or matches a horizontal tab (ASCII 9) sed 's/hello/world/' input.txt > output.txt907 Produces or matches a so called “vertical tab” (ASCII 11) sed 's/hello/world/' input.txt > output.txt908 Produces or matches CONTROL-x, where x is any character. The precise effect of ‘\cx’ is as follows. if x is a lower case letter, it is converted to upper case. Then bit 6 of the character (hex 40) is inverted. Thus ‘\cz’ becomes hex 1A, but ‘\c{’ becomes hex 3B, while ‘\c;’ becomes hex 7B sed 's/hello/world/' input.txt > output.txt909 Produces or matches a character whose decimal ASCII value is xxx sed 's/hello/world/' input.txt > output.txt910 Produces or matches a character whose octal ASCII value is xxx sed 's/hello/world/' input.txt > output.txt911 Produces or matches a character whose hexadecimal ASCII value is xx ‘\b’ (backspace) was omitted because of the conflict with the existing “word boundary” meaning 5. 8. 1 Escaping PrecedenceGNU sed -i 's/hello/world/' file.txt22 processes escape sequences before passing the text onto the regular-expression matching of the sed -i 's/hello/world/' file.txt666 command and Address matching. Thus the follwing two commands are equivalent (‘0x5e’ is the hexadecimal ASCII value of the character ‘^’) sed 's/hello/world/' input.txt > output.txt90 As are the following (‘0x5b’,‘0x5d’ are the hexadecimal ASCII values of ‘[’,‘]’, respectively) sed 's/hello/world/' input.txt > output.txt91 However it is recommended to avoid such special characters due to unexpected edge-cases. For example, the following are not equivalent sed 's/hello/world/' input.txt > output.txt92 5. 9 Multibyte characters and Locale ConsiderationsGNU sed -i 's/hello/world/' file.txt22 processes valid multibyte characters in multibyte locales (e. g. sed 's/hello/world/' input.txt > output.txt915). 7 The following example uses the Greek letter Capital Sigma (Σ, Unicode code point sed 's/hello/world/' input.txt > output.txt916). In a sed 's/hello/world/' input.txt > output.txt915 locale, sed -i 's/hello/world/' file.txt22 correctly processes the Sigma as one character despite it being 2 octets (bytes) sed 's/hello/world/' input.txt > output.txt93 To force sed -i 's/hello/world/' file.txt22 to process octets separately, use the sed 's/hello/world/' input.txt > output.txt920 locale (also known as the sed 's/hello/world/' input.txt > output.txt921 locale) sed 's/hello/world/' input.txt > output.txt94 5. 9. 1 Invalid multibyte 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 sed 's/hello/world/' input.txt > output.txt923 is an incomplete multibyte character (shown here as �). The regular expression ‘. ’ does not match it sed 's/hello/world/' input.txt > output.txt95 Similarly, the ’catch-all’ regular expression ‘. *’ does not match the entire line sed 's/hello/world/' input.txt > output.txt96 GNU sed -i 's/hello/world/' file.txt22 offers the special sed -i 's/hello/world/' file.txt977 command to clear the current pattern space regardless of invalid multibyte characters (i. e. it works like sed 's/hello/world/' input.txt > output.txt926 but also removes invalid multibyte characters) sed 's/hello/world/' input.txt > output.txt97 Alternatively, force the sed 's/hello/world/' input.txt > output.txt920 locale to process each octet separately (every octet is a valid character in the sed 's/hello/world/' input.txt > output.txt920 locale) sed 's/hello/world/' input.txt > output.txt98 sed -i 's/hello/world/' file.txt22’s inability to process invalid multibyte characters can be used to detect such invalid sequences in a file. In the following examples, the sed 's/hello/world/' input.txt > output.txt930 is an invalid multibyte sequence, while sed 's/hello/world/' input.txt > output.txt931 is a valid multibyte sequence (of the Greek Sigma character) The following sed -i 's/hello/world/' file.txt22 program removes all valid characters using sed 's/hello/world/' input.txt > output.txt933. Any content left in the pattern space (the invalid characters) are added to the hold space using the sed -i 's/hello/world/' file.txt947 command. On the last line ( sed -i 's/hello/world/' file.txt87), the hold space is retrieved ( sed -i 's/hello/world/' file.txt975), newlines are removed ( sed 's/hello/world/' input.txt > output.txt937), and any remaining octets are printed unambiguously ( sed -i 's/hello/world/' file.txt67). Thus, any invalid multibyte sequences are printed as octal values sed 's/hello/world/' input.txt > output.txt99 With a few more commands, sed -i 's/hello/world/' file.txt22 can print the exact line number corresponding to each invalid characters (line 3). These characters can then be removed by forcing the sed 's/hello/world/' input.txt > output.txt920 locale and using octal escape sequences sed -i 's/hello/world/' file.txt040 5. 9. 2 Chuyển đổi chữ hoa/chữ thườngGNU sed -i 's/hello/world/' file.txt22’s substitute command ( sed -i 's/hello/world/' file.txt967) supports upper/lower case conversions using sed -i 's/hello/world/' file.txt105, sed -i 's/hello/world/' file.txt104 codes. These conversions support multibyte characters sed -i 's/hello/world/' file.txt041 See The "s" Command 5. 9. 3 Multibyte regexp character 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 sed 's/hello/world/' input.txt > output.txt945 environment variable to the value ‘C’ sed -i 's/hello/world/' file.txt042 Their interpretation depends on the sed 's/hello/world/' input.txt > output.txt946 locale; for example, ‘[[. alnum. ]]’ means the character class of numbers and letters in the current locale TODO. show example of collation sed -i 's/hello/world/' file.txt043 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. 8 Then the next cycle starts for the next input line Unless special commands (like ‘D’) are used, the pattern space is deleted between two cycles. The hold space, on the other hand, keeps its data between cycles (see commands ‘h’, ‘H’, ‘x’, ‘g’, ‘G’ to move data between both buffers) 6. 2 Hold and Pattern 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 -i 's/hello/world/' file.txt939, sed -i 's/hello/world/' file.txt945, sed -i 's/hello/world/' file.txt947, sed -i 's/hello/world/' file.txt71, sed -i 's/hello/world/' file.txt957. They are similar to their lowercase counterparts ( sed -i 's/hello/world/' file.txt36, sed -i 's/hello/world/' file.txt944, sed -i 's/hello/world/' file.txt946, sed -i 's/hello/world/' file.txt952, sed -i 's/hello/world/' file.txt37), except that these commands append or subtract data while respecting embedded newlines - allowing adding and removing lines from the pattern and hold spaces They operate as follows sed -i 's/hello/world/' file.txt939 deletes line from the pattern space until the first newline, and restarts the cycle sed -i 's/hello/world/' file.txt945 appends line from the hold space to the pattern space, with a newline before it sed -i 's/hello/world/' file.txt947 appends line from the pattern space to the hold space, with a newline before it sed -i 's/hello/world/' file.txt71 appends line from the input file to the pattern space sed -i 's/hello/world/' file.txt957 prints line from the pattern space until the first newline The following example illustrates the operation of sed -i 's/hello/world/' file.txt71 and sed -i 's/hello/world/' file.txt939 commands sed -i 's/hello/world/' file.txt044
A common technique to process blocks of text such as paragraphs (instead of line-by-line) is using the following construct sed -i 's/hello/world/' file.txt045
The following example demonstrates this technique sed -i 's/hello/world/' file.txt046 For more annotated examples, see Text search across multiple lines and Line length adjustment 6. 4 Branching and Flow ControlThe branching commands sed -i 's/hello/world/' file.txt982, sed -i 's/hello/world/' file.txt983, and sed -i 's/hello/world/' file.txt984 enable changing the flow of sed -i 's/hello/world/' file.txt22 programs By default, sed -i 's/hello/world/' file.txt22 reads an input line into the pattern buffer, then continues to processes all commands in order. Commands without addresses affect all lines. Commands with addresses affect only matching lines. See Execution Cycle and Addresses overview sed -i 's/hello/world/' file.txt22 does not support a typical sed 's/hello/world/' input.txt > output.txt985 construct. Instead, some commands can be used as conditionals or to change the default flow control sed -i 's/hello/world/' file.txt36 delete (clears) the current pattern space, and restart the program cycle without processing the rest of the commands and without printing the pattern space sed -i 's/hello/world/' file.txt939 delete the contents of the pattern space up to the first newline, and restart the program cycle without processing the rest of the commands and without printing the pattern space sed 's/hello/world/' input.txt > output.txt988 sed 's/hello/world/' input.txt > output.txt989 sed 's/hello/world/' input.txt > output.txt990 sed 's/hello/world/' input.txt > output.txt991 Addresses and regular expressions can be used as an sed 's/hello/world/' input.txt > output.txt985 conditional. If [addr] matches the current pattern space, execute the command(s). For example. The command sed 's/hello/world/' input.txt > output.txt993 means. if the current pattern matches the regular expression sed 's/hello/world/' input.txt > output.txt994 (a line starting with a hash), then execute the sed -i 's/hello/world/' file.txt36 command. delete the line without printing it, and restart the program cycle immediately sed -i 's/hello/world/' file.txt982 branch unconditionally (that is. always jump to a label, skipping or repeating other commands, without restarting a new cycle). Combined with an address, the branch can be conditionally executed on matched lines sed -i 's/hello/world/' file.txt983 branch conditionally (that is. jump to a label) only if a sed -i 's/hello/world/' file.txt666 command has succeeded since the last input line was read or another conditional branch was taken sed -i 's/hello/world/' file.txt984 similar but opposite to the sed -i 's/hello/world/' file.txt983 command. branch only if there has been no successful substitutions since the last input line was read The following two sed -i 's/hello/world/' file.txt22 programs are equivalent. The first (contrived) example uses the sed -i 's/hello/world/' file.txt982 command to skip the sed -i 's/hello/world/' file.txt666 command on lines containing ‘1’. The second example uses an address with negation (‘. ’) to perform substitution only on desired lines. The sed -i 's/hello/world/' file.txt0404 command is still executed on all lines sed -i 's/hello/world/' file.txt047 6. 4. 1 Branching and CyclesThe sed -i 's/hello/world/' file.txt982, sed -i 's/hello/world/' file.txt983 and sed -i 's/hello/world/' file.txt984 commands can be followed by a label (typically a single letter). Labels are defined with a colon followed by one or more letters (e. g. ‘. x’). If the label is omitted the branch commands restart the cycle. Note the difference between branching to a label and restarting the cycle. when a cycle is restarted, sed -i 's/hello/world/' file.txt22 first prints the current content of the pattern space, then reads the next input line into the pattern space; Jumping to a label (even if it is at the beginning of the program) does not print the pattern space and does not read the next input line The following program is a no-op. The sed -i 's/hello/world/' file.txt982 command (the only command in the program) does not have a label, and thus simply restarts the cycle. On each cycle, the pattern space is printed and the next input line is read The following example is an infinite-loop - it doesn’t terminate and doesn’t print anything. The sed -i 's/hello/world/' file.txt982 command jumps to the ‘x’ label, and a new cycle is never started sed -i 's/hello/world/' file.txt048 Branching is often complemented with the sed -i 's/hello/world/' file.txt952 or sed -i 's/hello/world/' file.txt71 commands. both commands read the next input line into the pattern space without waiting for the cycle to restart. Trước khi đọc dòng đầu vào tiếp theo, sed -i 's/hello/world/' file.txt952 in không gian mẫu hiện tại sau đó làm trống nó, trong khi sed -i 's/hello/world/' file.txt71 nối một dòng mới và dòng đầu vào tiếp theo vào không gian mẫu Xét hai ví dụ sau sed -i 's/hello/world/' file.txt049
6. 4. 2 Ví dụ phân nhánh. đường nốiNhư một ví dụ thực tế về việc sử dụng phân nhánh, hãy xem xét trường hợp các tệp có thể in được trích dẫn, thường được sử dụng để mã hóa thư email. Trong các tệp này, các dòng dài được chia nhỏ và được đánh dấu bằng ngắt dòng mềm bao gồm một ký tự '=' duy nhất ở cuối dòng sed -i 's/hello/world/' file.txt001 Chương trình sau sử dụng đối sánh địa chỉ '/=$/' làm điều kiện. Nếu không gian mẫu hiện tại kết thúc bằng dấu '=', nó sẽ đọc dòng đầu vào tiếp theo bằng cách sử dụng sed -i 's/hello/world/' file.txt71, thay thế tất cả các ký tự '=' được theo sau bởi một dòng mới và phân nhánh vô điều kiện ( sed -i 's/hello/world/' file.txt982) đến đầu chương trình mà không cần khởi động lại một dòng mới . 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 -i 's/hello/world/' file.txt002 Here’s an alternative program with a slightly different approach. On all lines except the last, sed -i 's/hello/world/' file.txt71 appends the line to the pattern space. A substitution command then removes soft line breaks (‘=’ at the end of a line, i. e. followed by a newline) by replacing them with an empty string. if the substitution was successful (meaning the pattern space contained a line which should be joined), The conditional branch command sed -i 's/hello/world/' file.txt983 jumps to the beginning of the program without completing or restarting the cycle. If the substitution failed (meaning there were no soft line breaks), The sed -i 's/hello/world/' file.txt983 command will not branch. Then, sed -i 's/hello/world/' file.txt957 will print the pattern space content until the first newline, and sed -i 's/hello/world/' file.txt939 will delete the pattern space content until the first new line. (To learn more about sed -i 's/hello/world/' file.txt71, sed -i 's/hello/world/' file.txt957 and sed -i 's/hello/world/' file.txt939 commands see Multiline techniques) sed -i 's/hello/world/' file.txt003 Để biết thêm các ví dụ về nối dòng, hãy xem Nối các dòng 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 -i 's/hello/world/' file.txt939 and sed -i 's/hello/world/' file.txt957 commands to process multiple lines, and the sed -i 's/hello/world/' file.txt982 and sed -i 's/hello/world/' file.txt983 commands for branching. See Multiline techniques and Branching and flow control Join specific lines (e. g. if lines 2 and 3 need to be joined) sed -i 's/hello/world/' file.txt004 Join backslash-continued lines sed -i 's/hello/world/' file.txt005 Join lines that start with whitespace (e. g SMTP headers) sed -i 's/hello/world/' file.txt006 7. 2 Centering LinesThis script centers all lines of a file on a 80 columns width. To change that width, the number in sed -i 's/hello/world/' file.txt0439 must be replaced, and the number of added spaces also must be changed Note how the buffer commands are used to separate parts in the regular expressions to be matched—this is a common technique sed -i 's/hello/world/' file.txt007 sed -i 's/hello/world/' file.txt008 sed -i 's/hello/world/' file.txt009 sed -i 's/hello/world/' file.txt010 sed -i 's/hello/world/' file.txt011 sed -i 's/hello/world/' file.txt012 7. 3 Increment a 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,9 but must be done manually To increment one number you just add 1 to last digit, replacing it by the following digit. There is one exception. when the digit is a nine the previous digits must be also incremented until you don’t have a nine This solution by Bruno Haible is very clever and smart because it uses a single buffer; if you don’t have this limitation, the algorithm used in Numbering lines, is faster. It works by replacing trailing nines with an underscore, then using multiple sed -i 's/hello/world/' file.txt967 commands to increment the last digit, and then again substituting underscores with zeros sed -i 's/hello/world/' file.txt013 sed -i 's/hello/world/' file.txt014 sed -i 's/hello/world/' file.txt015 sed -i 's/hello/world/' file.txt016 sed -i 's/hello/world/' file.txt017 7. 4 Rename Files to Lower 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 -i 's/hello/world/' file.txt0444 into a sed -i 's/hello/world/' file.txt0445 program The main body of this is the sed -i 's/hello/world/' file.txt22 script, which remaps the name from lower to upper (or vice-versa) and even checks out if the remapped name is the same as the original name. Note how the script is parameterized using shell variables and proper quoting sed -i 's/hello/world/' file.txt018 sed -i 's/hello/world/' file.txt019 sed -i 's/hello/world/' file.txt020 sed -i 's/hello/world/' file.txt021 sed -i 's/hello/world/' file.txt022 sed -i 's/hello/world/' file.txt023 sed -i 's/hello/world/' file.txt024 sed -i 's/hello/world/' file.txt025 sed -i 's/hello/world/' file.txt026 sed -i 's/hello/world/' file.txt027 sed -i 's/hello/world/' file.txt028 sed -i 's/hello/world/' file.txt029 sed -i 's/hello/world/' file.txt030 sed -i 's/hello/world/' file.txt031 sed -i 's/hello/world/' file.txt032 sed -i 's/hello/world/' file.txt033 sed -i 's/hello/world/' file.txt034 sed -i 's/hello/world/' file.txt035 sed -i 's/hello/world/' file.txt036 sed -i 's/hello/world/' file.txt037 sed -i 's/hello/world/' file.txt038 sed -i 's/hello/world/' file.txt039 sed -i 's/hello/world/' file.txt040 7. 5 Print sed -i 's/hello/world/' file.txt 0447 EnvironmentThis script strips the definition of the shell functions from the output of the sed -i 's/hello/world/' file.txt0448 Bourne-shell command sed -i 's/hello/world/' file.txt041 sed -i 's/hello/world/' file.txt042 sed -i 's/hello/world/' file.txt043 sed -i 's/hello/world/' file.txt044 sed -i 's/hello/world/' file.txt045 sed -i 's/hello/world/' file.txt046 sed -i 's/hello/world/' file.txt047 7. 6 Reverse Characters of LinesThis script can be used to reverse the position of characters in lines. The technique moves two characters at a time, hence it is faster than more intuitive implementations Note the sed -i 's/hello/world/' file.txt0449 command before the definition of the label. This is often needed to reset the flag that is tested by the sed -i 's/hello/world/' file.txt983 command Imaginative readers will find uses for this script. An example is reversing the output of sed -i 's/hello/world/' file.txt0451. 10 sed -i 's/hello/world/' file.txt048 sed -i 's/hello/world/' file.txt049 sed -i 's/hello/world/' file.txt050 sed -i 's/hello/world/' file.txt051 7. 7 Text search across multiple linesThis section uses sed -i 's/hello/world/' file.txt71 and sed -i 's/hello/world/' file.txt939 commands to search for consecutive words spanning multiple lines. See Multiline techniques These examples deal with finding doubled occurrences of words in a document Finding doubled words in a single line is easy using GNU sed -i 's/hello/world/' file.txt687 and similarly with GNU sed -i 's/hello/world/' file.txt22 sed -i 's/hello/world/' file.txt052
When the doubled word span two lines the above regular expression will not find them as sed -i 's/hello/world/' file.txt687 and sed -i 's/hello/world/' file.txt22 operate line-by-line By using sed -i 's/hello/world/' file.txt71 and sed -i 's/hello/world/' file.txt939 commands, sed -i 's/hello/world/' file.txt22 can apply regular expressions on multiple lines (that is, multiple lines are stored in the pattern space, and the regular expression works on it) sed -i 's/hello/world/' file.txt053
See the GNU sed -i 's/hello/world/' file.txt0464 manual for an alternative solution using sed -i 's/hello/world/' file.txt0465 and sed -i 's/hello/world/' file.txt0466 at https. //gnu. org/s/coreutils/manual/html_node/Squeezing-and-deleting. html 7. 8 Line length adjustmentThis section uses sed -i 's/hello/world/' file.txt71 and sed -i 's/hello/world/' file.txt939 commands to search for consecutive words spanning multiple lines, and the sed -i 's/hello/world/' file.txt982 command for branching. See Multiline techniques and Branching and flow control This (somewhat contrived) example deal with formatting and wrapping lines of text of the following input file sed -i 's/hello/world/' file.txt054 The following sed program wraps lines at 40 characters sed -i 's/hello/world/' file.txt055 The wrapped output sed -i 's/hello/world/' file.txt056 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 -i 's/hello/world/' file.txt0470 workalike Note that on implementations other than GNU sed -i 's/hello/world/' file.txt22 this script might easily overflow internal buffers sed -i 's/hello/world/' file.txt057 sed -i 's/hello/world/' file.txt058 sed -i 's/hello/world/' file.txt059 sed -i 's/hello/world/' file.txt060 7. 10 dòng đánh sốTập lệnh này thay thế 'cat -n'; Tất nhiên điều này là hoàn toàn vô ích và vì hai lý do. thứ nhất, bởi vì ai đó khác đã làm nó trong C, thứ hai, bởi vì tập lệnh Bourne-shell sau đây có thể được sử dụng cho mục đích tương tự và sẽ nhanh hơn nhiều sed -i 's/hello/world/' file.txt061 Nó sử dụng sed -i 's/hello/world/' file.txt22 để in số dòng, sau đó nhóm hai dòng thành hai bằng cách sử dụng sed -i 's/hello/world/' file.txt71. Tất nhiên, bản kinh này không dạy nhiều như bản trình bày dưới đây Thuật toán được sử dụng để tăng dần sử dụng cả hai bộ đệm, vì vậy dòng được in càng sớm càng tốt và sau đó bị loại bỏ. Số được chia sao cho các chữ số thay đổi đi vào bộ đệm và những chữ số không thay đổi đi vào bộ đệm khác; . Số dòng cho dòng tiếp theo sau đó được soạn và lưu trữ trong không gian giữ, sẽ được sử dụng trong lần lặp tiếp theo sed -i 's/hello/world/' file.txt062 sed -i 's/hello/world/' file.txt063 sed -i 's/hello/world/' file.txt064 sed -i 's/hello/world/' file.txt065 sed -i 's/hello/world/' file.txt066 sed -i 's/hello/world/' file.txt067 sed -i 's/hello/world/' file.txt068 sed -i 's/hello/world/' file.txt069 sed -i 's/hello/world/' file.txt070 7. 11 Đánh số dòng không trốngMô phỏng 'cat -b' gần giống như 'cat -n'—chúng ta chỉ phải chọn dòng nào sẽ được đánh số và dòng nào không Phần chung của tập lệnh này và phần trước đó không được bình luận để cho thấy tầm quan trọng của việc bình luận đúng tập lệnh sed -i 's/hello/world/' file.txt22 ________ 1062 ________ 1072 ________ 1073 7. 12 số đếmKịch bản này cho thấy một cách khác để làm số học với 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) Cách tiếp cận là ánh xạ số thành chữ cái, một loại bàn tính được thực hiện với sed -i 's/hello/world/' file.txt22. 'a's là đơn vị, 'b's là hàng chục, v.v. chúng ta chỉ cần thêm số lượng ký tự trên dòng hiện tại dưới dạng đơn vị, sau đó chuyển số ký tự thành hàng chục, hàng trăm, v.v. Như thường lệ, tổng số chạy được giữ trong không gian giữ Ở dòng cuối cùng, chúng tôi chuyển đổi dạng bàn tính trở lại dạng thập phân. Để đa dạng, điều này được thực hiện bằng một vòng lặp chứ không phải bằng một số 80 lệnh sed -i 's/hello/world/' file.txt96711. đầu tiên, chúng tôi chuyển đổi các đơn vị, loại bỏ 'a' khỏi số; sed -i 's/hello/world/' file.txt062 sed -i 's/hello/world/' file.txt075 sed -i 's/hello/world/' file.txt076 sed -i 's/hello/world/' file.txt077 sed -i 's/hello/world/' file.txt078 sed -i 's/hello/world/' file.txt079 sed -i 's/hello/world/' file.txt080 7. 13 Đếm TừTập lệnh này gần giống như tập lệnh trước, khi mỗi từ trên dòng được chuyển thành một chữ 'a' (trong tập lệnh trước, mỗi chữ cái được đổi thành 'a') Điều thú vị là các chương trình sed -i 's/hello/world/' file.txt0480 thực có các vòng lặp được tối ưu hóa cho 'wc -c', vì vậy chúng đếm từ chậm hơn nhiều so với đếm ký tự. Thay vào đó, nút cổ chai của tập lệnh này là số học và do đó, phần đếm từ nhanh hơn (nó phải quản lý các số nhỏ hơn) Again, the common parts are not commented to show the importance of commenting sed -i 's/hello/world/' file.txt22 scripts sed -i 's/hello/world/' file.txt062 sed -i 's/hello/world/' file.txt082 sed -i 's/hello/world/' file.txt083 sed -i 's/hello/world/' file.txt084 7. 14 dòng đếmKhông có điều kỳ lạ nào được thực hiện ngay bây giờ, bởi vì sed -i 's/hello/world/' file.txt22 cung cấp cho chúng tôi chức năng 'wc -l' miễn phí. Nhìn 7. 15 In những dòng đầu tiênTập lệnh này có lẽ là tập lệnh sed -i 's/hello/world/' file.txt22 hữu ích đơn giản nhất. Nó hiển thị 10 dòng đầu vào; 7. 16 In những dòng cuối cùngIn n dòng cuối thay vì in dòng đầu tiên phức tạp hơn nhưng thực sự có thể. n được mã hóa ở dòng thứ hai, trước ký tự bang Tập lệnh này tương tự như tập lệnh sed -i 's/hello/world/' file.txt0470 ở chỗ nó giữ đầu ra cuối cùng trong không gian giữ và in nó ở cuối sed -i 's/hello/world/' file.txt062 sed -i 's/hello/world/' file.txt086 Về cơ bản, các tập lệnh giữ một cửa sổ gồm 10 dòng và trượt nó bằng cách thêm một dòng và xóa dòng cũ nhất (lệnh thay thế trên dòng thứ hai hoạt động giống như lệnh sed -i 's/hello/world/' file.txt939 nhưng không khởi động lại vòng lặp) Kỹ thuật “cửa sổ trượt” là một cách rất hiệu quả để viết các tập lệnh sed -i 's/hello/world/' file.txt22 hiệu quả và phức tạp, bởi vì các lệnh như sed -i 's/hello/world/' file.txt957 sẽ yêu cầu nhiều công việc nếu được triển khai thủ công Để giới thiệu kỹ thuật, được trình bày đầy đủ trong phần còn lại của chương này và dựa trên các lệnh sed -i 's/hello/world/' file.txt71, sed -i 's/hello/world/' file.txt957 và sed -i 's/hello/world/' file.txt939, đây là cách triển khai sed -i 's/hello/world/' file.txt0492 bằng cách sử dụng một “cửa sổ trượt” đơn giản. ” Điều này có vẻ phức tạp nhưng trên thực tế hoạt động giống như tập lệnh cuối cùng. after we have kicked in the appropriate number of lines, however, we stop using the hold space to keep inter-line state, and instead use sed -i 's/hello/world/' file.txt71 and sed -i 's/hello/world/' file.txt939 to slide pattern space by one line sed -i 's/hello/world/' file.txt007 sed -i 's/hello/world/' file.txt088 Note how the first, second and fourth line are inactive after the first ten lines of input. After that, all the script does is. exiting on the last line of input, appending the next input line to pattern space, and removing the first line 7. 17. Làm cho các dòng trùng lặp trở nên độc đáoĐây là một ví dụ về nghệ thuật sử dụng các lệnh sed -i 's/hello/world/' file.txt71, sed -i 's/hello/world/' file.txt957 và sed -i 's/hello/world/' file.txt939, có lẽ là lệnh khó thành thạo nhất sed -i 's/hello/world/' file.txt089 sed -i 's/hello/world/' file.txt090 sed -i 's/hello/world/' file.txt091 sed -i 's/hello/world/' file.txt092 Như bạn có thể thấy, chúng tôi duy trì cửa sổ 2 dòng bằng cách sử dụng sed -i 's/hello/world/' file.txt957 và sed -i 's/hello/world/' file.txt939. Kỹ thuật này thường được sử dụng trong các tập lệnh sed -i 's/hello/world/' file.txt22 nâng cao 7. 18 In các dòng đầu vào trùng lặpTập lệnh này chỉ in các dòng trùng lặp, như 'uniq -d' sed -i 's/hello/world/' file.txt062 sed -i 's/hello/world/' file.txt094 sed -i 's/hello/world/' file.txt095 sed -i 's/hello/world/' file.txt096 sed -i 's/hello/world/' file.txt097 7. 19 Xóa tất cả các dòng trùng lặpTập lệnh này chỉ in các dòng duy nhất, như 'uniq -u' sed -i 's/hello/world/' file.txt007 sed -i 's/hello/world/' file.txt099 sed -i 's/hello/world/' file.txt00 sed -i 's/hello/world/' file.txt01 sed -i 's/hello/world/' file.txt02 7. 20 Bóp dòng trốngNhư 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 Đầu tiên để lại một dòng trống ở đầu và cuối nếu đã có một số sed -i 's/hello/world/' file.txt007 sed -i 's/hello/world/' file.txt04 sed -i 's/hello/world/' file.txt05 Cái này phức tạp hơn một chút và loại bỏ tất cả các dòng trống ở đầu. Nó để lại một dòng trống ở cuối nếu có một dòng ở đó sed -i 's/hello/world/' file.txt007 sed -i 's/hello/world/' file.txt07 sed -i 's/hello/world/' file.txt08 Điều này loại bỏ các dòng trống đầu và cuối. Nó cũng là nhanh nhất. Lưu ý rằng các vòng lặp hoàn toàn được thực hiện với sed -i 's/hello/world/' file.txt952 và sed -i 's/hello/world/' file.txt982 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 -i 's/hello/world/' file.txt062 sed -i 's/hello/world/' file.txt10 sed -i 's/hello/world/' file.txt11 sed -i 's/hello/world/' file.txt12 sed -i 's/hello/world/' file.txt13 sed -i 's/hello/world/' file.txt14 8 Hạn chế và Không hạn chế của GNU sed -i 's/hello/world/' file.txt 22Đối với những người muốn viết tập lệnh sed -i 's/hello/world/' file.txt22 di động, hãy lưu ý rằng một số triển khai đã được biết là giới hạn độ dài dòng (đối với mẫu và khoảng trắng) không quá 4000 byte. Tiêu chuẩn POSIX chỉ định rằng việc triển khai sed -i 's/hello/world/' file.txt22 phù hợp sẽ hỗ trợ độ dài dòng ít nhất là 8192 byte. GNU sed -i 's/hello/world/' file.txt22 không có giới hạn tích hợp về độ dài dòng; Tuy nhiên, đệ quy được sử dụng để xử lý các mẫu con và lặp lại vô thời hạn. Điều này có nghĩa là không gian ngăn xếp có sẵn có thể giới hạn kích thước của bộ đệm có thể được xử lý theo một số mẫu nhất định 9 Các nguồn khác để tìm hiểu về sed -i 's/hello/world/' file.txt 22Để biết thông tin cập nhật về GNU sed -i 's/hello/world/' file.txt22, vui lòng truy cập https. //www. gnu. tổ chức/phần mềm/sed/ Gửi các câu hỏi và đề xuất chung tới sed-devel@gnu. tổ chức. Truy cập kho lưu trữ danh sách gửi thư cho các cuộc thảo luận trước đây tại https. //lists. gnu. org/archive/html/sed-devel/ Các tài nguyên sau đây cung cấp thông tin về sed -i 's/hello/world/' file.txt22 (cả GNU sed -i 's/hello/world/' file.txt22 và các biến thể khác). Lưu ý những điều này không được duy trì bởi các nhà phát triển GNU sed -i 's/hello/world/' file.txt22 10 lỗi báo cáoBáo cáo lỗi qua email tới bug-sed@gnu. tổ chức. Ngoài ra, vui lòng bao gồm đầu ra của 'sed --version' trong phần thân báo cáo của bạn nếu có thể Vui lòng không gửi báo cáo lỗi như thế này sed -i 's/hello/world/' file.txt15 Nếu GNU sed -i 's/hello/world/' file.txt22 không định cấu hình gói yêu thích của bạn, hãy dành thêm vài phút để xác định vấn đề cụ thể và tạo một trường hợp thử nghiệm độc lập. Không giống như các chương trình khác như trình biên dịch C, việc tạo các trường hợp thử nghiệm như vậy cho sed -i 's/hello/world/' file.txt22 khá đơn giản Một trường hợp thử nghiệm độc lập bao gồm tất cả dữ liệu cần thiết để thực hiện thử nghiệm và lệnh gọi cụ thể của sed -i 's/hello/world/' file.txt22 gây ra sự cố. Trường hợp kiểm thử độc lập càng nhỏ thì càng tốt. Một trường hợp thử nghiệm không nên liên quan đến thứ gì đó khác xa với sed -i 's/hello/world/' file.txt22 như “cố định cấu hình frobme-1. 3. 4”. Vâng, về nguyên tắc đó là đủ thông tin để tìm lỗi, nhưng đó không phải là một triển vọng thực tế Dưới đây là một vài lỗi thường được báo cáo không phải là lỗi Lệnhsed -i 's/hello/world/' file.txt71 ở dòng cuối cùng Hầu hết các phiên bản của sed -i 's/hello/world/' file.txt22 thoát mà không in bất kỳ thứ gì khi lệnh sed -i 's/hello/world/' file.txt71 được đưa ra ở dòng cuối cùng của tệp. GNU sed -i 's/hello/world/' file.txt22 in không gian mẫu trước khi thoát, tất nhiên trừ khi công tắc lệnh sed -i 's/hello/world/' file.txt45 đã được chỉ định. Sự lựa chọn này là do thiết kế Hành vi mặc định (tiện ích mở rộng gnu, không tuân thủ POSIX) Để buộc hành vi tuân thủ POSIX sed -i 's/hello/world/' file.txt16 Ví dụ, hành vi của sẽ phụ thuộc vào việc foo có số dòng chẵn hay lẻ12. Hoặc, khi viết một tập lệnh để đọc một vài dòng tiếp theo sau khi khớp mẫu, việc triển khai truyền thống của sed -i 's/hello/world/' file.txt22 sẽ buộc bạn phải viết một cái gì đó như sed -i 's/hello/world/' file.txt17 Thay vì chỉ sed -i 's/hello/world/' file.txt18 Trong mọi trường hợp, cách giải quyết đơn giản nhất là sử dụng sed -i 's/hello/world/' file.txt0024 trong các tập lệnh dựa trên hành vi truyền thống hoặc đặt biến sed -i 's/hello/world/' file.txt72 thành một giá trị không trốngXung đột cú pháp Regex (các vấn đề với dấu gạch chéo ngược) sed -i 's/hello/world/' file.txt22 sử dụng cú pháp biểu thức chính quy cơ bản POSIX. According to the standard, the meaning of some escape sequences is undefined in this syntax; notable in the case of sed -i 's/hello/world/' file.txt22 are sed -i 's/hello/world/' file.txt911, sed -i 's/hello/world/' file.txt923, sed -i 's/hello/world/' file.txt925, sed -i 's/hello/world/' file.txt156, sed -i 's/hello/world/' file.txt157, sed -i 's/hello/world/' file.txt991, sed -i 's/hello/world/' file.txt992, sed -i 's/hello/world/' file.txt987, sed -i 's/hello/world/' file.txt988, sed -i 's/hello/world/' file.txt985, and sed -i 's/hello/world/' file.txt986 As in all GNU programs that use POSIX basic regular expressions, sed -i 's/hello/world/' file.txt22 interprets these escape sequences as special characters. Vì vậy, sed -i 's/hello/world/' file.txt0040 khớp với một hoặc nhiều lần xuất hiện của 'x'. sed -i 's/hello/world/' file.txt0041 khớp với 'abc' hoặc 'def' Cú pháp này có thể gây ra sự cố khi chạy tập lệnh được viết cho các sed -i 's/hello/world/' file.txt22 khác. Một số chương trình sed -i 's/hello/world/' file.txt22 đã được viết với giả định rằng sed -i 's/hello/world/' file.txt911 và sed -i 's/hello/world/' file.txt923 khớp với các ký tự chữ sed -i 's/hello/world/' file.txt0046 và sed -i 's/hello/world/' file.txt0047. Các tập lệnh như vậy phải được sửa đổi bằng cách xóa các dấu gạch chéo ngược giả nếu chúng được sử dụng với các triển khai hiện đại của sed -i 's/hello/world/' file.txt22, như GNU sed -i 's/hello/world/' file.txt22 Mặt khác, một số tập lệnh sử dụng s. abc\. chắc chắn. g để loại bỏ các lần xuất hiện của sed -i 's/hello/world/' file.txt0050 hoặc sed -i 's/hello/world/' file.txt0051. Trong khi điều này làm việc cho đến khi sed -i 's/hello/world/' file.txt22 4. 0. x, các phiên bản mới hơn hiểu điều này là loại bỏ chuỗi sed -i 's/hello/world/' file.txt0053. Đây lại là hành vi không xác định theo POSIX và cách giải thích này được cho là mạnh mẽ hơn. Ví dụ: các sed -i 's/hello/world/' file.txt22 cũ hơn yêu cầu trình so khớp regex phân tích cú pháp sed -i 's/hello/world/' file.txt0055 thành sed -i 's/hello/world/' file.txt994 trong trường hợp phổ biến là thoát khỏi dấu gạch chéo, đây lại là hành vi không xác định; Ngoài ra, phiên bản sed -i 's/hello/world/' file.txt22 này hỗ trợ một số ký tự thoát (một số ký tự có nhiều ký tự) để chèn các ký tự không in được vào tập lệnh ( sed 's/hello/world/' input.txt > output.txt902, sed -i 's/hello/world/' file.txt0059, sed -i 's/hello/world/' file.txt0060, sed -i 's/hello/world/' file.txt0061, sed 's/hello/world/' input.txt > output.txt905, sed -i 's/hello/world/' file.txt953, sed 's/hello/world/' input.txt > output.txt907, sed -i 's/hello/world/' file.txt0065). Những thứ này có thể gây ra sự cố tương tự với các tập lệnh được viết cho các sed -i 's/hello/world/' file.txt22 khác-i chặn các tệp chỉ đọc Nói tóm lại, 'sed -i' sẽ cho phép bạn xóa nội dung của tệp chỉ đọc và nói chung, tùy chọn -i (xem Yêu cầu) cho phép bạn ghi đè các tệp được bảo vệ. Đây không phải là lỗi, mà là hậu quả của cách thức hoạt động của hệ thống tệp Unix Các quyền trên một tệp cho biết điều gì có thể xảy ra với dữ liệu trong tệp đó, trong khi các quyền trên một thư mục cho biết điều gì có thể xảy ra với danh sách các tệp trong thư mục đó. ‘sed -i’ will not ever open for writing a file that is already on disk. Thay vào đó, nó sẽ hoạt động trên một tệp tạm thời cuối cùng được đổi tên thành tên gốc. nếu bạn đổi tên hoặc xóa tệp, thực ra bạn đang sửa đổi nội dung của thư mục, vì vậy hoạt động phụ thuộc vào quyền của thư mục chứ không phải của tệp. Cũng vì lý do này, sed -i 's/hello/world/' file.txt22 không cho phép bạn sử dụng -i trên tệp có thể ghi trong thư mục chỉ đọc và sẽ phá vỡ liên kết cứng hoặc liên kết tượng trưng khi -i được sử dụng trên tệp đó sed -i 's/hello/world/' file.txt0068 không hoạt động (có lỗi) Không có dòng 0. 0 là một địa chỉ đặc biệt chỉ được sử dụng để coi các địa chỉ như sed -i 's/hello/world/' file.txt0069 là hoạt động khi tập lệnh bắt đầu. nếu bạn viết sed -i 's/hello/world/' file.txt0070 và dòng đầu tiên bao gồm từ 'abc', thì kết quả khớp đó sẽ bị bỏ qua vì phạm vi địa chỉ phải kéo dài ít nhất hai dòng (trừ phần cuối của tệp); sed -i 's/hello/world/' file.txt0072 không phân biệt chữ hoa chữ thường Bạn đang gặp sự cố với ngôn ngữ. POSIX bắt buộc rằng sed -i 's/hello/world/' file.txt0072 sử dụng thứ tự đối chiếu của ngôn ngữ hiện tại - theo cách nói của C, điều đó có nghĩa là sử dụng sed -i 's/hello/world/' file.txt0074 thay vì sed -i 's/hello/world/' file.txt0075. Một số ngôn ngữ có thứ tự đối chiếu không phân biệt chữ hoa chữ thường, những ngôn ngữ khác thì không Một vấn đề khác là sed -i 's/hello/world/' file.txt0072 cố gắng sử dụng các ký hiệu đối chiếu. Điều này chỉ xảy ra nếu bạn đang sử dụng hệ thống GNU, sử dụng trình so khớp biểu thức chính quy của GNU libc thay vì biên dịch cái được cung cấp cùng với GNU sed. Ví dụ: trong một ngôn ngữ Đan Mạch, biểu thức chính quy sed -i 's/hello/world/' file.txt0077 khớp với chuỗi 'aa', bởi vì đây là một ký hiệu đối chiếu duy nhất xuất hiện sau 'a' và trước 'b'; Để khắc phục những sự cố này, vốn có thể gây ra lỗi trong tập lệnh shell, hãy đặt biến môi trường sed -i 's/hello/world/' file.txt0078 và sed 's/hello/world/' input.txt > output.txt946 thành 'C' sed 's/hello/world/' input.txt > output.txt926 không xóa không gian mẫu Điều này xảy ra nếu luồng đầu vào của bạn bao gồm các chuỗi nhiều byte không hợp lệ. POSIX mandates that such sequences are not matched by ‘. ’, do đó ‘s/. *//' sẽ không xóa không gian mẫu như bạn mong đợi. Trên thực tế, không có cách nào để xóa bộ đệm của sed ở 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). Vì lý do này, GNU sed -i 's/hello/world/' file.txt22 cung cấp lệnh 'z' (cho 'zap') dưới dạng tiện ích mở rộng Để khắc phục những sự cố này, vốn có thể gây ra lỗi trong tập lệnh shell, hãy đặt biến môi trường sed -i 's/hello/world/' file.txt0078 và sed 's/hello/world/' input.txt > output.txt946 thành 'C' Phụ lục A Giấy phép Tài liệu Miễn phí GNUPhiên bản 1. 3, 3 tháng 11 năm 2008 sed -i 's/hello/world/' file.txt19
PHỤ LỤC. Cách sử dụng Giấy phép này cho tài liệu của bạnĐể sử dụng Giấy phép này trong tài liệu bạn đã viết, hãy đưa một bản sao của Giấy phép vào tài liệu và đặt thông báo bản quyền và giấy phép sau đây ngay sau trang tiêu đề sed -i 's/hello/world/' file.txt20 Nếu bạn có Phần bất biến, Văn bản bìa trước và Văn bản bìa sau, hãy thay thế “bằng…Văn bản. ” 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 |