Python thay thế nhiều dòng trong tệp
Câu hỏi. Tôi có một tệp văn bản mà tôi muốn thay đổi nhiều dòng văn bản thành một nội dung khác nhưng không sử dụng trình soạn thảo văn bản. Có cách nào để tìm và thay thế mẫu chuỗi nhiều dòng từ dòng lệnh Linux không? Show
Giả sử bạn có một tệp văn bản giống như sau Beginning of a text file. This is a cat. This is a dog. This is a guinea pig. This is a gecko. This is a hamster. This is a parrot. This is a rabbit. This is a ferret. This is a turtle. Bạn muốn thay thế nhiều dòng được đánh dấu bằng một dòng sau nhưng không sử dụng trình soạn thảo văn bản Beginning of a text file. This is a cat. This is a dog. These were removed. This is a parrot. This is a rabbit. This is a ferret. This is a turtle. Tìm kiếm và thay thế một mẫu chuỗi nhiều dòngKhi nói đến việc tìm và thay thế một mẫu chuỗi nhiều dòng, khớp mẫu dựa trên biểu thức chính quy của Perl rất hữu ích Một lớp lót sau đây có thể hoàn thành công việc $ perl -i -0pe 's///' input.txt Một vài lưu ý trong biểu thức trên
Trong ví dụ cụ thể này, lệnh sau sẽ thay thế một chuỗi nhiều dòng theo yêu cầu và hiển thị kết quả (không áp dụng thay đổi đối với tệp đầu vào) $ perl -0pe 's/This is a guinea pig.nThis is a gecko.nThis is a hamster.n/These were removed.n/' input.txt Sau khi xác nhận rằng kết quả là chính xác, bạn có thể áp dụng thay đổi cho tệp đầu vào bằng cách thêm tùy chọn $ perl -i -0pe 's/This is a guinea pig.nThis is a gecko.nThis is a hamster.n/These were removed.n/' input.txt Dưới đây là một số biến thể Để tìm mẫu chuỗi nhiều dòng theo kiểu không phân biệt chữ hoa chữ thường $ perl -i -0pe 's///i' input.txt Để xóa mẫu chuỗi nhiều dòng $ perl -i -0pe 's///' input.txt Tìm kiếm và thay thế chuỗi nhiều dòng trong nhiều tệpNếu bạn muốn tìm và thay thế một chuỗi nhiều dòng trong nhiều tệp, bạn có thể sử dụng kết hợp Beginning of a text file. This is a cat. This is a dog. These were removed. This is a parrot. This is a rabbit. This is a ferret. This is a turtle.3 và Beginning of a text file. This is a cat. This is a dog. These were removed. This is a parrot. This is a rabbit. This is a ferret. This is a turtle.4 như sau $ find ~/doc -name "*.txt" | xargs perl -i -0pe 's///' Tìm kiếm một lớp lót này cho tất cả các tệp văn bản trong thư mục Beginning of a text file. This is a cat. This is a dog. These were removed. This is a parrot. This is a rabbit. This is a ferret. This is a turtle.5 và áp dụng thay thế chuỗi nhiều dòng trong mỗi tệp Trong bài đăng này, chúng ta sẽ xem cách sử dụng mô-đun Ansible lineinfile để thay thế nhiều Dòng cùng một lúc. Cách sử dụng nhiều biểu thức chính quy hoặc Regex cùng một lúc. Cách khớp nhiều dòng Đối với ví dụ này, chúng tôi sẽ lấy tệp apache Nói một cách đơn giản, chúng tôi sẽ di chuyển trang web/máy chủ ảo hiện tại của chúng tôi sang trang web/máy chủ ảo mới Trước khi có bất kỳ sửa đổi nào, Lệnh Ad Hoc nhanh để kiểm tra giá trị cấu hình hiện tại là gì
Mục lục Có thể bạn quan tâmLệnh nhanh để kiểm tra cấu hình hiện tại là gì$ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts mwiweb02 | CHANGED | rc=0 >> 88:ServerAdmin [email protected] 99:ServerName www.middlewareinventory.com:80 123:DocumentRoot "/var/www/html" 193:LogLevel warn
Với sự trợ giúp của lệnh ansible, ad hoc và grep, chúng tôi có thể nhận được các giá trị Cấu hình hiện tại trên máy chủ từ xa httpd. tập tin conf Bây giờ, chúng ta hãy viết Playbook để Thay đổi tất cả các giá trị này cùng một lúc theo cách Nhiều Tìm kiếm và Thay thế
Ansible Lineinfile Playbook để thay thế nhiều dòngĐây là playbook để thay thế nhiều dòng bằng mô-đun lineinfile. Chúng tôi đã sử dụng câu lệnh lặp/vòng lặp Trong mô-đun lineinfile, tham số regex được sử dụng để giữ Chuỗi mà chúng tôi đang tìm kiếm và dòng là giữ dòng mà chúng tôi muốn thay thế bằng Có nói rằng. Bây giờ hãy nhìn vào Playbook --- - name: Examples of lineinfile hosts: web tasks: - name: "Ansible Lineinfile Multiple Lines" become: yes become_user: root lineinfile: path: /etc/httpd/conf/httpd.conf # Line to Search/Match against regexp: '{{item.From}}' # Line to Replace with line: '{{item.To}}' state: present # To validate the Changes Before Committing/Saving validate: "httpd -t -f %s" with_items: - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'} - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'} - { From: '^LogLevel .*', To: 'LogLevel debug'} - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}
Trong with_items, mỗi Mục có hai giá trị --- - name: Examples of lineinfile hosts: web tasks: - name: "Ansible Lineinfile Multiple Lines" become: yes become_user: root lineinfile: path: /etc/httpd/conf/httpd.conf # Line to Search/Match against regexp: '{{item.From}}' # Line to Replace with line: '{{item.To}}' state: present # To validate the Changes Before Committing/Saving validate: "httpd -t -f %s" with_items: - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'} - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'} - { From: '^LogLevel .*', To: 'LogLevel debug'} - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}0 Nơi đây,
--- - name: Examples of lineinfile hosts: web tasks: - name: "Ansible Lineinfile Multiple Lines" become: yes become_user: root lineinfile: path: /etc/httpd/conf/httpd.conf # Line to Search/Match against regexp: '{{item.From}}' # Line to Replace with line: '{{item.To}}' state: present # To validate the Changes Before Committing/Saving validate: "httpd -t -f %s" with_items: - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'} - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'} - { From: '^LogLevel .*', To: 'LogLevel debug'} - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}2 trong tham số --- - name: Examples of lineinfile hosts: web tasks: - name: "Ansible Lineinfile Multiple Lines" become: yes become_user: root lineinfile: path: /etc/httpd/conf/httpd.conf # Line to Search/Match against regexp: '{{item.From}}' # Line to Replace with line: '{{item.To}}' state: present # To validate the Changes Before Committing/Saving validate: "httpd -t -f %s" with_items: - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'} - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'} - { From: '^LogLevel .*', To: 'LogLevel debug'} - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}3 và --- - name: Examples of lineinfile hosts: web tasks: - name: "Ansible Lineinfile Multiple Lines" become: yes become_user: root lineinfile: path: /etc/httpd/conf/httpd.conf # Line to Search/Match against regexp: '{{item.From}}' # Line to Replace with line: '{{item.To}}' state: present # To validate the Changes Before Committing/Saving validate: "httpd -t -f %s" with_items: - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'} - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'} - { From: '^LogLevel .*', To: 'LogLevel debug'} - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}0 sẽ được gọi là --- - name: Examples of lineinfile hosts: web tasks: - name: "Ansible Lineinfile Multiple Lines" become: yes become_user: root lineinfile: path: /etc/httpd/conf/httpd.conf # Line to Search/Match against regexp: '{{item.From}}' # Line to Replace with line: '{{item.To}}' state: present # To validate the Changes Before Committing/Saving validate: "httpd -t -f %s" with_items: - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'} - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'} - { From: '^LogLevel .*', To: 'LogLevel debug'} - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}5 trong tham số dòng
Xác thực Cấu hình bằng lệnh AD HOC$ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts mwiweb02 | CHANGED | rc=0 >> 88:ServerAdmin [email protected] 99:ServerName www.devopsjunction.com:80 123:DocumentRoot "/var/www/devopsjunction" 193:LogLevel debug
Bạn có thể thấy Yêu cầu của chúng tôi đã hoàn thành. Chúng tôi đã di chuyển thành công Máy chủ ảo/Trang web từ miền này sang Miền khác. Cảm ơn Ansible lineinfile
Một cảnh báo nhanh mà tôi muốn đưa ra ở đây là Ansible lineinfile sẽ chỉ xem xét dòng phù hợp cuối cùng, khi có nhiều hơn một mục bạn muốn thay thế của cùng một chuỗi/từ khóa. Bạn nên sử dụng mô-đun --- - name: Examples of lineinfile hosts: web tasks: - name: "Ansible Lineinfile Multiple Lines" become: yes become_user: root lineinfile: path: /etc/httpd/conf/httpd.conf # Line to Search/Match against regexp: '{{item.From}}' # Line to Replace with line: '{{item.To}}' state: present # To validate the Changes Before Committing/Saving validate: "httpd -t -f %s" with_items: - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'} - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'} - { From: '^LogLevel .*', To: 'LogLevel debug'} - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}6 Đây là ví dụ nhanh
Thay thế nhiều dòng và toàn bộ bằng mô-đun Thay thếĐôi khi bạn muốn thay thế nhiều mục nhập của một số chuỗi hoặc từ. Ngoài ra, bạn sẽ có một danh sách các chuỗi được thay thế ví dụ: giả sử bạn muốn thay thế nhiều chuỗi và nhiều mục nhập của chúng. Bạn không thể sử dụng mô-đun --- - name: Examples of lineinfile hosts: web tasks: - name: "Ansible Lineinfile Multiple Lines" become: yes become_user: root lineinfile: path: /etc/httpd/conf/httpd.conf # Line to Search/Match against regexp: '{{item.From}}' # Line to Replace with line: '{{item.To}}' state: present # To validate the Changes Before Committing/Saving validate: "httpd -t -f %s" with_items: - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'} - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'} - { From: '^LogLevel .*', To: 'LogLevel debug'} - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}7 như đã đề cập trước đó Chúng ta hãy xem xét câu chuyện truyền thống --- - name: Examples of lineinfile hosts: web tasks: - name: "Ansible Lineinfile Multiple Lines" become: yes become_user: root lineinfile: path: /etc/httpd/conf/httpd.conf # Line to Search/Match against regexp: '{{item.From}}' # Line to Replace with line: '{{item.To}}' state: present # To validate the Changes Before Committing/Saving validate: "httpd -t -f %s" with_items: - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'} - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'} - { From: '^LogLevel .*', To: 'LogLevel debug'} - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}8 được lưu vào một tệp có tên --- - name: Examples of lineinfile hosts: web tasks: - name: "Ansible Lineinfile Multiple Lines" become: yes become_user: root lineinfile: path: /etc/httpd/conf/httpd.conf # Line to Search/Match against regexp: '{{item.From}}' # Line to Replace with line: '{{item.To}}' state: present # To validate the Changes Before Committing/Saving validate: "httpd -t -f %s" with_items: - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'} - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'} - { From: '^LogLevel .*', To: 'LogLevel debug'} - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}9 A Fox one day spied a beautiful bunch of ripe grapes hanging from a vine trained along the branches of a tree. The grapes seemed ready to burst with juice, and the Fox's mouth watered as he gazed longingly at them. The bunch hung from a high branch, and the Fox had to jump for it. The first time he jumped he missed it by a long way. So he walked off a short distance and took a running leap at it, only to fall short once more. Again and again he tried, but in vain. Now he sat down and looked at the grapes in disgust. "What a fool I am," he said. "Here I am wearing myself out to get a bunch of sour grapes that are not worth gaping for." And off he walked very, very scornfully. There are many who pretend to despise and belittle that which is beyond their reach.
Tại đây Nếu bạn muốn thực hiện một vài thay đổi như thế này
Bạn có thể nhận thấy có nhiều mục từ $ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts mwiweb02 | CHANGED | rc=0 >> 88:ServerAdmin [email protected] 99:ServerName www.devopsjunction.com:80 123:DocumentRoot "/var/www/devopsjunction" 193:LogLevel debug0 và $ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts mwiweb02 | CHANGED | rc=0 >> 88:ServerAdmin [email protected] 99:ServerName www.devopsjunction.com:80 123:DocumentRoot "/var/www/devopsjunction" 193:LogLevel debug2 trong câu chuyện đó Với playbook sau, bạn có thể thay đổi toàn bộ câu chuyện --- - name: Change Fox to Bear and Grape to Apple hosts: localhost tasks: - name: Replace multiple lines and entries replace: path: story # Line to Search/Match against regexp: '{{item.From}}' # Line to Replace with replace: '{{item.To}}' with_items: - { From: '[fF]ox', To: 'Bear'} - { From: '[Gg]rapes', To: 'Apples'}
Đây là một ví dụ đơn giản tôi đã giữ để làm cho mọi thứ dễ hiểu. Các mô-đun thay thế và dòng trong tệp ansible đều hỗ trợ $ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts mwiweb02 | CHANGED | rc=0 >> 88:ServerAdmin [email protected] 99:ServerName www.devopsjunction.com:80 123:DocumentRoot "/var/www/devopsjunction" 193:LogLevel debug7 Ngoài ra, mô-đun thay thế cũng hỗ trợ các yếu tố lựa chọn như $ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts mwiweb02 | CHANGED | rc=0 >> 88:ServerAdmin [email protected] 99:ServerName www.devopsjunction.com:80 123:DocumentRoot "/var/www/devopsjunction" 193:LogLevel debug8 và $ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts mwiweb02 | CHANGED | rc=0 >> 88:ServerAdmin [email protected] 99:ServerName www.devopsjunction.com:80 123:DocumentRoot "/var/www/devopsjunction" 193:LogLevel debug9 Tham khảo các bài viết sau để biết thêm chi tiết và ví dụ về Thay thế và dòng trong tệp
Đây là cách chúng tôi có thể thực hiện Nhiều Tìm kiếm và Thay thế bằng Ansible lineinfile và thay thế các mô-đun Hi vọng điêu nay co ich Chúc mừng Theo dõi tôi trên Linkedin Hồ sơ của tôi Đăng ký nội dung độc quyền "chỉ dành cho người đăng ký" Tên* E-mail* Thêm từ Khoảng không quảng cáo phần mềm trung gian
|