在bash / shell脚本中将所有的'/'替换为'\ /'

时间:2018-10-04 00:32:22

标签: bash shell

这可能非常简单,但我只能在shell / bash中完成此操作。 我输入了类似“ a/b/c/d的内容,我想用“ /”替换所有的“ \/”,输出将是“ a / b / c / d”

另一个示例是12.12.12.12/32 >>> 12.12.12.12\/32

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:5)

您可以使用bash的参数扩展:

$ input="a/b/c/d"
$ echo "${input//\//\\/}"
a\/b\/c\/d

给定变量input${input//xxx/yyy}产生input的内容,每个xxxyyy替换。这里的xxx\/(必须转义/才能不与下一个/混淆),而yyy\\/({{ 1}},其中\/被转义了。

答案 1 :(得分:0)

根据您访问要替换的文本或字符串的方式,类似的方法可能会起作用。您必须在sed命令中同时退出/\

var='a/b/c/d'
echo "$var" | sed 's/\//\\\//g'

答案 2 :(得分:0)

仅使用bash

{
  "name": "eventhawk",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "heroku-postbuild": "node_modules/.bin/webpack",
    "start": "node_modules/.bin/webpack && node app.js"
  },
  "dependencies": {
    "@types/axios": "^0.14.0",
    "@types/body-parser": "^1.16.5",
    "@types/d3": "^4.12.0",
    "@types/express": "^4.0.37",
    "@types/immutability-helper": "^2.0.15",
    "@types/jwt-decode": "^2.2.1",
    "@types/react": "^16.0.9",
    "@types/react-bootstrap": "^0.31.4",
    "@types/react-dom": "^16.0.0",
    "@types/react-redux": "^5.0.12",
    "@types/react-router": "^4.0.17",
    "@types/react-router-bootstrap": "^0.24.2",
    "@types/react-router-dom": "^4.0.8",
    "@types/redux-logger": "^3.0.5",
    "axios": "^0.16.2",
    "body-parser": "^1.18.2",
    "d3": "^4.12.0",
    "express": "^4.16.1",
    "immutability-helper": "^2.5.0",
    "import-loader": "^1.0.1",
    "jwt-decode": "^2.2.0",
    "moment": "^2.19.2",
    "react": "^16.0.0",
    "react-bootstrap": "^0.31.3",
    "react-datetime": "^2.11.0",
    "react-dom": "^16.0.0",
    "react-redux": "^5.0.6",
    "react-router": "^4.2.0",
    "react-router-bootstrap": "^0.24.4",
    "react-router-dom": "^4.2.2",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "source-map-loader": "^0.2.2"
  },
  "devDependencies": {
    "awesome-typescript-loader": "^3.5.0",
    "typescript": "^3.1.1",
    "webpack": "^3.12.0"
  }
}

答案 3 :(得分:0)

这可以做到:

echo "12.12.12.12/32" |sed 's,/,\\/,g'

输出:

12.12.12.12\/32

在这种情况下,请使用除/之外的定界符,如上例中我使用的,一样。您可以使用例如:#? .... etc