Cube.
Cube.

Reputation: 513

Replace last occurrence of space with sed

I need to replace the last occurrence of space in an input file, using sed.

What I came up with is

sed "s/([ ])[0-9]*$/,/g"

However, it does not seem to want to remember the space which it's supposed to replace. Running the command without round brackets works fine (for what it's supposed to do - replace the space and the chain of numbers). When I add the brackets, it does nothing.

Yes, I am aware of this solution, however when trying to pass \1 to sed, it screams that "\1 not defined in the RE".

Anyone care to help? It seems to be a simple issue, I'd be glad to know the solution.

Upvotes: 1

Views: 589

Answers (1)

DigitalRoss
DigitalRoss

Reputation: 146151

This seemed to work "the first time" (yay) ...

$ sed -e 's/ \([^ ][^ ]*\)$/,\1/' /etc/hosts

Upvotes: 1

Related Questions