Skip to content

How to remove '/' from keys in a .yaml file if the '/' is the first character #1560

Answered by mikefarah
williamj20 asked this question in Q&A
Discussion options

You must be logged in to vote

You can use the sub operator (see https://mikefarah.gitbook.io/yq/operators/string-operators#sub-regex-replacement) like this:

sample file (next time please provide one)

- /a/path
- something/else
yq '.[] |= sub("^/", "")' file.yaml

output:

- a/path
- something/else

Explanation:

  • .[] will match all the elements in the array
  • |= will update each element, with respect to itself
  • sub("^/", "") matches a leading / and replaces it with an empty string

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by williamj20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants