In the process of converting a blog from WordPress to Hugo, I find that I need to do a global search and replace for tags to convert them to a list of strings.
Currently I have something like this:
tags: owl, owl2, rdfs, rdf, snippets, vscode, visual studio code
But I need something like this:
tags: ["owl", "owl2", "rdfs", "rdf", "snippets", "vscode", "visual studio code"]
I have a variety of editors such as VSCode, VS22, Neovim, Rider etc. at my disposal as well as sed, gawk etc, so any solution will do.
2
Answers
To find all these with such a pattern:
^(tags: )|(, )|$
.Then the expected result for a given string can be achieved in 3 steps using VSCode:
tags:
(note there is a space) usingcommand+F
/cntrl+F
and to aReplace
field add a stringtags: ["
.,
(note there is a space) and replace it with", "
.$
orn
(in a search dialog select Use Regular Expression option) and replace it with"]
.I would harness GNU
sed
for this task following way, letfile.txt
content bethen
gives output
Explanation: I give three instructions
["
once (i.e. it will affect only first space)"]
(i.e. append"]
),
using", "
globally (i.e. it wil affect all,
)(tested in GNU sed 4.7)