You are not logged in.

#1 2016-08-24 10:39:25

marius-arc
Member
Registered: 2016-04-17
Posts: 42

[SOLVED ]Understanding this small sed piece

Every once in a while I get overly focused on understanding something, and this is one I'm having a hard time googling. Could someone please explain to me what this does:

sed -e 's|/$||

As far as I can understand it's a substitution/removal with the delimiter changed to pipe. What exactly does it remove? $? /$? Something else? If it was backslash then $ would be removed, but forward slash? I've tried various echo tests but can't really figure it out.

Last edited by marius-arc (2016-08-24 12:59:56)

Offline

#2 2016-08-24 10:42:20

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: [SOLVED ]Understanding this small sed piece

It removes any backslash forward slash which occurs at the end of a line.

Offline

#3 2016-08-24 10:54:12

marius-arc
Member
Registered: 2016-04-17
Posts: 42

Re: [SOLVED ]Understanding this small sed piece

doooooh
Thank you so much!

Offline

#4 2016-08-24 11:14:51

Awebb
Member
Registered: 2010-05-06
Posts: 6,275

Re: [SOLVED ]Understanding this small sed piece

Just to understand why you didn't understand... would you understand the following?

sed "s/\/$//"

Last edited by Awebb (2016-08-24 11:15:54)

Offline

#5 2016-08-24 12:59:41

marius-arc
Member
Registered: 2016-04-17
Posts: 42

Re: [SOLVED ]Understanding this small sed piece

Awebb wrote:

Just to understand why you didn't understand... would you understand the following?

sed "s/\/$//"

I had written a whole bunch trying to figure that one out, but in the end isn't it the same as the original one, only different delimiter?
That is assuming there is no difference between " and ' and with/without -e

Offline

#6 2016-08-24 16:03:53

Awebb
Member
Registered: 2010-05-06
Posts: 6,275

Re: [SOLVED ]Understanding this small sed piece

Indeed, the delimiters are different, but it does the same. I'd say the difference between double quotes and single quotes is irrelevant to sed. You use single quotes, if you want to prevent shells like sh and bash from expanding variables (or trying to expand anything like $text as a variable) and prevent globbing (although I'm not sure about the existence of edge cases). My question was meant to see, whether you were only confused by the pipe delimiters.

EDIT: To clear this up, sed treats everything after the command (s in this case) as a delimiter. You could also write sed 's /$  ', although I'm not sure whether this is understood by every implementation. See: http://backreference.org/2010/02/20/usi … rs-in-sed/

EDIT: Since I feel like phrasing today: The first instance of the delimiter defines the delimiter for the rest of the expression. If your delimiter appears too often in your expression as part of the expression, consider using another symbol to minimize escaping. The pipe was chosen here, because the single quotes defuse the pipe.

Last edited by Awebb (2016-08-24 16:12:32)

Offline

#7 2016-08-25 09:22:50

marius-arc
Member
Registered: 2016-04-17
Posts: 42

Re: [SOLVED ]Understanding this small sed piece

Thanks for the explanation!

Offline

Board footer

Powered by FluxBB