繁体   English   中英

用sed替换文件中的特定模式或替换

[英]Replace specific pattern in a files with sed or replace

在我的站点地图文件中,我想用_(下划线)替换:(冒号)。 但是我不想替换诸如时间戳等文件中的other。

我的线

    <sitemap><loc>http://www.example.com/sitemap_2014-09-27_09:42:43_1.xml.gz</loc>
<lastmod>2014-09-27 09:42:43</lastmod></sitemap>

在这一行中,“ sitemap_2014-09-27_09:42:43_1.xml.gz”应变为“ sitemap_2014-09-27_09_42_43_1.xml.gz”,但时间戳不会影响。

我是sed和pattern的新手。 有人可以提供sed或replace或任何其他linux命令来替换文件中的此模式。

您可以尝试使用此GNU sed命令,

sed -r 's/([^ ])([0-9]{2}):([0-9]{2}):([0-9]{2})/\1\2_\3_\4/g' file

通过基本的sed

sed 's/\([^ ]\)\([0-9]\{2\}\):\([0-9]\{2\}\):\([0-9]\{2\}\)/\1\2_\3_\4/g' file

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM