bash - Can't match regex with sed -
i'm trying match pattern (\^|\~?)(\d|x|\*)+\.(\d|x|\*)+\.(\d|x|\*)+
sed
without luck. file i'm running through this:
{ "name": "something", "version": "0.0.1", "description": "some desc", "main": "gulpfile.js", "directories": { "test": "tests" }, "dependencies": { "babel-polyfill": "^6.7.4", "babel-preset-es2015": "^6.6.0", "babel-preset-react": "^6.5.0", "gulp-clean": "^0.3.2", "jquery": "^2.1.4", "lodash": "^4.0.0", "moment": "^2.13.0", "moment-timezone": "^0.5.0", "radium": "^0.16.2", "react": "^15.1.0", "react-bootstrap-sweetalert": "^1.1.10", "react-dom": "^15.1.0", "react-timeago": "^2.2.1", "sprintf": "^0.1.5", "smoothscroll": "~0.2.2" }, "devdependencies": { "babel": "^6.3.26", "babelify": "^7.2.0", "browserify": "~12.0.1", "console-stamp": "^0.2.0", "estraverse-fb": "^1.3.1", "gulp": "^3.9.0", "gulp-concat": "^2.6.0", "gulp-sass": "^2.1.1", "gulp-sourcemaps": "^1.6.0", "gulp-util": "^3.0.7", "lodash": "4.5.1", "lodash.assign": "^3.2.0", "lodash.isfunction": "^3.0.8", "lodash.reduce": "^4.3.0", "node-sass": "3.4.2", "react-bootstrap": "^0.29.4", "react-intl": "2.1.0", "reactify": "1.1.1", "sweetalert": "^1.1.3", "vinyl": "^1.1.0", "vinyl-buffer": "^1.0.0", "vinyl-source-stream": "^1.1.0", "watchify": "^3.4.0", "jsx-to-string": "~0.2.11" }, "optionaldependencies": { "pkg-save": "~1.0.2" }, "scripts": { "test": "echo \"error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "someurl" }, "author": "authorname", "license": "mit" }
as can see in regexr matches desired pattern (also matching "version" that's issue i'll tackle later): http://regexr.com/3e324
i'm invoking invoking sed following command:
cat package.json | sed 's/(\^|\~?)(\d|x|\*)+\.(\d|x|\*)+\.(\d|x|\*)+/hello/g' -r
for sake of brevity, outputs (ie. unfiltered input):
... "dependencies": { "babel-polyfill": "^6.7.4", "babel-preset-es2015": "^6.6.0", "babel-preset-react": "^6.5.0", "gulp-clean": "^0.3.2", ...
it should replace digits "hello".
doing wrong?
bad flags (i've tried /gm
)
or not using correct regex engine (i'm passing -r
option utilize extended regex)?
while posix regular expression support named character classes, [[:digit:]]
, [[:alnum:]]
, not support shorthand classes such \d
, \w
.
some gnu extensions bring shorthand classes support, restricted few of them, \w
, \w
, \s
, \s
according regular-expressions.info.
by replacing \d
in regular expression [0-9]
able transform document. regular expression becomes (\^|\~?)([0-9]|x|\*)+\.([0-9]|x|\*)+\.([0-9]|x|\*)+
, or better [~^]([0-9x*]+\.){2}[0-9x*]
(thanks ed morton !).
as side note, command rewritten following, not uses cat
:
sed -e 's/[~^]([0-9x*]+\.){2}[0-9x*]/hello/' package.json
and noted matt, you'd better off using json parser such jq
.
Comments
Post a Comment