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”
1. Giới thiệu
sed -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 sed
Chươ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. Sơ lượt
Thô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ố; . This allows you to add a prefix to the backup file, instead of [or in addition to] a suffix, or even to place backup copies of the original files into another directory [provided the directory already exists]
Nếu không có phần mở rộng nào được cung cấp, tệp gốc sẽ bị ghi đè mà không tạo bản sao lưu
Bởi vì -i lấy một đối số tùy chọn, nó không nên được theo sau bởi các tùy chọn ngắn khác
sed -i 's/hello/world/' file.txt62
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
Điều này tương đương với --in-place=E, tạo FILES làm bản sao lưu của FILE
Hãy thận trọng khi sử dụng -n với -i. cái trước tắt tự động in các dòng và cái sau thay đổi tệp tại chỗ mà không cần sao lưu. Được sử dụng bất cẩn [và không có lệnh
sed -i 's/hello/world/' file.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
Chỉ định độ dài ngắt dòng mặc định cho lệnh
sed -i 's/hello/world/' file.txt67. Độ 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. Để đơn giản hóa việc viết các tập lệnh di động, tùy chọn này sẽ vô hiệu hóa tất cả các tiện ích mở rộng mà tài liệu hướng dẫn sử dụng này, bao gồm cả các lệnh bổ sung. Hầu hết các tiện ích mở rộng đều chấp nhận các chương trình
sed -i 's/hello/world/' file.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. Nếu bạn chỉ muốn tắt loại tiện ích mở rộng thứ hai, bạn có thể đặt biến
sed -i 's/hello/world/' file.txt72 thành một giá trị không trống
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. Khi có sự phân biệt như vậy—như trường hợp của MS-DOS, Windows, Cygwin—các tệp văn bản bao gồm các dòng được phân tách bằng ký tự xuống dòng và ký tự nguồn cấp dữ liệu, và
sed -i 's/hello/world/' file.txt22 không thấy CR kết thúc. 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
Sử dụng các biểu thức chính quy mở rộng thay vì các biểu thức chính quy cơ bản. Biểu thức chính quy mở rộng là những biểu thức mà
sed -i 's/hello/world/' file.txt82 chấp nhận; . Về mặt lịch sử, đây là một 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 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_______092____093
Bộ đệm cả đầu vào và đầu ra ở mức tối thiểu nhất có thể. [Điều này đặc biệt hữu ích nếu đầu vào đến từ những thứ như 'tail -f' và bạn muốn xem đầu ra được chuyển đổi càng sớm càng tốt. ]
sed -i 's/hello/world/' file.txt94
sed -i 's/hello/world/' file.txt95
sed -i 's/hello/world/' file.txt96
Coi đầu vào là một tập hợp các dòng, mỗi dòng được kết thúc bằng một byte 0 [ký tự ASCII 'NUL'] thay vì một dòng mới. Tùy chọn này có thể được sử dụng với các lệnh như ‘sort -z’ và ‘find -print0’ để xử lý các tên tệp tùy ý
Nếu không có tùy chọn -e, -f, --expression hoặc --file nào được cung cấp trên dòng lệnh, thì đối số không phải tùy chọn đầu tiên trên dòng lệnh sẽ được coi là tập lệnh sẽ được thực thi
Nếu bất kỳ tham số dòng lệnh nào vẫn còn sau khi xử lý ở trên, các tham số này được hiểu là tên của tệp đầu vào sẽ được xử lý. Tên tệp '-' đề cập đến luồng đầu vào tiêu chuẩn. Đầu vào tiêu chuẩn sẽ được xử lý nếu không có tên tệp nào được chỉ định
2. 3 Exit status
Trạng thái thoát bằng 0 cho biết thành công và giá trị khác không cho biết thất bại. GNU
sed -i 's/hello/world/' file.txt22 trả về các giá trị lỗi trạng thái thoát sau0
Hoàn thành thành công
1Lệnh không hợp lệ, cú pháp không hợp lệ, biểu thức chính quy không hợp lệ hoặc lệnh mở rộng GNU
sed -i 's/hello/world/' file.txt22 được sử dụng với --posix2
Không thể mở một hoặc nhiều tệp đầu vào được chỉ định trên dòng lệnh [e. g. nếu không tìm thấy tệp hoặc quyền đọc bị từ chối]. Xử lý tiếp tục với các tập tin khác
4Lỗi I/O hoặc lỗi xử lý nghiêm trọng trong thời gian chạy, GNU
sed -i 's/hello/world/' file.txt22 bị hủy bỏ ngay lập tức
Ngoài ra, các lệnh
sed -i 's/hello/world/' file.txt900 và
sed -i 's/hello/world/' file.txt901 có thể được sử dụng để chấm dứt
sed -i 's/hello/world/' file.txt22 với giá trị mã thoát tùy chỉnh [đây là tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22]
sed 's/hello/world/' input.txt > output.txt9
3 tập lệnh sed -i 's/hello/world/' file.txt
22
3. 1 Tổng quan kịch bản sed -i 's/hello/world/' file.txt
22
Một chương trình
sed -i 's/hello/world/' file.txt22 bao gồm một hoặc nhiều lệnh
sed -i 's/hello/world/' file.txt22, được truyền vào bởi một hoặc nhiều tùy chọn -e, -f, --expression và --file hoặc đối số không phải tùy chọn đầu tiên nếu không sử dụng các tùy chọn này. Tài liệu này sẽ đề cập đến “tập lệnh”
sed -i 's/hello/world/' file.txt22; . Xem Tổng quan
sed -i 's/hello/world/' file.txt22 lệnh theo cú pháp này
X là lệnh một ký tự
sed -i 's/hello/world/' file.txt22.
sed -i 's/hello/world/' file.txt911 là một địa chỉ dòng tùy chọn. Nếu
sed -i 's/hello/world/' file.txt911 được chỉ định, lệnh X sẽ chỉ được thực hiện trên các dòng phù hợp.
sed -i 's/hello/world/' file.txt911 có thể là số dòng đơn, biểu thức chính quy hoặc dải dòng [xem địa chỉ sed].
sed -i 's/hello/world/' file.txt914 bổ sung được sử dụng cho một số lệnh
sed -i 's/hello/world/' file.txt22
Ví dụ sau xóa dòng 30 đến 35 trong đầu vào.
sed -i 's/hello/world/' file.txt916 là một dải địa chỉ.
sed -i 's/hello/world/' file.txt36 là lệnh xóa
sed -i 's/hello/world/' file.txt04
Ví dụ sau in tất cả đầu vào cho đến khi tìm thấy một dòng bắt đầu bằng từ 'foo'. Nếu dòng đó được tìm thấy,
sed -i 's/hello/world/' file.txt22 sẽ kết thúc với trạng thái thoát 42. Nếu dòng đó không được tìm thấy [và không có lỗi nào khác xảy ra],
sed -i 's/hello/world/' file.txt22 sẽ thoát với trạng thái 0.
sed -i 's/hello/world/' file.txt920 là một địa chỉ biểu thức chính quy.
sed -i 's/hello/world/' file.txt900 là lệnh thoát.
sed -i 's/hello/world/' file.txt922 là tùy chọn lệnh
sed -i 's/hello/world/' file.txt00
Các lệnh trong tập lệnh hoặc tệp tập lệnh có thể được phân tách bằng dấu chấm phẩy [
sed -i 's/hello/world/' file.txt923] hoặc dòng mới [ASCII 10]. Nhiều tập lệnh có thể được chỉ định với các tùy chọn -e hoặc -f
Các ví dụ sau đây đều tương đương. Họ thực hiện hai hoạt động
sed -i 's/hello/world/' file.txt22. xóa bất kỳ dòng nào khớp với biểu thức chính quy
sed -i 's/hello/world/' file.txt920 và thay thế tất cả các lần xuất hiện của chuỗi 'xin chào' bằng 'thế giới'
sed -i 's/hello/world/' file.txt01
Các lệnh
sed -i 's/hello/world/' file.txt926,
sed -i 's/hello/world/' file.txt927,
sed -i 's/hello/world/' file.txt928, do cú pháp của chúng, không thể được theo sau bởi dấu chấm phẩy hoạt động như dấu phân cách lệnh và do đó nên được kết thúc bằng dòng mới hoặc được đặt ở cuối tập lệnh hoặc tệp tập lệnh. Các lệnh cũng có thể được đặt trước các ký tự khoảng trắng không quan trọng tùy chọn. Xem Cú pháp nhiều lệnh
3. 2 sed -i 's/hello/world/' file.txt
22 lệnh tóm tắt
Các lệnh sau được hỗ trợ trong GNU
sed -i 's/hello/world/' file.txt22. Một số là các lệnh POSIX tiêu chuẩn, trong khi một số khác là các phần mở rộng của GNU. Chi tiết và ví dụ cho từng lệnh có trong các phần sau. [Mnemonics] được hiển thị trong ngoặc đơn
sed -i 's/hello/world/' file.txt931
sed -i 's/hello/world/' file.txt932
Nối văn bản sau một dòng
sed -i 's/hello/world/' file.txt933
Nối văn bản sau một dòng [cú pháp thay thế]
sed -i 's/hello/world/' file.txt934
Chi nhánh vô điều kiện để dán nhãn. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu
sed -i 's/hello/world/' file.txt935
sed -i 's/hello/world/' file.txt932
Thay thế [thay đổi] dòng bằng văn bản
sed -i 's/hello/world/' file.txt937
Thay thế [thay đổi] dòng bằng văn bản [cú pháp thay thế]
sed -i 's/hello/world/' file.txt36
Xóa không gian mẫu;
sed -i 's/hello/world/' file.txt939
Nếu không gian mẫu chứa các dòng mới, hãy xóa văn bản trong không gian mẫu cho đến dòng mới đầu tiên và bắt đầu lại chu kỳ với không gian mẫu kết quả mà không đọc dòng đầu vào mới
Nếu không gian mẫu không chứa dòng mới, hãy bắt đầu một chu kỳ mới bình thường như thể lệnh
sed -i 's/hello/world/' file.txt36 đã được ban hành
sed -i 's/hello/world/' file.txt941
Thực thi lệnh được tìm thấy trong không gian mẫu và thay thế không gian mẫu bằng đầu ra;
sed -i 's/hello/world/' file.txt942
Thực thi lệnh và gửi đầu ra của nó tới luồng đầu ra. Lệnh có thể chạy trên nhiều dòng, tất cả trừ dòng cuối cùng có dấu gạch chéo ngược
sed -i 's/hello/world/' file.txt943
[tên tệp] In tên tệp của tệp đầu vào hiện tại [với một dòng mới ở cuối]
sed -i 's/hello/world/' file.txt944
Thay thế nội dung của không gian mẫu bằng nội dung của không gian giữ
sed -i 's/hello/world/' file.txt945
Nối một dòng mới vào nội dung của không gian mẫu, rồi nối thêm nội dung của không gian giữ vào nội dung của không gian mẫu
sed -i 's/hello/world/' file.txt946
[giữ] Thay thế nội dung của không gian giữ bằng nội dung của không gian mẫu
sed -i 's/hello/world/' file.txt947
Nối một dòng mới vào nội dung của không gian giữ, sau đó nối thêm nội dung của không gian mẫu vào nội dung của không gian giữ
sed -i 's/hello/world/' file.txt948
sed -i 's/hello/world/' file.txt932
chèn văn bản trước một dòng
sed -i 's/hello/world/' file.txt950
chèn văn bản trước một dòng [cú pháp thay thế]
sed -i 's/hello/world/' file.txt67
In không gian mẫu ở dạng rõ ràng
sed -i 's/hello/world/' file.txt952
[tiếp theo] Nếu tính năng tự động in không bị tắt, hãy in không gian mẫu, sau đó, bất kể, thay thế không gian mẫu bằng dòng nhập tiếp theo. Nếu không có thêm đầ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
sed -i 's/hello/world/' file.txt71
Thêm một dòng mới vào không gian mẫu, sau đó nối dòng đầu vào tiếp theo vào không gian mẫu. Nếu không có thêm đầ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
sed -i 's/hello/world/' file.txt37
In không gian mẫu
sed -i 's/hello/world/' file.txt957
In không gian mẫu, cho đến mẫu đầu tiên
sed -i 's/hello/world/' file.txt958
[thoát] Thoát
sed -i 's/hello/world/' file.txt22 mà không xử lý thêm bất kỳ lệnh hoặc đầu vào nào
sed -i 's/hello/world/' file.txt960
[thoát] Lệnh này giống như lệnh
sed -i 's/hello/world/' file.txt900, nhưng sẽ không in nội dung của không gian mẫu. Giống như
sed -i 's/hello/world/' file.txt900, nó cung cấp khả năng trả lại mã thoát cho người gọi
sed -i 's/hello/world/' file.txt963
Đọc tên tập tin
sed -i 's/hello/world/' file.txt964
Xếp hàng một dòng tên tệp sẽ được đọc và chèn vào luồng đầu ra ở cuối chu kỳ hiện tại hoặc khi dòng đầu vào tiếp theo được đọc
sed -i 's/hello/world/' file.txt965
[thay thế] Khớp biểu thức chính quy với nội dung của không gian mẫu. Nếu tìm thấy, hãy thay thế chuỗi phù hợp bằng chuỗi thay thế
sed -i 's/hello/world/' file.txt966
[thử nghiệm] Chỉ nhánh để gắn nhãn nếu đã có một sự thay thế
sed -i 's/hello/world/' file.txt967 thành công kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện được thực hiện. The label may be omitted, in which case the next cycle is started
sed -i 's/hello/world/' file.txt968
[thử nghiệm] Chỉ nhánh để gắn nhãn nếu không có thay thế
sed -i 's/hello/world/' file.txt967 thành công nào kể từ khi dòng đầu vào cuối cùng được đọc hoặc nhánh có điều kiện được thực hiện. Nhãn có thể bị bỏ qua, trong trường hợp đó, chu kỳ tiếp theo được bắt đầu
sed -i 's/hello/world/' file.txt970
[phiên bản] Lệnh này không làm gì, nhưng làm cho
sed -i 's/hello/world/' file.txt22 không thành công nếu tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22 không được hỗ trợ hoặc nếu phiên bản được yêu cầu không có sẵn
sed -i 's/hello/world/' file.txt973
Viết không gian mẫu vào tên tệp
sed -i 's/hello/world/' file.txt974
Ghi vào tên tệp đã cho phần không gian mẫu cho đến dòng mới đầu tiên
sed -i 's/hello/world/' file.txt975
Trao đổi nội dung của không gian giữ và mẫu
sed -i 's/hello/world/' file.txt976
Chuyển ngữ bất kỳ ký tự nào trong không gian mẫu khớp với bất kỳ ký tự nguồn nào với ký tự tương ứng trong ký tự đích
sed -i 's/hello/world/' file.txt977
[zap] Lệnh này làm trống nội dung của không gian mẫu
sed -i 's/hello/world/' file.txt978
Một nhận xét, cho đến dòng mới tiếp theo
sed -i 's/hello/world/' file.txt979
Nhóm một số lệnh lại với nhau
sed -i 's/hello/world/' file.txt980
In số dòng đầu vào hiện tại [với một dòng mới ở cuối]
sed -i 's/hello/world/' file.txt981
Chỉ định vị trí của nhãn cho các lệnh nhánh [______1982,
sed -i 's/hello/world/' file.txt983,
sed -i 's/hello/world/' file.txt984]
3. 3 Lệnh sed -i 's/hello/world/' file.txt
967
Lệnh
sed -i 's/hello/world/' file.txt967 [để thay thế] có lẽ là lệnh quan trọng nhất trong
sed -i 's/hello/world/' file.txt22 và có rất nhiều tùy chọn khác nhau. Cú pháp của lệnh
sed -i 's/hello/world/' file.txt967 là ‘s/regexp/replacement/flags’
Khái niệm cơ bản của nó là đơn giản. lệnh
sed -i 's/hello/world/' file.txt967 cố gắng khớp không gian mẫu với biểu thức chính quy được cung cấp;
Để biết chi tiết về cú pháp biểu thức chính quy, hãy xem Địa chỉ biểu thức chính quy
Phần thay thế có thể chứa các tham chiếu
sed -i 's/hello/world/' file.txt990 [n là một số từ 1 đến 9], tham chiếu đến phần trùng khớp được chứa giữa
sed -i 's/hello/world/' file.txt991 thứ n và
sed -i 's/hello/world/' file.txt992 trùng khớp của nó. Ngoài ra, phần thay thế có thể chứa các ký tự
sed -i 's/hello/world/' file.txt993 chưa thoát tham chiếu đến toàn bộ phần khớp của không gian mẫu
Các ký tự
sed -i 's/hello/world/' file.txt994 có thể được thay thế thống nhất bằng bất kỳ ký tự đơn nào khác trong bất kỳ lệnh
sed -i 's/hello/world/' file.txt967 nào. Ký tự
sed -i 's/hello/world/' file.txt994 [hoặc bất kỳ ký tự nào khác được sử dụng thay cho nó] chỉ có thể xuất hiện trong biểu thức chính quy hoặc thay thế nếu nó đứng trước ký tự
sed -i 's/hello/world/' file.txt997
Cuối cùng, với tư cách là tiện ích mở rộng GNU
sed -i 's/hello/world/' file.txt22, bạn có thể bao gồm một chuỗi đặc biệt được tạo bằng dấu gạch chéo ngược và một trong các chữ cái
sed -i 's/hello/world/' file.txt999,
sed -i 's/hello/world/' file.txt67,
sed -i 's/hello/world/' file.txt101,
sed -i 's/hello/world/' file.txt102 hoặc
sed -i 's/hello/world/' file.txt103. Ý nghĩa như sau
sed -i 's/hello/world/' file.txt104
Biến thay thế thành chữ thường cho đến khi tìm thấy
sed -i 's/hello/world/' file.txt105 hoặc
sed -i 's/hello/world/' file.txt106,
sed -i 's/hello/world/' file.txt107
Biến ký tự tiếp theo thành chữ thường,
sed -i 's/hello/world/' file.txt105
Chuyển từ thay thế sang chữ hoa cho đến khi tìm thấy
sed -i 's/hello/world/' file.txt104 hoặc
sed -i 's/hello/world/' file.txt106,
sed -i 's/hello/world/' file.txt111
Chuyển ký tự tiếp theo thành chữ hoa,
sed -i 's/hello/world/' file.txt106
Dừng chuyển đổi trường hợp bắt đầu bởi
sed -i 's/hello/world/' file.txt104 hoặc
sed -i 's/hello/world/' file.txt105
Khi cờ
sed -i 's/hello/world/' file.txt944 đang được sử dụng, chuyển đổi trường hợp không lan truyền từ một lần xuất hiện của biểu thức chính quy sang một lần khác. Ví dụ: khi lệnh sau được thực thi với 'a-b-' trong không gian mẫu
đầu ra là 'axxB'. Khi thay thế '-' đầu tiên, chuỗi '\ u' chỉ ảnh hưởng đến thay thế trống của '\ 1'. Nó không ảnh hưởng đến ký tự
sed -i 's/hello/world/' file.txt975 được thêm vào không gian mẫu khi thay thế
sed -i 's/hello/world/' file.txt117 bằng
sed -i 's/hello/world/' file.txt118
Mặt khác,
sed -i 's/hello/world/' file.txt107 và
sed -i 's/hello/world/' file.txt111 ảnh hưởng đến phần còn lại của văn bản thay thế nếu theo sau chúng là một thay thế trống. Với 'a-b-' trong không gian mẫu, lệnh sau
sẽ thay thế '-' bằng 'X' [chữ hoa] và 'b-' bằng 'Bx'. Nếu hành vi này là không mong muốn, bạn có thể ngăn chặn nó bằng cách thêm một chuỗi '\E'—sau '\1' trong trường hợp này
Để bao gồm một
sed -i 's/hello/world/' file.txt997,
sed -i 's/hello/world/' file.txt993 hoặc dòng mới trong phần thay thế cuối cùng, hãy đảm bảo đặt trước
sed -i 's/hello/world/' file.txt997,
sed -i 's/hello/world/' file.txt993 hoặc dòng mới mong muốn trong phần thay thế bằng một
sed -i 's/hello/world/' file.txt997
Lệnh
sed -i 's/hello/world/' file.txt967 có thể được theo sau bởi 0 hoặc nhiều cờ sau
sed -i 's/hello/world/' file.txt944
Apply the replacement to all matches to the regexp, not just the first
sed -i 's/hello/world/' file.txt128
Chỉ thay thế trận đấu số của regrec
tương tác trong lệnh
sed -i 's/hello/world/' file.txt967 Lưu ý. tiêu chuẩn POSIX không chỉ định điều gì sẽ xảy ra khi bạn kết hợp các công cụ sửa đổi số và số
sed -i 's/hello/world/' file.txt944 và hiện tại không có ý nghĩa thống nhất rộng rãi trên các triển khai
sed -i 's/hello/world/' file.txt22. Đố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ùng
Nếu bạn hoàn toàn sử dụng
sed -i 's/hello/world/' file.txt22, rất có thể bạn sẽ muốn biết các lệnh này
sed -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
Thí dụ. dừng lại sau khi in dòng thứ hai
Lệnh này chỉ chấp nhận một địa chỉ. Lưu ý rằng không gian mẫu hiện tại được in nếu tính năng in tự động không bị tắt với tùy chọn -n. Khả năng trả lại mã thoát từ tập lệnh
sed -i 's/hello/world/' file.txt22 là 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
Xóa không gian mẫu;
Thí dụ. xóa dòng đầu vào thứ hai
sed -i 's/hello/world/' file.txt37
In ra không gian mẫu [đến đầu ra tiêu chuẩn]. Lệnh này thường chỉ được sử dụng cùng với tùy chọn dòng lệnh -n
Thí dụ. chỉ in dòng đầu vào thứ hai
sed -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]
Thí dụ. thực hiện thay thế trên mỗi dòng thứ 3 [i. e. two
sed -i 's/hello/world/' file.txt952 commands skip two lines]
sed -i 's/hello/world/' file.txt02
GNU
sed -i 's/hello/world/' file.txt22 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
Thí 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ơn
Mặ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
Chuyển ngữ bất kỳ ký tự nào trong không gian mẫu khớp với bất kỳ ký tự nguồn nào với ký tự tương ứng trong ký tự đích
Thí dụ. phiên âm 'a-j' thành '0-9'
sed -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
Thí 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
Thí 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ỉ
Trình tự thoát trong văn bản được xử lý, vì vậy bạn nên sử dụng
sed -i 's/hello/world/' file.txt195 trong văn bản để in một dấu gạch chéo ngược
Các lệnh tiếp tục sau dòng cuối cùng mà không có dấu gạch chéo ngược [
sed -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
Thí dụ. Chèn từ 'xin chào' trước dòng thứ hai
sed -i 's/hello/world/' file.txt0
Khoảng trắng hàng đầu sau lệnh
sed -i 's/hello/world/' file.txt928 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.txt948
sed -i 's/hello/world/' file.txt932
Xuất ngay các dòng văn bản theo lệnh này
Thí dụ. Chèn 'xin chào' trước dòng thứ hai [-. cho biết các dòng đầu ra được in]
sed -i 's/hello/world/' file.txt1
Là một phần mở rộng GNU, lệnh này chấp nhận hai địa chỉ
Trình tự thoát trong văn bản được xử lý, vì vậy bạn nên sử dụng
sed -i 's/hello/world/' file.txt195 trong văn bản để in một dấu gạch chéo ngược
Các lệnh tiếp tục sau dòng cuối cùng mà không có dấu gạch chéo ngược [
sed -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
Thay thế [các] dòng bằng văn bản. Đâ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
Thí dụ. Replace the 2nd to 9th lines with the word ‘hello’
sed -i 's/hello/world/' file.txt4
Khoảng trắng hàng đầu sau lệnh
sed -i 's/hello/world/' file.txt927 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.txt935
sed -i 's/hello/world/' file.txt932
Delete the lines matching the address or address-range, and output the lines of text which follow this command
Thí dụ. Thay thế dòng thứ 2 đến thứ 4 bằng các từ 'xin chào' và 'thế giới' [-. cho biết các dòng đầu ra được in]
sed -i 's/hello/world/' file.txt5
Nếu không có địa chỉ nào được cung cấp, mỗi dòng sẽ được thay thế
Một chu kỳ mới được bắt đầu sau khi lệnh này được thực hiện, vì không gian mẫu sẽ bị xóa. Trong ví dụ sau,
sed -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
Print the pattern space in an unambiguous form. non-printable characters [and the
sed -i 's/hello/world/' file.txt997 character] are printed in C-style escaped form; long lines are split, with a trailing
sed -i 's/hello/world/' file.txt997 character to indicate the split; the end of each line is marked with a
sed -i 's/hello/world/' file.txt87
n specifies the desired line-wrap length; a length of 0 [zero] means to never wrap long lines. If omitted, the default as specified on the command line is used. The n parameter is a GNU
sed -i 's/hello/world/' file.txt22 extension
sed -i 's/hello/world/' file.txt963
Reads file filename. Example
sed -i 's/hello/world/' file.txt9
Queue the contents of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that if filename cannot be read, it is treated as if it were an empty file, without any error indication
As a GNU
sed -i 's/hello/world/' file.txt22 extension, the special value /dev/stdin is supported for the file name, which reads the contents of the standard input
As a GNU extension, this command accepts two addresses. The file will then be reread and inserted on each of the addressed lines
sed -i 's/hello/world/' file.txt973
Write the pattern space to filename. As a GNU
sed -i 's/hello/world/' file.txt22 extension, two special values of filename are supported. /dev/stderr, which writes the result to the standard error, and /dev/stdout, which writes to the standard output. 4
The file will be created [or truncated] before the first input line is read; all
sed -i 's/hello/world/' file.txt326 commands [including instances of the
sed -i 's/hello/world/' file.txt326 flag on successful
sed -i 's/hello/world/' file.txt967 commands] which refer to the same filename are output without closing and reopening the file
sed -i 's/hello/world/' file.txt939
If pattern space contains no newline, start a normal new cycle as if the
sed -i 's/hello/world/' file.txt36 command was issued. Otherwise, delete text in the pattern space up to the first newline, and restart cycle with the resultant pattern space, without reading a new line of input
sed -i 's/hello/world/' file.txt71
Thêm một dòng mới vào không gian mẫu, sau đó nối dòng đầu vào tiếp theo vào không gian mẫu. Nếu không có thêm đầ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
When -z is used, a zero byte [the ascii ‘NUL’ character] is added between the lines [instead of a new line]
By default
sed -i 's/hello/world/' file.txt22 does not terminate if there is no ’next’ input line. This is a GNU extension which can be disabled with --posix. See N command on the last line
sed -i 's/hello/world/' file.txt957
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
Replace the contents of the hold space with the contents of the pattern space
sed -i 's/hello/world/' file.txt947
Nối một dòng mới vào nội dung của không gian giữ, sau đó nối thêm nội dung của không gian mẫu vào nội dung của không gian giữ
sed -i 's/hello/world/' file.txt944
Thay thế nội dung của không gian mẫu bằng nội dung của không gian giữ
sed -i 's/hello/world/' file.txt945
Nối một dòng mới vào nội dung của không gian mẫu, rồi nối thêm nội dung của không gian giữ vào nội dung của không gian mẫu
sed -i 's/hello/world/' file.txt975
Trao đổi nội dung của không gian giữ và mẫu
3. 6 Commands for sed -i 's/hello/world/' file.txt
22 gurus
In most cases, use of these commands indicates that you are probably better off programming in something like
sed -i 's/hello/world/' file.txt341 or Perl. But occasionally one is committed to sticking with
sed -i 's/hello/world/' file.txt22, and these commands can enable one to write quite convoluted scripts
sed -i 's/hello/world/' file.txt981
[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
Unconditionally branch to label. The label may be omitted, in which case the next cycle is started
sed -i 's/hello/world/' file.txt966
Branch to label only if there has been a successful
sed -i 's/hello/world/' file.txt967ubstitution since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started
3. 7 Lệnh dành riêng cho GNU sed -i 's/hello/world/' file.txt
22
These commands are specific to GNU
sed -i 's/hello/world/' file.txt22, so you must use them with care and only when you are sure that hindering portability is not evil. They allow you to check for GNU
sed -i 's/hello/world/' file.txt22 extensions or to do tasks that are required quite often, yet are unsupported by standard
sed -i 's/hello/world/' file.txt22s
sed -i 's/hello/world/' file.txt351
This command allows one to pipe input from a shell command into pattern space. Without parameters, the
sed -i 's/hello/world/' file.txt941 command executes the command that is found in pattern space and replaces the pattern space with the output; a trailing newline is suppressed
If a parameter is specified, instead, the
sed -i 's/hello/world/' file.txt941 command interprets it as a command and sends its output to the output stream. The command can run across multiple lines, all but the last ending with a back-slash
In both cases, the results are undefined if the command to be executed contains a NUL character
Note that, unlike the
sed -i 's/hello/world/' file.txt354 command, the output of the command will be printed immediately; the
sed -i 's/hello/world/' file.txt354 command instead delays the output to the end of the current cycle
sed -i 's/hello/world/' file.txt943
Print out the file name of the current input file [with a trailing newline]
sed -i 's/hello/world/' file.txt357
This command accepts only one address
This command is the same as
sed -i 's/hello/world/' file.txt900, but will not print the contents of pattern space. Like
sed -i 's/hello/world/' file.txt900, it provides the ability to return an exit code to the caller
This command can be useful because the only alternative ways to accomplish this apparently trivial function are to use the -n option [which can unnecessarily complicate your script] or resorting to the following snippet, which wastes time by reading the whole file without any visible effect
sed -i 's/hello/world/' file.txt90
sed -i 's/hello/world/' file.txt964
Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that if filename cannot be read, or if its end is reached, no line is appended, without any error indication
Như với lệnh
sed -i 's/hello/world/' file.txt354, giá trị đặc biệt /dev/stdin được hỗ trợ cho tên tệp, đọc một dòng từ đầu vào tiêu chuẩn
sed -i 's/hello/world/' file.txt968
Branch to label only if there have been no successful
sed -i 's/hello/world/' file.txt967ubstitutions since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started
sed -i 's/hello/world/' file.txt364
This command does nothing, but makes
sed -i 's/hello/world/' file.txt22 fail if GNU
sed -i 's/hello/world/' file.txt22 extensions are not supported, simply because other versions of
sed -i 's/hello/world/' file.txt22 do not implement it. In addition, you can specify the version of
sed -i 's/hello/world/' file.txt22 that your script requires, such as
sed -i 's/hello/world/' file.txt369. Giá trị mặc định là
sed -i 's/hello/world/' file.txt370 vì đó là phiên bản đầu tiên triển khai lệnh này
This command enables all GNU extensions even if
sed -i 's/hello/world/' file.txt72 is set in the environment
sed -i 's/hello/world/' file.txt974
Write to the given filename the portion of the pattern space up to the first newline. Everything said under the
sed -i 's/hello/world/' file.txt326 command about file handling holds here too
sed -i 's/hello/world/' file.txt977
This command empties the content of pattern space. It is usually the same as ‘s/. *//’, but is more efficient and works in the presence of invalid multibyte sequences in the input stream. POSIX mandates that such sequences are not matched by ‘. ’, so that there is no portable way to clear
sed -i 's/hello/world/' file.txt22’s buffers in the middle of the script in most multibyte locales [including UTF-8 locales]
3. 8 Multiple commands syntax
There are several methods to specify multiple commands in a
sed -i 's/hello/world/' file.txt22 program
Using newlines is most natural when running a sed script from a file [using the -f option]
On the command line, all
sed -i 's/hello/world/' file.txt22 commands may be separated by newlines. Alternatively, you may specify each command as an argument to an -e option
sed -i 's/hello/world/' file.txt91
A semicolon [‘;’] may be used to separate most simple commands
sed -i 's/hello/world/' file.txt92
The
sed -i 's/hello/world/' file.txt178,
sed -i 's/hello/world/' file.txt179,
sed -i 's/hello/world/' file.txt982,
sed -i 's/hello/world/' file.txt983,
sed -i 's/hello/world/' file.txt984,
sed -i 's/hello/world/' file.txt383 commands can be separated with a semicolon [this is a non-portable GNU
sed -i 's/hello/world/' file.txt22 extension]
sed -i 's/hello/world/' file.txt93
Labels used in
sed -i 's/hello/world/' file.txt982,
sed -i 's/hello/world/' file.txt983,
sed -i 's/hello/world/' file.txt984,
sed -i 's/hello/world/' file.txt383 commands are read until a semicolon. Leading and trailing whitespace is ignored. In the examples below the label is ‘x’. The first example works with GNU
sed -i 's/hello/world/' file.txt22. The second is a portable equivalent. For more information about branching and labels see Branching and flow control
sed -i 's/hello/world/' file.txt94
3. 8. 1 Commands Requiring a newline
Các lệnh sau không thể được phân tách bằng dấu chấm phẩy và yêu cầu một dòng mới
sed -i 's/hello/world/' file.txt926,
sed -i 's/hello/world/' file.txt927,
sed -i 's/hello/world/' file.txt928 [append/change/insert]
All characters following
sed -i 's/hello/world/' file.txt926,
sed -i 's/hello/world/' file.txt927,
sed -i 's/hello/world/' file.txt928 commands are taken as the text to append/change/insert. Using a semicolon leads to undesirable results
sed -i 's/hello/world/' file.txt95
Separate the commands using -e or a newline
sed -i 's/hello/world/' file.txt96
Note that specifying the text to add [‘Hello’] immediately after
sed -i 's/hello/world/' file.txt926,
sed -i 's/hello/world/' file.txt927,
sed -i 's/hello/world/' file.txt928 is itself a GNU
sed -i 's/hello/world/' file.txt22 extension. A portable, POSIX-compliant alternative is
sed -i 's/hello/world/' file.txt97
sed -i 's/hello/world/' file.txt978 [comment]
All characters following ‘#’ until the next newline are ignored
sed -i 's/hello/world/' file.txt98
sed -i 's/hello/world/' file.txt354,
sed -i 's/hello/world/' file.txt88,
sed -i 's/hello/world/' file.txt326,
sed -i 's/hello/world/' file.txt33 [reading and writing files]
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. If whitespace, comments or semicolons are found, they will be included in the filename, leading to unexpected results
sed -i 's/hello/world/' file.txt99
Lưu ý rằng
sed -i 's/hello/world/' file.txt22 âm thầm bỏ qua lỗi đọc/ghi trong các lệnh
sed -i 's/hello/world/' file.txt354,
sed -i 's/hello/world/' file.txt88,
sed -i 's/hello/world/' file.txt326,
sed -i 's/hello/world/' file.txt33 [chẳng hạn như tệp bị thiếu]. Trong ví dụ sau,
sed -i 's/hello/world/' file.txt22 cố gắng đọc một tệp có tên 'hello. văn bản ; . Tệp bị thiếu và lỗi được âm thầm bỏ qua
sed -i 's/hello/world/' file.txt10
sed -i 's/hello/world/' file.txt941 [command execution]
Any characters following the
sed -i 's/hello/world/' file.txt941 command until the end of the line will be sent to the shell. If whitespace, comments or semicolons are found, they will be included in the shell command, leading to unexpected results
sed -i 's/hello/world/' file.txt11
sed -i 's/hello/world/' file.txt617 [substitute with
sed -i 's/hello/world/' file.txt941 or
sed -i 's/hello/world/' file.txt326 flags]
In a substitution command, the
sed -i 's/hello/world/' file.txt326 flag writes the substitution result to a file, and the
sed -i 's/hello/world/' file.txt941 flag executes the subsitution result as a shell command. As with the
sed -i 's/hello/world/' file.txt622 commands, these must be terminated with a newline. If whitespace, comments or semicolons are found, they will be included in the shell command or filename, leading to unexpected results
sed -i 's/hello/world/' file.txt12
4 Addresses. selecting lines
4. 1 Addresses overview
Addresses determine on which line[s] the
sed -i 's/hello/world/' file.txt22 command will be executed. The following command replaces the word ‘hello’ with ‘world’ only on line 144
sed -i 's/hello/world/' file.txt13
If no addresses are given, the command is performed on all lines. The following command replaces the word ‘hello’ with ‘world’ on all lines in the input file
________số 8
Addresses can contain regular expressions to match lines based on content instead of line numbers. The following command replaces the word ‘hello’ with ‘world’ only in lines containing the word ‘apple’
sed -i 's/hello/world/' file.txt15
An address range is specified with two addresses separated by a comma [
sed -i 's/hello/world/' file.txt624]. Addresses can be numeric, regular expressions, or a mix of both. The following command replaces the word ‘hello’ with ‘world’ only in lines 4 to 17 [inclusive]
sed -i 's/hello/world/' file.txt16
Appending the
sed -i 's/hello/world/' file.txt625 character to the end of an address specification [before the command letter] negates the sense of the match. That is, if the
sed -i 's/hello/world/' file.txt625 character follows an address or an address range, then only lines which do not match the addresses will be selected. The following command replaces the word ‘hello’ with ‘world’ only in lines not containing the word ‘apple’
sed -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. excluding lines 4 to 17]
sed -i 's/hello/world/' file.txt18
4. 2 Selecting lines by numbers
Addresses in a
sed -i 's/hello/world/' file.txt22 script can be in any of the following forms
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
This address matches the last line of the last file of input, or the last line of each file when the -i or -s options are specified
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
The following commands demonstrate the step address usage
sed -i 's/hello/world/' file.txt19
4. 3 selecting lines by text matching
GNU
sed -i 's/hello/world/' file.txt22 supports the following regular expression addresses. The default regular expression is Basic Regular Expression [BRE]. If -E or -r options are used, The regular expression should be in Extended Regular Expression [ERE] syntax. See BRE vs ERE
sed -i 's/hello/world/' file.txt636
This will select any line which matches the regular expression regexp. If regexp itself includes any
sed -i 's/hello/world/' file.txt994 characters, each must be escaped by a backslash [
sed -i 's/hello/world/' file.txt997]
The following command prints lines in /etc/passwd which end with ‘bash’5
sed -i 's/hello/world/' file.txt30
The empty regular expression ‘//’ repeats the last regular expression match [the same holds if the empty regular expression is passed to the
sed -i 's/hello/world/' file.txt967 command]. Note that modifiers to regular expressions are evaluated when the regular expression is compiled, thus it is invalid to specify them together with the empty regular expression
sed -i 's/hello/world/' file.txt640
[The
sed -i 's/hello/world/' file.txt641 may be replaced by any other single character. ]
This also matches the regular expression regexp, but allows one to use a different delimiter than
sed -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. If regexp itself includes any delimiter characters, each must be escaped by a backslash [
sed -i 's/hello/world/' file.txt997]
The following commands are equivalent. They print lines which start with ‘/home/alice/documents/’
sed -i 's/hello/world/' file.txt31
sed -i 's/hello/world/' file.txt645
sed -i 's/hello/world/' file.txt646
The
sed -i 's/hello/world/' file.txt145 modifier to regular-expression matching is a GNU extension which causes the regexp to be matched in a case-insensitive manner
In many other programming languages, a lower case
sed -i 's/hello/world/' file.txt928 is used for case-insensitive regular expression matching. However, in
sed -i 's/hello/world/' file.txt22 the
sed -i 's/hello/world/' file.txt928 is used for the insert command [see insert command]
Observe the difference between the following examples
In this example,
sed -i 's/hello/world/' file.txt651 is the address. regular expression with
sed -i 's/hello/world/' file.txt145 modifier.
sed -i 's/hello/world/' file.txt36 is the delete command
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 Addresses
An 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
So khớp addr1 và các dòng theo sau addr1 cho đến dòng tiếp theo có số dòng đầu vào là bội số của N. 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 text
5. 1 Overview of regular expression in sed -i 's/hello/world/' file.txt
22
To 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 expression
Basic and extended regular expressions are two variations on the syntax of the specified pattern. Basic Regular Expression [BRE] syntax is the default in
sed -i 's/hello/world/' file.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 syntax
Here 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
- Apply postfix operators, like
sed -i 's/hello/world/' file.txt
932. this will search for zero or more whole sequences of ‘abcd’, whilesed -i 's/hello/world/' file.txt
933 would search for ‘abc’ followed by zero or more occurrences of ‘d’. Note that support forsed -i 's/hello/world/' file.txt
932 is required by POSIX 1003. 1-2001, but many non-GNU implementations do not support it and hence it is not universally portable - Use back references [see below]
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]’Trong ngôn ngữ C, điều này khớp với bất kỳ chữ cái hoặc chữ số ASCII nào
‘[^ TAB]\+’[Ở đây TAB là viết tắt của một ký tự tab đơn. ] Điều này khớp với một chuỗi gồm một hoặc nhiều ký tự, không có ký tự nào là khoảng trắng hoặc tab. 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 syntax
The 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 Expressions
A 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. ‘[. lower. ]’ and ‘[. upper. ]’; in the ‘C’ locale and ASCII character encoding, this is the same as ‘[A-Za-z]’
‘[. blank. ]’Blank characters. space and tab
‘[. cntrl. ]’Control characters. In ASCII, these characters have octal codes 000 through 037, and 177 [DEL]. In other character sets, these are the equivalent characters, if any
‘[. digit. ]’Digits.
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. ]', và dấu cách
‘[. dấu câu. ]’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 extensions
The 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. This is different from
sed -i 's/hello/world/' file.txt154 in multi-line mode
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 Subexpressions
back-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. Trong ví dụ sau, biểu thức con là '. ’ - any single character [being surrounded by parentheses makes it a subexpression]. The back-reference ‘\1’ asks to match the same content [same character] as the sub-expression
Lệnh bên dưới khớp với các từ bắt đầu bằng bất kỳ ký tự nào, theo sau là chữ 'o', theo sau là ký tự giống như ký tự đầu tiên
sed -i 's/hello/world/' file.txt97
Multiple subexpressions are automatically numbered from left-to-right. This command searches for 6-letter palindromes [the first three letters are 3 subexpressions, followed by 3 back-references in reverse order]
sed -i 's/hello/world/' file.txt98
Trong lệnh
sed -i 's/hello/world/' file.txt967, các tham chiếu ngược có thể được sử dụng trong phần thay thế để tham chiếu lại các biểu thức con trong phần biểu thức chính quy
Ví dụ sau sử dụng hai biểu thức con trong biểu thức chính quy để khớp với hai từ được phân tách bằng dấu cách. Các tham chiếu ngược trong phần thay thế in các từ theo một thứ tự khác
sed -i 's/hello/world/' file.txt99
Khi được sử dụng xen kẽ, nếu nhóm không tham gia trận đấu thì tham chiếu ngược khiến toàn bộ trận đấu thất bại. Ví dụ: 'a [. ]. b\1' sẽ không khớp với 'ba'. Khi nhiều biểu thức chính quy được đưa ra với -e hoặc từ một tệp [tệp '-f'], các tham chiếu ngược là cục bộ cho từng biểu thức
5. 8 Escape Sequences - chỉ định các ký tự đặc biệt
Cho đến chương này, chúng ta mới chỉ gặp các lối thoát có dạng '\^', lệnh này yêu cầu
sed -i 's/hello/world/' file.txt22 không hiểu dấu mũ là một ký tự đặc biệt, mà nên hiểu nó theo nghĩa đen. For example, ‘\*’ matches a single asterisk rather than zero or more backslashes
Chương này giới thiệu một loại thoát khác6—đó là, các thoát được áp dụng cho một ký tự hoặc chuỗi ký tự thường được hiểu theo nghĩa đen và
sed -i 's/hello/world/' file.txt22 đó thay thế bằng một ký tự đặc biệt. Điều này cung cấp một cách mã hóa các ký tự không in được trong các mẫu theo cách hiển thị. Không có hạn chế về sự xuất hiện của các ký tự không in được trong tập lệnh
sed -i 's/hello/world/' file.txt22 nhưng khi tập lệnh đang được chuẩn bị trong trình bao hoặc bằng cách chỉnh sửa văn bản, việc sử dụng một trong các chuỗi thoát sau đây thường dễ dàng hơn so với ký tự nhị phân mà nó đại diện
The list of these escapes is
sed 's/hello/world/' input.txt > output.txt902
Tạo hoặc khớp một ký tự BEL, đó là một "cảnh báo" [ASCII 7]
sed 's/hello/world/' input.txt > output.txt903
Tạo hoặc khớp với nguồn cấp dữ liệu biểu mẫu [ASCII 12]
sed -i 's/hello/world/' file.txt990
Tạo hoặc khớp một dòng mới [ASCII 10]
sed 's/hello/world/' input.txt > output.txt905
Tạo hoặc khớp với trả lại vận chuyển [ASCII 13]
sed -i 's/hello/world/' file.txt953
Tạo hoặc khớp với tab ngang [ASCII 9]
sed 's/hello/world/' input.txt > output.txt907
Tạo hoặc khớp với cái gọi là “tab dọc” [ASCII 11]
sed 's/hello/world/' input.txt > output.txt908
Tạo hoặc khớp với CONTROL-x, trong đó x là ký tự bất kỳ. Tác dụng chính xác của ‘\cx’ như sau. nếu x là chữ thường, nó được chuyển thành chữ hoa. Sau đó, bit 6 của ký tự [hex 40] được đảo ngược. Do đó, '\cz' trở thành hex 1A, nhưng '\c{' trở thành hex 3B, trong khi '\c;' trở thành hex 7B
sed 's/hello/world/' input.txt > output.txt909
Tạo hoặc khớp một ký tự có giá trị ASCII thập phân là xxx
sed 's/hello/world/' input.txt > output.txt910
Tạo hoặc khớp một ký tự có giá trị ASCII bát phân là xxx
sed 's/hello/world/' input.txt > output.txt911
Tạo hoặc khớp một ký tự có giá trị ASCII thập lục phân là xx
'\b' [xóa lùi] đã bị bỏ qua do xung đột với nghĩa "ranh giới từ" hiện có
5. 8. 1 ưu tiên thoát
GNU
sed -i 's/hello/world/' file.txt22 xử lý các chuỗi thoát trước khi chuyển văn bản vào đối sánh biểu thức chính quy của lệnh
sed -i 's/hello/world/' file.txt666 và đối sánh Địa chỉ. Do đó, hai lệnh sau đây là tương đương ['0x5e' là giá trị ASCII thập lục phân của ký tự '^']
sed 's/hello/world/' input.txt > output.txt90
Như sau [‘0x5b’,‘0x5d’ lần lượt là các giá trị ASCII thập lục phân của ‘[’, ‘]’]
sed 's/hello/world/' input.txt > output.txt91
Tuy nhiên, nên tránh các ký tự đặc biệt như vậy do các trường hợp cạnh không mong muốn. Ví dụ, những điều sau đây không tương đương
sed 's/hello/world/' input.txt > output.txt92
5. 9 ký tự Multibyte và cân nhắc ngôn ngữ
GNU
sed -i 's/hello/world/' file.txt22 xử lý các ký tự nhiều byte hợp lệ trong các ngôn ngữ nhiều byte [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]. Trong ngôn ngữ
sed 's/hello/world/' input.txt > output.txt915,
sed -i 's/hello/world/' file.txt22 xử lý chính xác Sigma dưới dạng một ký tự mặc dù nó là 2 octet [byte]
sed 's/hello/world/' input.txt > output.txt93
Để buộc
sed -i 's/hello/world/' file.txt22 xử lý các octet riêng biệt, hãy sử dụng ngôn ngữ
sed 's/hello/world/' input.txt > output.txt920 [còn được gọi là ngôn ngữ
sed 's/hello/world/' input.txt > output.txt921]
sed 's/hello/world/' input.txt > output.txt94
5. 9. 1 Ký tự nhiều byte không hợp lệ
sed -i 's/hello/world/' file.txt22’s regular expressions do not match invalid multibyte sequences in a multibyte locale
In the following examples, the ascii value
sed 's/hello/world/' input.txt > output.txt923 is an incomplete multibyte character [shown here as �]. The regular expression ‘. ’ does not match it
sed 's/hello/world/' input.txt > output.txt95
Similarly, the ’catch-all’ regular expression ‘. *’ does not match the entire line
sed 's/hello/world/' input.txt > output.txt96
GNU
sed -i 's/hello/world/' file.txt22 offers the special
sed -i 's/hello/world/' file.txt977 command to clear the current pattern space regardless of invalid multibyte characters [i. e. it works like
sed 's/hello/world/' input.txt > output.txt926 but also removes invalid multibyte characters]
sed 's/hello/world/' input.txt > output.txt97
Alternatively, force the
sed 's/hello/world/' input.txt > output.txt920 locale to process each octet separately [every octet is a valid character in the
sed 's/hello/world/' input.txt > output.txt920 locale]
sed 's/hello/world/' input.txt > output.txt98
sed -i 's/hello/world/' file.txt22’s inability to process invalid multibyte characters can be used to detect such invalid sequences in a file. In the following examples, the
sed 's/hello/world/' input.txt > output.txt930 is an invalid multibyte sequence, while
sed 's/hello/world/' input.txt > output.txt931 is a valid multibyte sequence [of the Greek Sigma character]
The following
sed -i 's/hello/world/' file.txt22 program removes all valid characters using
sed 's/hello/world/' input.txt > output.txt933. Any content left in the pattern space [the invalid characters] are added to the hold space using the
sed -i 's/hello/world/' file.txt947 command. On the last line [
sed -i 's/hello/world/' file.txt87], the hold space is retrieved [
sed -i 's/hello/world/' file.txt975], newlines are removed [
sed 's/hello/world/' input.txt > output.txt937], and any remaining octets are printed unambiguously [
sed -i 's/hello/world/' file.txt67]. Thus, any invalid multibyte sequences are printed as octal values
sed 's/hello/world/' input.txt > output.txt99
With a few more commands,
sed -i 's/hello/world/' file.txt22 can print the exact line number corresponding to each invalid characters [line 3]. These characters can then be removed by forcing the
sed 's/hello/world/' input.txt > output.txt920 locale and using octal escape sequences
sed -i 's/hello/world/' file.txt040
5. 9. 2 Upper/Lower case conversion
GNU
sed -i 's/hello/world/' file.txt22’s substitute command [
sed -i 's/hello/world/' file.txt967] supports upper/lower case conversions using
sed -i 's/hello/world/' file.txt105,
sed -i 's/hello/world/' file.txt104 codes. These conversions support multibyte characters
sed -i 's/hello/world/' file.txt041
See The "s" Command
5. 9. 3 Multibyte regexp character classes
In other locales, the sorting sequence is not specified, and ‘[a-d]’ might be equivalent to ‘[abcd]’ or to ‘[aBbCcDd]’, or it might fail to match any character, or the set of characters that it matches might even be erratic. To obtain the traditional interpretation of bracket expressions, you can use the ‘C’ locale by setting the
sed 's/hello/world/' input.txt > output.txt945 environment variable to the value ‘C’
sed -i 's/hello/world/' file.txt042
Their interpretation depends on the
sed 's/hello/world/' input.txt > output.txt946 locale; for example, ‘[[. 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 buffers
6. 1 How sed -i 's/hello/world/' file.txt
22 Works
sed -i 's/hello/world/' file.txt22 maintains two data buffers. the active pattern space, and the auxiliary hold space. Both are initially empty
sed -i 's/hello/world/' file.txt22 operates by performing the following cycle on each line of input. first,
sed -i 's/hello/world/' file.txt22 reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it. addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed
When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed. 8 Then the next cycle starts for the next input line
Trừ khi các lệnh đặc biệt [như 'D'] được sử dụng, không gian mẫu sẽ bị xóa giữa hai chu kỳ. The hold space, on the other hand, keeps its data between cycles [see commands ‘h’, ‘H’, ‘x’, ‘g’, ‘G’ to move data between both buffers]
6. 2 Hold and Pattern Buffers
LÀM
6. 3 Multiline techniques - using D,G,H,N,P to process multiple lines
Nhiều dòng có thể được xử lý như một bộ đệm bằng cách sử dụng
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. Chúng tương tự như các lệnh viết thường [
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], ngoại trừ việc các lệnh này nối hoặc trừ dữ liệu trong khi tôn trọng các dòng mới được nhúng - cho phép thêm và xóa các dòng khỏi mẫu và giữ khoảng trắng
Họ hoạt động như sau
sed -i 's/hello/world/' file.txt939
xóa dòng khỏi không gian mẫu cho đến dòng mới đầu tiên và khởi động lại chu kỳ
sed -i 's/hello/world/' file.txt945
nối thêm dòng từ không gian giữ vào không gian mẫu, với một dòng mới trước nó
sed -i 's/hello/world/' file.txt947
nối thêm dòng từ không gian mẫu vào không gian giữ, với một dòng mới trước nó
sed -i 's/hello/world/' file.txt71
nối thêm dòng từ tệp đầu vào vào không gian mẫu
sed -i 's/hello/world/' file.txt957
in dòng từ không gian mẫu cho đến dòng mới đầu tiên
Ví dụ sau minh họa hoạt động của lệnh
sed -i 's/hello/world/' file.txt71 và
sed -i 's/hello/world/' file.txt939
sed -i 's/hello/world/' file.txt044
sed -i 's/hello/world/' file.txt
22 bắt đầu bằng cách đọc dòng đầu tiên vào không gian mẫu [i. e. '1']- Vào đầu mỗi chu kỳ, lệnh
sed -i 's/hello/world/' file.txt
71 nối thêm một dòng mới và dòng tiếp theo vào không gian mẫu [i. e. ‘1’, ‘\n’, ‘2’ trong chu kỳ đầu tiên] - Lệnh
sed -i 's/hello/world/' file.txt
67 in rõ ràng nội dung của không gian mẫu - Sau đó, lệnh
sed -i 's/hello/world/' file.txt
939 sẽ xóa nội dung của không gian mẫu cho đến dòng mới đầu tiên [để lại '2' ở cuối chu kỳ đầu tiên] - Ở chu kỳ tiếp theo, lệnh
sed -i 's/hello/world/' file.txt
71 nối thêm một dòng mới và dòng đầu vào tiếp theo vào không gian mẫu [e. g. ‘2’, ‘\n’, ‘3’]
Một kỹ thuật phổ biến để xử lý các khối văn bản như đoạn văn [thay vì từng dòng] là sử dụng cấu trúc sau
sed -i 's/hello/world/' file.txt045
- Biểu thức đầu tiên,
sed 's/hello/world/' input.txt > output.txt
974 hoạt động trên tất cả các dòng không trống và thêm dòng hiện tại [trong không gian mẫu] vào không gian giữ. Trên tất cả các dòng ngoại trừ dòng cuối cùng, không gian mẫu bị xóa và chu kỳ được bắt đầu lại - Các biểu thức khác
sed -i 's/hello/world/' file.txt
975 vàsed -i 's/hello/world/' file.txt
967 chỉ được thực hiện trên các dòng trống [i. e. ngăn cách đoạn văn]. Lệnhsed -i 's/hello/world/' file.txt
975 tìm nạp các dòng tích lũy từ không gian giữ trở lại không gian mẫu. Sau đó, lệnhsed -i 's/hello/world/' file.txt
666 sẽ hoạt động trên tất cả văn bản trong đoạn văn [bao gồm cả các dòng mới được nhúng]
Ví dụ sau minh họa kỹ thuật này
sed -i 's/hello/world/' file.txt046
Để biết thêm ví dụ về chú thích, hãy xem Tìm kiếm văn bản trên nhiều dòng và Điều chỉnh độ dài dòng
6. 4 Phân nhánh và Kiểm soát luồng
Các lệnh rẽ nhánh
sed -i 's/hello/world/' file.txt982,
sed -i 's/hello/world/' file.txt983 và
sed -i 's/hello/world/' file.txt984 cho phép thay đổi luồng chương trình
sed -i 's/hello/world/' file.txt22
Theo mặc định,
sed -i 's/hello/world/' file.txt22 đọc một dòng đầu vào vào bộ đệm mẫu, sau đó tiếp tục xử lý tất cả các lệnh theo thứ tự. Các lệnh không có địa chỉ ảnh hưởng đến tất cả các dòng. Các lệnh có địa chỉ chỉ ảnh hưởng đến các dòng phù hợp. Xem tổng quan về Chu kỳ thực thi và địa chỉ
sed -i 's/hello/world/' file.txt22 không hỗ trợ cấu trúc
sed 's/hello/world/' input.txt > output.txt985 điển hình. Thay vào đó, một số lệnh có thể được sử dụng làm điều kiện hoặc để thay đổi điều khiển luồng mặc định
sed -i 's/hello/world/' file.txt36
xóa [xóa] không gian mẫu hiện tại và khởi động lại chu trình chương trình mà không xử lý phần còn lại của lệnh và không in không gian mẫu
sed -i 's/hello/world/' file.txt939
xóa nội dung của không gian mẫu cho đến dòng mới đầu tiên và khởi động lại chu kỳ chương trình mà không xử lý phần còn lại của lệnh và không in không gian mẫu
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. Nếu [addr] khớp với không gian mẫu hiện tại, hãy thực hiện [các] lệnh. Ví dụ. Lệnh
sed 's/hello/world/' input.txt > output.txt993 có nghĩa là. nếu mẫu hiện tại khớp với biểu thức chính quy
sed 's/hello/world/' input.txt > output.txt994 [dòng bắt đầu bằng hàm băm], thì hãy thực hiện lệnh
sed -i 's/hello/world/' file.txt36. xóa dòng mà không in và khởi động lại chu kỳ chương trình ngay lập tức
sed -i 's/hello/world/' file.txt982
nhánh vô điều kiện [nghĩa là. luôn nhảy tới một nhãn, bỏ qua hoặc lặp lại các lệnh khác mà không bắt đầu lại một chu kỳ mới]. Kết hợp với một địa chỉ, nhánh có thể được thực hiện có điều kiện trên các dòng phù hợp
sed -i 's/hello/world/' file.txt983
nhánh có điều kiện [nghĩa là. nhảy tới nhãn] chỉ khi lệnh ________ 3666 đã được tách ra kể từ khi dòng đầu vào cuối cùng được đọc hoặc một nhánh có điều kiện khác được thực hiện
sed -i 's/hello/world/' file.txt984
tương tự nhưng ngược lại với lệnh
sed -i 's/hello/world/' file.txt983. chỉ rẽ nhánh nếu không có sự thay thế thành công nào kể từ khi dòng đầu vào cuối cùng được đọc
Hai chương trình
sed -i 's/hello/world/' file.txt22 sau đây là tương đương. Ví dụ đầu tiên [giả tạo] sử dụng lệnh
sed -i 's/hello/world/' file.txt982 để bỏ qua lệnh
sed -i 's/hello/world/' file.txt666 trên các dòng chứa '1'. Ví dụ thứ hai sử dụng một địa chỉ phủ định [‘. ’] để chỉ thực hiện thay thế trên các dòng mong muốn. 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 Phân nhánh và Chu kỳ
Các lệnh
sed -i 's/hello/world/' file.txt982,
sed -i 's/hello/world/' file.txt983 và
sed -i 's/hello/world/' file.txt984 có thể được theo sau bởi một nhãn [thường là một chữ cái]. Nhãn được xác định bằng dấu hai chấm theo sau bởi một hoặc nhiều chữ cái [e. g. ‘. x’]. Nếu nhãn bị bỏ qua, các lệnh nhánh sẽ khởi động lại chu trình. Lưu ý sự khác biệt giữa phân nhánh thành nhãn và khởi động lại chu kỳ. khi một chu kỳ được khởi động lại, trước tiên,
sed -i 's/hello/world/' file.txt22 sẽ in nội dung hiện tại của không gian mẫu, sau đó đọc dòng đầu vào tiếp theo vào không gian mẫu;
Chương trình sau đây là một no-op. Lệnh
sed -i 's/hello/world/' file.txt982 [lệnh duy nhất trong chương trình] không có nhãn và do đó chỉ cần khởi động lại chu trình. Trên mỗi chu kỳ, không gian mẫu được in và dòng đầu vào tiếp theo được đọc
Ví dụ sau đây là một vòng lặp vô hạn - nó không kết thúc và không in bất cứ thứ gì. Lệnh
sed -i 's/hello/world/' file.txt982 chuyển đến nhãn 'x' và một chu kỳ mới không bao giờ được bắt đầu
sed -i 's/hello/world/' file.txt048
Phân nhánh thường được bổ sung bằng các lệnh
sed -i 's/hello/world/' file.txt952 hoặc
sed -i 's/hello/world/' file.txt71. cả hai lệnh đều đọc dòng đầu vào tiếp theo vào không gian mẫu mà không cần đợi chu kỳ khởi động lại. Trước khi đọc dòng đầu vào tiếp theo,
sed -i 's/hello/world/' file.txt952 in không gian mẫu hiện tại rồi làm trống nó, trong khi
sed -i 's/hello/world/' file.txt71 nối một dòng mới và dòng đầu vào tiếp theo vào không gian mẫu
Xét hai ví dụ sau
sed -i 's/hello/world/' file.txt049
- Cả hai ví dụ không inf-loop, mặc dù không bao giờ bắt đầu một chu kỳ mới
- Trong ví dụ đầu tiên, các lệnh
sed -i 's/hello/world/' file.txt
952 trước tiên in nội dung của không gian mẫu, làm trống không gian mẫu sau đó đọc dòng đầu vào tiếp theo - Trong ví dụ thứ hai, các lệnh
sed -i 's/hello/world/' file.txt
71 nối thêm dòng đầu vào tiếp theo vào không gian mẫu [với một dòng mới]. Các dòng được tích lũy trong không gian mẫu cho đến khi không còn dòng đầu vào nào để đọc, sau đó lệnhsed -i 's/hello/world/' file.txt
71 kết thúc chương trìnhsed -i 's/hello/world/' file.txt
22. Khi chương trình kết thúc, các hành động cuối chu kỳ được thực hiện và toàn bộ không gian mẫu được in - Ví dụ thứ hai yêu cầu GNU
sed -i 's/hello/world/' file.txt
22, vì nó sử dụng hành vi không chuẩn POSIX củased -i 's/hello/world/' file.txt
71. Xem đoạn “lệnh ______071 ở dòng cuối cùng” trong Báo cáo lỗi - Để kiểm tra thêm sự khác biệt giữa hai ví dụ, hãy thử các lệnh sau
sed -i 's/hello/world/' file.txt
000
6. 4. 2 Ví dụ phân nhánh. đường nối
Như một ví dụ thực tế về việc sử dụng phân nhánh, hãy xem xét trường hợp các tệp có thể in được trích dẫn, thường được sử dụng để mã hóa thư email. Trong các tệp này, các dòng dài được chia nhỏ và được đánh dấu bằng ngắt dòng mềm bao gồm một ký tự '=' duy nhất ở cuối dòng
sed -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ự '=' 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. không gian mẫu được in và một chu kỳ mới được bắt đầu
sed -i 's/hello/world/' file.txt002
Đây là một chương trình thay thế với cách tiếp cận hơi khác. Trên tất cả các dòng trừ dòng cuối cùng,
sed -i 's/hello/world/' file.txt71 nối thêm dòng vào không gian mẫu. Sau đó, một lệnh thay thế sẽ loại bỏ các ngắt dòng mềm ['=' ở cuối dòng, i. e. theo sau là một dòng mới] bằng cách thay thế chúng bằng một chuỗi rỗng. nếu thay thế thành công [có nghĩa là không gian mẫu chứa một dòng cần được nối], Lệnh rẽ nhánh có điều kiện
sed -i 's/hello/world/' file.txt983 sẽ nhảy đến đầu chương trình mà không hoàn thành hoặc khởi động lại chu trình. Nếu thay thế không thành công [có nghĩa là không có ngắt dòng mềm], lệnh
sed -i 's/hello/world/' file.txt983 sẽ không phân nhánh. Sau đó,
sed -i 's/hello/world/' file.txt957 sẽ in nội dung không gian mẫu cho đến dòng mới đầu tiên và
sed -i 's/hello/world/' file.txt939 sẽ xóa nội dung không gian mẫu cho đến dòng mới đầu tiên. [Để tìm hiểu thêm về các lệnh
sed -i 's/hello/world/' file.txt71,
sed -i 's/hello/world/' file.txt957 và
sed -i 's/hello/world/' file.txt939, hãy xem Kỹ thuật đa dòng]
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 Một số kịch bản mẫu
Dưới đây là một số kịch bản
sed -i 's/hello/world/' file.txt22 để hướng dẫn bạn nghệ thuật làm chủ
sed -i 's/hello/world/' file.txt22
7. 1 Đường nối
Phần này sử dụng các lệnh
sed -i 's/hello/world/' file.txt71,
sed -i 's/hello/world/' file.txt939 và
sed -i 's/hello/world/' file.txt957 để xử lý nhiều dòng và các lệnh
sed -i 's/hello/world/' file.txt982 và
sed -i 's/hello/world/' file.txt983 để phân nhánh. Xem Kỹ thuật đa tuyến và Kiểm soát luồng và phân nhánh
Tham gia các dòng cụ thể [e. g. nếu dòng 2 và 3 cần được nối]
sed -i 's/hello/world/' file.txt004
Tham gia các dòng tiếp tục dấu gạch chéo ngược
sed -i 's/hello/world/' file.txt005
Nối các dòng bắt đầu bằng khoảng trắng [e. g tiêu đề SMTP]
sed -i 's/hello/world/' file.txt006
7. 2 đường định tâm
Tập lệnh này căn giữa tất cả các dòng của tệp trên chiều rộng 80 cột. Để thay đổi chiều rộng đó, số trong
sed -i 's/hello/world/' file.txt0439 phải được thay thế và số khoảng trắng đã thêm cũng phải được thay đổi
Lưu ý cách các lệnh đệm được sử dụng để phân tách các phần trong biểu thức chính quy được khớp—đây là một kỹ thuật phổ biến
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 Tăng một số
Tập lệnh này là một trong số ít tập lệnh minh họa cách thực hiện số học trong
sed -i 's/hello/world/' file.txt22. Điều này thực sự có thể,9 nhưng phải được thực hiện thủ công
Để tăng một số, bạn chỉ cần thêm 1 vào chữ số cuối cùng, thay thế nó bằng chữ số tiếp theo. Có một ngoại lệ. khi chữ số là chín, các chữ số trước đó cũng phải được tăng dần cho đến khi bạn không có số chín
Giải pháp này của Bruno Haible rất thông minh và thông minh vì nó sử dụng một bộ đệm duy nhất; . Nó hoạt động bằng cách thay thế các số 9 ở cuối bằng dấu gạch dưới, sau đó sử dụng nhiều lệnh
sed -i 's/hello/world/' file.txt967 để tăng chữ số cuối cùng, sau đó lại thay thế dấu gạch dưới bằng số không
sed -i 's/hello/world/' file.txt013
sed -i 's/hello/world/' file.txt014
sed -i 's/hello/world/' file.txt015
sed -i 's/hello/world/' file.txt016
sed -i 's/hello/world/' file.txt017
7. 4 Đổi tên tệp thành chữ thường
Đây là một cách sử dụng khá lạ của
sed -i 's/hello/world/' file.txt22. Chúng tôi chuyển đổi văn bản và chuyển đổi nó thành các lệnh trình bao, sau đó chỉ cần đưa chúng vào trình bao. Đừng lo lắng, những vụ hack thậm chí còn tồi tệ hơn được thực hiện khi sử dụng
sed -i 's/hello/world/' file.txt22;
Nội dung chính của phần này là tập lệnh
sed -i 's/hello/world/' file.txt22, ánh xạ lại tên từ dưới lên trên [hoặc ngược lại] và thậm chí kiểm tra xem tên được ánh xạ lại có giống với tên gốc không. Lưu ý cách tập lệnh được tham số hóa bằng cách sử dụng các biến trình bao và trích dẫn thích hợp
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___1
7. 5 Bản in sed -i 's/hello/world/' file.txt
0447 Môi trường
Tập lệnh này loại bỏ định nghĩa của các hàm trình bao khỏi đầu ra của lệnh
sed -i 's/hello/world/' file.txt0448 Bourne-shell
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 ký tự đảo ngược của dòng
Tập lệnh này có thể được sử dụng để đảo ngược vị trí của các ký tự trong dòng. Kỹ thuật này di chuyển hai ký tự cùng một lúc, do đó nó nhanh hơn so với các triển khai trực quan hơn
Lưu ý lệnh
sed -i 's/hello/world/' file.txt0449 trước định nghĩa của nhãn. Điều này thường cần thiết để đặt lại cờ được kiểm tra bằng lệnh
sed -i 's/hello/world/' file.txt983
Độc giả giàu trí tưởng tượng sẽ tìm thấy cách sử dụng cho kịch bản này. Một ví dụ là đảo ngược đầu ra của
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 Tìm kiếm văn bản trên nhiều dòng
Phần này sử dụng lệnh
sed -i 's/hello/world/' file.txt71 và
sed -i 's/hello/world/' file.txt939 để tìm kiếm các từ liên tiếp trên nhiều dòng. Xem kỹ thuật Multiline
Những ví dụ này giải quyết việc tìm kiếm các từ xuất hiện gấp đôi trong tài liệu
Dễ dàng tìm các từ kép trong một dòng bằng GNU
sed -i 's/hello/world/' file.txt687 và tương tự với GNU
sed -i 's/hello/world/' file.txt22
sed -i 's/hello/world/' file.txt052
- Cụm từ thông dụng ‘\b\w+\s+’ tìm kiếm ranh giới từ [‘\b’], theo sau là một hoặc nhiều ký tự từ [‘\w+’], tiếp theo là khoảng trắng [‘\s+’]. Xem tiện ích mở rộng regrec
- Thêm dấu ngoặc đơn quanh biểu thức ‘[\w+]’ sẽ tạo ra một biểu thức con. Mẫu biểu thức chính quy '[PATTERN]\s+\1' xác định một biểu thức con [trong ngoặc đơn], theo sau là tham chiếu ngược, được phân tách bằng khoảng trắng. Kết hợp thành công có nghĩa là MẪU được lặp lại hai lần liên tiếp. Xem Back-references và Subexpressions
- Biểu thức ranh giới từ [‘\b’] ở cả hai đầu đảm bảo các từ một phần không được khớp [e. g. 'the then' không phải là một kết hợp mong muốn]
- Tùy chọn -E cho phép cú pháp biểu thức chính quy mở rộng, giảm bớt nhu cầu thêm dấu gạch chéo ngược trước dấu ngoặc đơn. Xem cú pháp ERE
Khi từ được nhân đôi kéo dài hai dòng, biểu thức chính quy ở trên sẽ không tìm thấy chúng vì
sed -i 's/hello/world/' file.txt687 và
sed -i 's/hello/world/' file.txt22 hoạt động theo từng dòng
Bằng cách sử dụng các lệnh
sed -i 's/hello/world/' file.txt71 và
sed -i 's/hello/world/' file.txt939,
sed -i 's/hello/world/' file.txt22 có thể áp dụng các biểu thức chính quy trên nhiều dòng [nghĩa là nhiều dòng được lưu trữ trong không gian mẫu và biểu thức chính quy hoạt động trên đó]
sed -i 's/hello/world/' file.txt053
- Lệnh
sed -i 's/hello/world/' file.txt
71 nối thêm dòng tiếp theo vào không gian mẫu [do đó đảm bảo nó chứa hai dòng liên tiếp trong mỗi chu kỳ] - Biểu thức chính quy sử dụng '\s+' cho dấu tách từ khớp với cả khoảng trắng và dòng mới
- Biểu thức chính quy khớp, toàn bộ không gian mẫu được in bằng
sed -i 's/hello/world/' file.txt
37. Không có dòng nào được in theo mặc định do tùy chọn -n sed -i 's/hello/world/' file.txt
939 xóa dòng đầu tiên khỏi không gian mẫu [cho đến dòng mới đầu tiên], sẵn sàng cho chu kỳ tiếp theo
Xem hướng dẫn sử dụng GNU
sed -i 's/hello/world/' file.txt0464 để biết giải pháp thay thế bằng cách sử dụng
sed -i 's/hello/world/' file.txt0465 và
sed -i 's/hello/world/' file.txt0466 tại https. //gnu. org/s/coreutils/manual/html_node/Ép-và-xóa. html
7. 8 Điều chỉnh độ dài dòng
Phần này sử dụng lệnh
sed -i 's/hello/world/' file.txt71 và
sed -i 's/hello/world/' file.txt939 để tìm kiếm các từ liên tiếp kéo dài trên nhiều dòng và lệnh
sed -i 's/hello/world/' file.txt982 để phân nhánh. Xem Kỹ thuật đa tuyến và Kiểm soát luồng và phân nhánh
Ví dụ [hơi giả tạo] này xử lý việc định dạng và ngắt dòng văn bản của tệp đầu vào sau
sed -i 's/hello/world/' file.txt054
Chương trình sed sau kết thúc các dòng ở 40 ký tự
sed -i 's/hello/world/' file.txt055
đầu ra được bọc
sed -i 's/hello/world/' file.txt056
7. 9 dòng tệp ngược
Cái này bắt đầu một loạt các kịch bản hoàn toàn vô dụng [nhưng thú vị] mô phỏng các lệnh Unix khác nhau. Điều này, đặc biệt, là một công việc tương tự
sed -i 's/hello/world/' file.txt0470
Lưu ý rằng trên các triển khai khác ngoài GNU
sed -i 's/hello/world/' file.txt22, tập lệnh này có thể dễ dàng làm tràn bộ đệm bên trong
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 cùng một mục đích 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ống
Mô phỏng 'cat -b' gần giống như 'cat -n'—chúng ta chỉ phải chọn dòng nào sẽ được đánh số và dòng nào không
The part that is common to this script and the previous one is not commented to show how important it is to comment
sed -i 's/hello/world/' file.txt22 scripts properly
________ 1062 ________ 1072 ________ 1073
7. 12 số đếm
This script shows another way to do arithmetic with
sed -i 's/hello/world/' file.txt22. Trong trường hợp này, chúng tôi phải thêm các số có thể lớn, do đó, việc triển khai điều này theo các bước tăng liên tiếp sẽ không khả thi [và thậm chí có thể phức tạp hơn để xử lý so với tập lệnh này]
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. we simply add the number of characters on the current line as units, and then propagate the carry to tens, hundreds, and so on
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']
It is interesting that real
sed -i 's/hello/world/' file.txt0480 programs have optimized loops for ‘wc -c’, so they are much slower at counting words rather than characters. 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]
Một lần nữa, các phần chung không được bình luận để cho thấy tầm quan trọng của việc bình luận các kịch bản
sed -i 's/hello/world/' file.txt22
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 đếm
No strange things are done now, because
sed -i 's/hello/world/' file.txt22 gives us ‘wc -l’ functionality for free. Look
7. 15 Printing the First Lines
This script is probably the simplest useful
sed -i 's/hello/world/' file.txt22 script. It displays the first 10 lines of input; the number of displayed lines is right before the
sed -i 's/hello/world/' file.txt900 command
7. 16 Printing the Last Lines
Printing the last n lines rather than the first is more complex but indeed possible. n is encoded in the second line, before the bang character
This script is similar to the
sed -i 's/hello/world/' file.txt0470 script in that it keeps the final output in the hold space and prints it at the end
sed -i 's/hello/world/' file.txt062
sed -i 's/hello/world/' file.txt086
Mainly, the scripts keeps a window of 10 lines and slides it by adding a line and deleting the oldest [the substitution command on the second line works like a
sed -i 's/hello/world/' file.txt939 command but does not restart the loop]
The “sliding window” technique is a very powerful way to write efficient and complex
sed -i 's/hello/world/' file.txt22 scripts, because commands like
sed -i 's/hello/world/' file.txt957 would require a lot of work if implemented manually
To introduce the technique, which is fully demonstrated in the rest of this chapter and is based on the
sed -i 's/hello/world/' file.txt71,
sed -i 's/hello/world/' file.txt957 and
sed -i 's/hello/world/' file.txt939 commands, here is an implementation of
sed -i 's/hello/world/' file.txt0492 using a simple “sliding window. ”
This looks complicated but in fact the working is the same as the last script. after we have kicked in the appropriate number of lines, however, we stop using the hold space to keep inter-line state, and instead use
sed -i 's/hello/world/' file.txt71 and
sed -i 's/hello/world/' file.txt939 to slide pattern space by one line
sed -i 's/hello/world/' file.txt007
sed -i 's/hello/world/' file.txt088
Note how the first, second and fourth line are inactive after the first ten lines of input. After that, all the script does is. exiting on the last line of input, appending the next input line to pattern space, and removing the first line
7. 17 Make Duplicate Lines Unique
This is an example of the art of using the
sed -i 's/hello/world/' file.txt71,
sed -i 's/hello/world/' file.txt957 and
sed -i 's/hello/world/' file.txt939 commands, probably the most difficult to master
sed -i 's/hello/world/' file.txt089
sed -i 's/hello/world/' file.txt090
sed -i 's/hello/world/' file.txt091
sed -i 's/hello/world/' file.txt092
As you can see, we maintain a 2-line window using
sed -i 's/hello/world/' file.txt957 and
sed -i 's/hello/world/' file.txt939. This technique is often used in advanced
sed -i 's/hello/world/' file.txt22 scripts
7. 18 Print Duplicated Lines of Input
This script prints only duplicated lines, like ‘uniq -d’
sed -i 's/hello/world/' file.txt062
sed -i 's/hello/world/' file.txt094
sed -i 's/hello/world/' file.txt095
sed -i 's/hello/world/' file.txt096
sed -i 's/hello/world/' file.txt097
7. 19 Remove All Duplicated Lines
This script prints only unique lines, like ‘uniq -u’
sed -i 's/hello/world/' file.txt007
sed -i 's/hello/world/' file.txt099
sed -i 's/hello/world/' file.txt00
sed -i 's/hello/world/' file.txt01
sed -i 's/hello/world/' file.txt02
7. 20 Squeezing Blank Lines
As a final example, here are three scripts, of increasing complexity and speed, that implement the same function as ‘cat -s’, that is squeezing blank lines
The first leaves a blank line at the beginning and end if there are some already
sed -i 's/hello/world/' file.txt007
sed -i 's/hello/world/' file.txt04
sed -i 's/hello/world/' file.txt05
This one is a bit more complex and removes all empty lines at the beginning. It does leave a single blank line at end if one was there
sed -i 's/hello/world/' file.txt007
sed -i 's/hello/world/' file.txt07
sed -i 's/hello/world/' file.txt08
This removes leading and trailing blank lines. It is also the fastest. Note that loops are completely done with
sed -i 's/hello/world/' file.txt952 and
sed -i 's/hello/world/' file.txt982, without relying on
sed -i 's/hello/world/' file.txt22 to restart the script automatically at the end of a line
sed -i 's/hello/world/' file.txt062
sed -i 's/hello/world/' file.txt10
sed -i 's/hello/world/' file.txt11
sed -i 's/hello/world/' file.txt12
sed -i 's/hello/world/' file.txt13
sed -i 's/hello/world/' file.txt14
8 GNU sed -i 's/hello/world/' file.txt
22’s Limitations and Non-limitations
For those who want to write portable
sed -i 's/hello/world/' file.txt22 scripts, be aware that some implementations have been known to limit line lengths [for the pattern and hold spaces] to be no more than 4000 bytes. The POSIX standard specifies that conforming
sed -i 's/hello/world/' file.txt22 implementations shall support at least 8192 byte line lengths. GNU
sed -i 's/hello/world/' file.txt22 has no built-in limit on line length; as long as it can
sed -i 's/hello/world/' file.txt0008 more [virtual] memory, you can feed or construct lines as long as you like
However, recursion is used to handle subpatterns and indefinite repetition. This means that the available stack space may limit the size of the buffer that can be processed by certain patterns
9 Other Resources for Learning About sed -i 's/hello/world/' file.txt
22
For up to date information about GNU
sed -i 's/hello/world/' file.txt22 please visit https. //www. gnu. org/software/sed/
Send general questions and suggestions to sed-devel@gnu. org. Visit the mailing list archives for past discussions at https. //lists. gnu. org/archive/html/sed-devel/
The following resources provide information about
sed -i 's/hello/world/' file.txt22 [both GNU
sed -i 's/hello/world/' file.txt22 and other variations]. Note these not maintained by GNU
sed -i 's/hello/world/' file.txt22 developers
10 Reporting Bugs
Email bug reports to bug-sed@gnu. org. Also, please include the output of ‘sed --version’ in the body of your report if at all possible
Please do not send a bug report like this
sed -i 's/hello/world/' file.txt15
If GNU
sed -i 's/hello/world/' file.txt22 doesn’t configure your favorite package, take a few extra minutes to identify the specific problem and make a stand-alone test case. Unlike other programs such as C compilers, making such test cases for
sed -i 's/hello/world/' file.txt22 is quite simple
A stand-alone test case includes all the data necessary to perform the test, and the specific invocation of
sed -i 's/hello/world/' file.txt22 that causes the problem. The smaller a stand-alone test case is, the better. A test case should not involve something as far removed from
sed -i 's/hello/world/' file.txt22 as “try to configure frobme-1. 3. 4”. Yes, that is in principle enough information to look for the bug, but that is not a very practical prospect
Here are a few commonly reported bugs that are not bugs
sed -i 's/hello/world/' file.txt71 command on the last line
Most versions of
sed -i 's/hello/world/' file.txt22 exit without printing anything when the
sed -i 's/hello/world/' file.txt71 command is issued on the last line of a file. GNU
sed -i 's/hello/world/' file.txt22 prints pattern space before exiting unless of course the
sed -i 's/hello/world/' file.txt45 command switch has been specified. This choice is by design
Default behavior [gnu extension, non-POSIX conforming]
To force POSIX-conforming behavior
sed -i 's/hello/world/' file.txt16
For example, the behavior of
would depend on whether foo has an even or an odd number of lines12. Or, when writing a script to read the next few lines following a pattern match, traditional implementations of
sed -i 's/hello/world/' file.txt22 would force you to write something like
sed -i 's/hello/world/' file.txt17
instead of just
sed -i 's/hello/world/' file.txt18
In any case, the simplest workaround is to use
sed -i 's/hello/world/' file.txt0024 in scripts that rely on the traditional behavior, or to set the
sed -i 's/hello/world/' file.txt72 variable to a non-empty valueXung độ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 uses the POSIX basic regular expression syntax. According to the standard, the meaning of some escape sequences is undefined in this syntax; notable in the case of
sed -i 's/hello/world/' file.txt22 are
sed -i 's/hello/world/' file.txt911,
sed -i 's/hello/world/' file.txt923,
sed -i 's/hello/world/' file.txt925,
sed -i 's/hello/world/' file.txt156,
sed -i 's/hello/world/' file.txt157,
sed -i 's/hello/world/' file.txt991,
sed -i 's/hello/world/' file.txt992,
sed -i 's/hello/world/' file.txt987,
sed -i 's/hello/world/' file.txt988,
sed -i 's/hello/world/' file.txt985, and
sed -i 's/hello/world/' file.txt986
As in all GNU programs that use POSIX basic regular expressions,
sed -i 's/hello/world/' file.txt22 interprets these escape sequences as special characters. So,
sed -i 's/hello/world/' file.txt0040 matches one or more occurrences of ‘x’.
sed -i 's/hello/world/' file.txt0041 matches either ‘abc’ or ‘def’
This syntax may cause problems when running scripts written for other
sed -i 's/hello/world/' file.txt22s. Some
sed -i 's/hello/world/' file.txt22 programs have been written with the assumption that
sed -i 's/hello/world/' file.txt911 and
sed -i 's/hello/world/' file.txt923 match the literal characters
sed -i 's/hello/world/' file.txt0046 and
sed -i 's/hello/world/' file.txt0047. Such scripts must be modified by removing the spurious backslashes if they are to be used with modern implementations of
sed -i 's/hello/world/' file.txt22, like GNU
sed -i 's/hello/world/' file.txt22
On the other hand, some scripts use s. abc\. def. g to remove occurrences of either
sed -i 's/hello/world/' file.txt0050 or
sed -i 's/hello/world/' file.txt0051. While this worked until
sed -i 's/hello/world/' file.txt22 4. 0. x, newer versions interpret this as removing the string
sed -i 's/hello/world/' file.txt0053. This is again undefined behavior according to POSIX, and this interpretation is arguably more robust. older
sed -i 's/hello/world/' file.txt22s, for example, required that the regex matcher parsed
sed -i 's/hello/world/' file.txt0055 as
sed -i 's/hello/world/' file.txt994 in the common case of escaping a slash, which is again undefined behavior; the new behavior avoids this, and this is good because the regex matcher is only partially under our control
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' sẽ không bao giờ mở để ghi tệp đã có trên đĩa. 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. For this same reason,
sed -i 's/hello/world/' file.txt22 does not let you use -i on a writable file in a read-only directory, and will break hard or symbolic links when -i is used on such a file
sed -i 's/hello/world/' file.txt0068 does not work [gives an error]
There is no line 0. 0 is a special address that is only used to treat addresses like
sed -i 's/hello/world/' file.txt0069 as active when the script starts. if you write
sed -i 's/hello/world/' file.txt0070 and the first line includes the word ‘abc’, then that match would be ignored because address ranges must span at least two lines [barring the end of the file]; but what you probably wanted is to delete every line up to the first one including ‘abc’, and this is obtained with
sed -i 's/hello/world/' file.txt0071
sed -i 's/hello/world/' file.txt0072 không phân biệt chữ hoa chữ thường
You are encountering problems with locales. POSIX mandates that
sed -i 's/hello/world/' file.txt0072 uses the current locale’s collation order – in C parlance, that means using
sed -i 's/hello/world/' file.txt0074 instead of
sed -i 's/hello/world/' file.txt0075. Some locales have a case-insensitive collation order, others don’t
Another problem is that
sed -i 's/hello/world/' file.txt0072 tries to use collation symbols. This only happens if you are on the GNU system, using GNU libc’s regular expression matcher instead of compiling the one supplied with GNU sed. In a Danish locale, for example, the regular expression
sed -i 's/hello/world/' file.txt0077 matches the string ‘aa’, because this is a single collating symbol that comes after ‘a’ and before ‘b’; ‘ll’ behaves similarly in Spanish locales, or ‘ij’ in Dutch locales
To work around these problems, which may cause bugs in shell scripts, set the
sed -i 's/hello/world/' file.txt0078 and
sed 's/hello/world/' input.txt > output.txt946 environment variables to ‘C’
sed 's/hello/world/' input.txt > output.txt926 does not clear pattern space
This happens if your input stream includes invalid multibyte sequences. POSIX mandates that such sequences are not matched by ‘. ’, so that ‘s/. *//’ will not clear pattern space as you would expect. In fact, there is no way to clear sed’s buffers in the middle of the script in most multibyte locales [including UTF-8 locales]. For this reason, GNU
sed -i 's/hello/world/' file.txt22 provides a ‘z’ command [for ‘zap’] as an extension
To work around these problems, which may cause bugs in shell scripts, set the
sed -i 's/hello/world/' file.txt0078 and
sed 's/hello/world/' input.txt > output.txt946 environment variables to ‘C’
Appendix A GNU Free Documentation License
Version 1. 3, 3 November 2008
sed -i 's/hello/world/' file.txt19
- PREAMBLE
The purpose of this License is to make a manual, textbook, or other functional and useful document free in the sense of freedom. to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others
This License is a kind of “copyleft”, which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General Public License, which is a copyleft license designed for free software
We have designed this License in order to use it for manuals for free software, because free software needs free documentation. a free program should come with manuals providing the same freedoms that the software does. But this License is not limited to software manuals; it can be used for any textual work, regardless of subject matter or whether it is published as a printed book. Chúng tôi khuyên dùng Giấy phép này chủ yếu cho các tác phẩm có mục đích hướng dẫn hoặc tham khảo
- APPLICABILITY AND DEFINITIONS
This License applies to any manual or other work, in any medium, that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License. Such a notice grants a world-wide, royalty-free license, unlimited in duration, to use that work under the conditions stated herein. The “Document”, below, refers to any such manual or work. Any member of the public is a licensee, and is addressed as “you”. You accept the license if you copy, modify or distribute the work in a way requiring permission under copyright law
A “Modified Version” of the Document means any work containing the Document or a portion of it, either copied verbatim, or with modifications and/or translated into another language
A “Secondary Section” is a named appendix or a front-matter section of the Document that deals exclusively with the relationship of the publishers or authors of the Document to the Document’s overall subject [or to related matters] and contains nothing that could fall directly within that overall subject. [Thus, if the Document is in part a textbook of mathematics, a Secondary Section may not explain any mathematics. ] The relationship could be a matter of historical connection with the subject or with related matters, or of legal, commercial, philosophical, ethical or political position regarding them
The “Invariant Sections” are certain Secondary Sections whose titles are designated, as being those of Invariant Sections, in the notice that says that the Document is released under this License. If a section does not fit the above definition of Secondary then it is not allowed to be designated as Invariant. The Document may contain zero Invariant Sections. If the Document does not identify any Invariant Sections then there are none
The “Cover Texts” are certain short passages of text that are listed, as Front-Cover Texts or Back-Cover Texts, in the notice that says that the Document is released under this License. A Front-Cover Text may be at most 5 words, and a Back-Cover Text may be at most 25 words
A “Transparent” copy of the Document means a machine-readable copy, represented in a format whose specification is available to the general public, that is suitable for revising the document straightforwardly with generic text editors or [for images composed of pixels] generic paint programs or [for drawings] some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup, or absence of markup, has been arranged to thwart or discourage subsequent modification by readers is not Transparent. An image format is not Transparent if used for any substantial amount of text. A copy that is not “Transparent” is called “Opaque”
Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standard-conforming simple HTML, PostScript or PDF designed for human modification. Examples of transparent image formats include PNG, XCF and JPG. Opaque formats include proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machine-generated HTML, PostScript or PDF produced by some word processors for output purposes only
The “Title Page” means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, “Title Page” means the text near the most prominent appearance of the work’s title, preceding the beginning of the body of the text
The “publisher” means any person or entity that distributes copies of the Document to the public
A section “Entitled XYZ” means a named subunit of the Document whose title either is precisely XYZ or contains XYZ in parentheses following text that translates XYZ in another language. [Here XYZ stands for a specific section name mentioned below, such as “Acknowledgements”, “Dedications”, “Endorsements”, or “History”. ] To “Preserve the Title” of such a section when you modify the Document means that it remains a section “Entitled XYZ” according to this definition
The Document may include Warranty Disclaimers next to the notice which states that this License applies to the Document. These Warranty Disclaimers are considered to be included by reference in this License, but only as regards disclaiming warranties. any other implication that these Warranty Disclaimers may have is void and has no effect on the meaning of this License
- VERBATIM COPYING
You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this License, the copyright notices, and the license notice saying this License applies to the Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to obstruct or control the reading or further copying of the copies you make or distribute. However, you may accept compensation in exchange for copies. If you distribute a large enough number of copies you must also follow the conditions in section 3
You may also lend copies, under the same conditions stated above, and you may publicly display copies
- COPYING IN QUANTITY
If you publish printed copies [or copies in media that commonly have printed covers] of the Document, numbering more than 100, and the Document’s license notice requires Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all these Cover Texts. Front-Cover Texts on the front cover, and Back-Cover Texts on the back cover. Both covers must also clearly and legibly identify you as the publisher of these copies. The front cover must present the full title with all words of the title equally prominent and visible. You may add other material on the covers in addition. Copying with changes limited to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be treated as verbatim copying in other respects
If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed [as many as fit reasonably] on the actual cover, and continue the rest onto adjacent pages
If you publish or distribute Opaque copies of the Document numbering more than 100, you must either include a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a computer-network location from which the general network-using public has access to download using public-standard network protocols a complete Transparent copy of the Document, free of added material. If you use the latter option, you must take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy [directly or through your agents or retailers] of that edition to the public
It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document
- MODIFICATIONS
You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modified Version under precisely this License, with the Modified Version filling the role of the Document, thus licensing distribution and modification of the Modified Version to whoever possesses a copy of it. In addition, you must do these things in the Modified Version
- Use in the Title Page [and on the covers, if any] a title distinct from that of the Document, and from those of previous versions [which should, if there were any, be listed in the History section of the Document]. You may use the same title as a previous version if the original publisher of that version gives permission
- List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modifications in the Modified Version, together with at least five of the principal authors of the Document [all of its principal authors, if it has fewer than five], unless they release you from this requirement
- Nêu trên trang Tiêu đề tên của nhà xuất bản Phiên bản đã sửa đổi, với tư cách là nhà xuất bản
- Preserve all the copyright notices of the Document
- Add an appropriate copyright notice for your modifications adjacent to the other copyright notices
- Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified Version under the terms of this License, in the form shown in the Addendum below
- Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document’s license notice
- Include an unaltered copy of this License
- Preserve the section Entitled “History”, Preserve its Title, and add to it an item stating at least the title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no section Entitled “History” in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modified Version as stated in the previous sentence
- Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document, and likewise the network locations given in the Document for previous versions it was based on. These may be placed in the “History” section. You may omit a network location for a work that was published at least four years before the Document itself, or if the original publisher of the version it refers to gives permission
- For any section Entitled “Acknowledgements” or “Dedications”, Preserve the Title of the section, and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein
- Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles
- Delete any section Entitled “Endorsements”. Such a section may not be included in the Modified Version
- Do not retitle any existing section to be Entitled “Endorsements” or to conflict in title with any Invariant Section
- Preserve any Warranty Disclaimers
If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document, you may at your option designate some or all of these sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version’s license notice. These titles must be distinct from any other section titles
You may add a section Entitled “Endorsements”, provided it contains nothing but endorsements of your Modified Version by various parties—for example, statements of peer review or that the text has been approved by an organization as the authoritative definition of a standard
You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover Text and one of Back-Cover Text may be added by [or through arrangements made by] any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one
The author[s] and publisher[s] of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modified Version
- COMBINING DOCUMENTS
You may combine the Document with other documents released under this License, under the terms defined in section 4 above for modified versions, provided that you include in the combination all of the Invariant Sections of all of the original documents, unmodified, and list them all as Invariant Sections of your combined work in its license notice, and that you preserve all their Warranty Disclaimers
The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents, make the title of each such section unique by adding at the end of it, in parentheses, the name of the original author or publisher of that section if known, or else a unique number. Make the same adjustment to the section titles in the list of Invariant Sections in the license notice of the combined work
In the combination, you must combine any sections Entitled “History” in the various original documents, forming one section Entitled “History”; likewise combine any sections Entitled “Acknowledgements”, and any sections Entitled “Dedications”. You must delete all sections Entitled “Endorsements. ”
- COLLECTIONS OF DOCUMENTS
You may make a collection consisting of the Document and other documents released under this License, and replace the individual copies of this License in the various documents with a single copy that is included in the collection, provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects
You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy of this License into the extracted document, and follow this License in all other respects regarding verbatim copying of that document
- AGGREGATION WITH INDEPENDENT WORKS
A compilation of the Document or its derivatives with other separate and independent documents or works, in or on a volume of a storage or distribution medium, is called an “aggregate” if the copyright resulting from the compilation is not used to limit the legal rights of the compilation’s users beyond what the individual works permit. When the Document is included in an aggregate, this License does not apply to the other works in the aggregate which are not themselves derivative works of the Document
If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document is less than one half of the entire aggregate, the Document’s Cover Texts may be placed on covers that bracket the Document within the aggregate, or the electronic equivalent of covers if the Document is in electronic form. Otherwise they must appear on printed covers that bracket the whole aggregate
- TRANSLATION
Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License, and all the license notices in the Document, and any Warranty Disclaimers, provided that you also include the original English version of this License and the original versions of those notices and disclaimers. In case of a disagreement between the translation and the original version of this License or a notice or disclaimer, the original version will prevail
If a section in the Document is Entitled “Acknowledgements”, “Dedications”, or “History”, the requirement [section 4] to Preserve its Title [section 1] will typically require changing the actual title
- TERMINATION
You may not copy, modify, sublicense, or distribute the Document except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, or distribute it is void, and will automatically terminate your rights under this License
However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated [a] provisionally, unless and until the copyright holder explicitly and finally terminates your license, and [b] permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation
Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License [for any work] from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice
Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, receipt of a copy of some or all of the same material does not give you any rights to use it
- FUTURE REVISIONS OF THIS LICENSE
Tổ chức Phần mềm Tự do thỉnh thoảng có thể xuất bản các phiên bản mới, sửa đổi của Giấy phép Tài liệu Miễn phí GNU. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. See https. //www. gnu. org/copyleft/
Each version of the License is given a distinguishing version number. If the Document specifies that a particular numbered version of this License “or any later version” applies to it, you have the option of following the terms and conditions either of that specified version or of any later version that has been published [not as a draft] by the Free Software Foundation. If the Document does not specify a version number of this License, you may choose any version ever published [not as a draft] by the Free Software Foundation. If the Document specifies that a proxy can decide which future versions of this License can be used, that proxy’s public statement of acceptance of a version permanently authorizes you to choose that version for the Document
- GIẤY PHÉP
“Massive Multiauthor Collaboration Site” [or “MMC Site”] means any World Wide Web server that publishes copyrightable works and also provides prominent facilities for anybody to edit those works. A public wiki that anybody can edit is an example of such a server. A “Massive Multiauthor Collaboration” [or “MMC”] contained in the site means any set of copyrightable works thus published on the MMC site
“CC-BY-SA” means the Creative Commons Attribution-Share Alike 3. 0 license published by Creative Commons Corporation, a not-for-profit corporation with a principal place of business in San Francisco, California, as well as future copyleft versions of that license published by that same organization
“Incorporate” means to publish or republish a Document, in whole or in part, as part of another Document
An MMC is “eligible for relicensing” if it is licensed under this License, and if all works that were first published under this License somewhere other than this MMC, and subsequently incorporated in whole or in part into the MMC, [1] had no cover texts or invariant sections, and [2] were thus incorporated prior to November 1, 2008
The operator of an MMC Site may republish an MMC contained in the site under CC-BY-SA on the same site at any time before August 1, 2009, provided the MMC is eligible for relicensing
ADDENDUM. How to use this License for your documents
To use this License in a document you have written, include a copy of the License in the document and put the following copyright and license notices just after the title page
sed -i 's/hello/world/' file.txt20
If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the “with…Texts. ” line with this
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