gpt4 book ai didi

regex - 使用sed over SSH使用正则表达式从文件中删除文本

转载 作者:行者123 更新时间:2023-12-02 14:30:44 25 4
gpt4 key购买 nike

我试图通过SSH使用sed命令从文件中删除文本。该命令是:

sed -i -r '/(\#\#Some\ text\ domain\.com\ some\ text)((.*?(\n))+.*?)(\#\#Some\ text\ domain\.com\ some\ text)/d' file

该文件中包含以下文本:
##Some text domain.com some text
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType image/svg "access 1 month"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType text/javascript "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/xhtml+xml "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 month"
ExpiresDefault "access 1 month"
</IfModule>

<ifModule mod_headers.c>
<filesMatch ".(css|jpg|jpeg|png|gif|swf|svg|js|ico)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<filesMatch ".(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</filesMatch>
</ifModule>

<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/atom+xml
AddOutputFilterByType DEFLATE application/rdf+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-woff
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/truetype
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
</IfModule>
##Some text domain.com some text

我使用了下一个工具来验证我的常规技能:

https://regex101.com/

并确认同样有效,因此我想问题出在sed命令中。任何想法都非常感谢!

先感谢您 :)

最佳答案

由于您编写了可工作的PCRE / Perl兼容表达式,因此将其与perl结合使用:

perl -0777 -i 's/(\#\#Some\ text\ domain\.com\ some\ text)((.*?(\n))+.*?)(\#\#Some\ text\ domain\.com\ some\ text)//g' file
也就是说, -0777将文件压缩为单个文本,其中换行符可以与regex模式匹配, -i将内联更改, s中的 s/表示替换,该模式是regex101.com上的工作模式,并且替换模式为空。 g表示替换所有匹配项。

关于regex - 使用sed over SSH使用正则表达式从文件中删除文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47911291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com