Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Given the below json example - how would I replace the 7 characters with a different string after the hash for module2 from a unix command line?
{
"module": "git+https://9gke37d40a7a9f84659e98f386d4ea2fda2a5d86:x-oauth-basic@github.com/org-name/repo.git#71e84fd",
"module2": "git+https://9gke37d40a7a9f9999e98f386d4ea2fda2a5d86:x-oauth-basic@github.com/org-name/repo.git#99h43ae",
"module3": "git+https://9gke37d40a7a9f33759e98f386d4ea2fda2a5d6:x-oauth-basic@github.com/org-name/repo.git#94i53bb"
}
@rafaelrinaldi

This comment has been minimized.

Copy link

rafaelrinaldi commented Oct 28, 2015

This will replace every nth occurrence (module2 in this case) with __foo__:

$ sed -e 's/#.*$/__foo__",/g;n' /tmp/sed-regex-question.json

{
 "module": "git+https://9gke37d40a7a9f84659e98f386d4ea2fda2a5d86:x-oauth-basic@github.com/org-name/repo.git#71e84fd",
 "module2": "git+https://9gke37d40a7a9f9999e98f386d4ea2fda2a5d86:x-oauth-basic@github.com/org-name/repo.git__foo__",
 "module3": "git+https://9gke37d40a7a9f33759e98f386d4ea2fda2a5d6:x-oauth-basic@github.com/org-name/repo.git#94i53bb"
}
@Shadow6363

This comment has been minimized.

Copy link

Shadow6363 commented Oct 28, 2015

Alternatively, you can specify an address regex for the substitution:
sed -e '/"module2"/ s/#.*$/__foo__",/g' /tmp/sed-regex-question.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.