简体   繁体   English

了解sed命令的语法和sed命令

[英]Understanding sed command syntax and sed commands

Could someone explain what this sed command does here? 有人可以在这里解释这个sed命令做什么吗?

sed 's!^M$!!;s!\-!!g;s!\.!!g;s!\(..\)!\1:!g;s!:$!!'

It seems replacing/deleting some characters... But I couldn't figure it out... It's really complicated (I mean all of those s ; / g M ^ . and other characters) 似乎替换/删除了一些字符...但是我无法弄清楚...这真的很复杂(我的意思是所有这些s ; / g M ^ 。和其他字符)

thanx regards 谢谢

You can split it up into a series of substitutions: 您可以将其拆分为一系列替换:

s!^M$!!
s!\-!!g
s!\.!!g
s!\(..\)!\1:!g
s!:$!!

Each one is using ! 每个人都在使用! as the delimiter, so the patterns are s!match!replacement! 作为定界符,因此模式是s!match!replacement! . The g on the end means that some of them are global, so will happen as many times as possible rather than only once on each line. 最后的g表示其中一些是全局的,因此将尽可能多地发生,而不是每行仅发生一次。

^ matches the start of the line and $ matches the end, so the first one removes any M s that are found on a line by themselves. ^匹配行的开始, $匹配行的结束,因此第一个将删除行中发现的所有M

The next two remove all . 接下来的两个全部删除. and - that are found. -被发现。 The . . needs a slash before it so that it only matches a literal . 在它之前需要一个斜杠,以便它仅与文字匹配. rather than matching any character . 而不是匹配任何字符 The - doesn't need a slash before it but it doesn't do any harm either. -不需要在其前面加上斜杠,但也没有任何害处。

The fourth one adds a : after every 2 characters, using a capture group and back reference. 第四个使用捕获组和反向引用在每2个字符后添加一个:

Hopefully you can work out what the last one does, based on my explanation of the first one! 希望您能根据我对第一个的解释得出最后一个的结果!

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

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