简体   繁体   English

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

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

In my sitemap file i want to replace :(colon) with _ (underscore). 在我的站点地图文件中,我想用_(下划线)替换:(冒号)。 But I don't want to replace other : in file such as timestamp. 但是我不想替换诸如时间戳等文件中的other。

my line 我的线

    <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>

In this line "sitemap_2014-09-27_09:42:43_1.xml.gz" should become "sitemap_2014-09-27_09_42_43_1.xml.gz" but timestamp in should not affect. 在这一行中,“ sitemap_2014-09-27_09:42:43_1.xml.gz”应变为“ sitemap_2014-09-27_09_42_43_1.xml.gz”,但时间戳不会影响。

I am new to sed and patterns. 我是sed和pattern的新手。 Can someone provide sed or replace or any other linux command to replace this pattern in a file. 有人可以提供sed或replace或任何其他linux命令来替换文件中的此模式。

You could try this GNU sed command, 您可以尝试使用此GNU sed命令,

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

Through basic sed, 通过基本的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