简体   繁体   中英

Understanding sed command syntax and sed commands

Could someone explain what this sed command does here?

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)

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

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

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.

Hopefully you can work out what the last one does, based on my explanation of the first one!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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