Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 使用sed或awk或新安装的Debian9工具,如何在未经修剪的情况下添加文本?_Linux_String_Bash_Awk_Sed - Fatal编程技术网

Linux 使用sed或awk或新安装的Debian9工具,如何在未经修剪的情况下添加文本?

Linux 使用sed或awk或新安装的Debian9工具,如何在未经修剪的情况下添加文本?,linux,string,bash,awk,sed,Linux,String,Bash,Awk,Sed,如何使用sed或awk或fresh debian 9安装中的工具,执行以下文本附加操作: sed '/"start":/a "start:prod": "hello",' package.json 当前我的字符串起始空格已被修剪 这就是我们所拥有的: "scripts": { "start": "export REACT_APP_ENV=${REACT_APP_ENV:-default} && cp src/env/.env.$REACT_APP_ENV .

如何使用sed或awk或fresh debian 9安装中的工具,执行以下文本附加操作:

sed '/"start":/a     "start:prod": "hello",' package.json
当前我的字符串起始空格已被修剪

这就是我们所拥有的:

  "scripts": {
    "start": "export REACT_APP_ENV=${REACT_APP_ENV:-default} && cp src/env/.env.$REACT_APP_ENV .env && react-scripts start",
“开始:prod”:“你好”, ... },

这就是我们所期望的:

  "scripts": {
    "start": "export REACT_APP_ENV=${REACT_APP_ENV:-default} && cp src/env/.env.$REACT_APP_ENV .env && react-scripts start",
    "start:prod": "hello",
    ...
  },

只需在
a
命令之后添加
\
(假设
gnused
来自
linux
标记)

忽略a命令后的前导空格

文本中的转义序列是经过处理的,因此您应该在文本中使用
\\
来打印单个反斜杠


另一种方法是使用
r
命令

$ seq 3 > ip.txt
$ # need to take care of escape sequences, unless that is a feature you need
$ sed '/2/a\ a\\ny' ip.txt
1
2
 a\ny
3

$ # r command will add the text as is
$ # won't cause trouble for multiline text, clashes with sed meta characters, etc
$ echo ' a\ny' | sed '/2/r /dev/stdin' ip.txt
1
2
 a\ny
3
$ # or from file input
$ echo ' a\ny' > f1
$ sed '/2/r f1' ip.txt
1
2
 a\ny
3
或者,使用“替换”命令捕获行开头的空格,并重新用于追加

$ echo '  hello123' | sed -E 's/^([[:blank:]]+)hello.*/&\n\1hi/'
  hello123
  hi

您可以这样做:-

sed -e "/\"start\":/a\\
      \"start:prod\": \"hello\"," <  package.json

sed代表s/old/new/,仅此而已。只需使用awk:

awk '{print} sub(/"start":.*/,""){print $0 "\"start:prod\": \"hello\","}' package.json

请注意,以上内容只需使用与前一行相同的缩进打印新行,使用输入中存在的空格和/或制表符的任何组合,您无需硬编码希望与原始行匹配的前导空格。

显示package.json和预期输出
"scripts": {
"start": "export REACT_APP_ENV=${REACT_APP_ENV:-default} && cp src/env/.env.$REACT_APP_ENV .env && react-scripts start",
"start:prod": "hello",
awk '{print} sub(/"start":.*/,""){print $0 "\"start:prod\": \"hello\","}' package.json