[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Including ' in a sed command



On Thu, 7 Aug 2025, The Wanderer wrote:

On 2025-08-07 at 22:17, Tim Woodall wrote:

iHi All,

(Typed, rather than cut/pasted so apologies if there's a typo)

This is part of a sed command in a bash script.

sed 's/["'"'"']//'

This matches " or '. It works.

But it's 'unreadable' and almost impossible to find the typo if it's
 wrong.

Is there a better way of writing it? the obvious '["\']' doesn't
work.

That depends on your threshold for "better".

The problem, of course, is the overlap in characters between those
needed for the s-expression and those parsed by the shell.

My best stab at it, which is only arguably better, looks like:

sed 's/["'\'']//'


I like that. Thanks!


Alternatively you could probably do

sed s/\[\"\'\]//

I didn't mention it but this hits the same snag as "[\"']", the real expression is much more complex, approximately:

s/.*\$ref["']\{0,1\}: *["']\([^"']*\)["'].*/\1/

which gets messy with \\\$ etc malarky without the single quotes.

Due to "official incompetence" I'm forced into scraping a malformed yaml and json openapi specification from the web so that I can then fixup things like illegal tabs in yaml, invalid trailing comnas in json, to the point that it can actually be loaded by python yaml/json modules.

This is a crude 'first pass' downloader that does just enough to download all the files I need.

Fortunately, at least so far, I've not had to cope with multiple $ref on a single line :-)

Tim.


Reply to: