Skip to content

Instantly share code, notes, and snippets.

@mrmrs
Created October 28, 2015 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmrs/1783b78ebdb682a8edb1 to your computer and use it in GitHub Desktop.
Save mrmrs/1783b78ebdb682a8edb1 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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