【问题标题】:Use sed with special character使用带有特殊字符的 sed
【发布时间】:2018-07-02 11:03:08
【问题描述】:

我尝试使用带有特殊字符的 sed:

sed -i -e "1i$jtempo" $file

$file 是我的文件。 $jtempo 是我的具有特殊字符的变量,例如:" 或 [ 或 (

但是,当我运行这个脚本时,我有这个错误:

sed: -e expression #1, char 17: unknown command: `"'

我的文件:

"desc_test":[
"id",
"name",
],

在我的脚本 bash 中:

jtempo=`cat myfile`

一个想法? 谢谢你!

【问题讨论】:

  • 我想用sed来表示行。 1是一个例子
  • 同理,别给我报错。但是我有一个错误,我不知道是什么问题
  • 如果我将 $jtempo 替换为 teststring,则该命令有效。 $jtempo 变量是:` jtempo=`cat myfile`
  • 然后将$jtempo 的内容添加到问题... 但在此之前,请检查a='*([]'; sed "1i$a" "$file".. 如果失败,请继续添加$jtempo 中的字符,直到失败。 . 这样你就会找到自己的解决方案:)
  • 如果您输入 sed --version,您会得到有效的回复吗?如果是,请将该信息添加到您的 Q 中。祝您好运。

标签: sed character cut


【解决方案1】:
$ cat f1
"desc_test":[
"id",
"name",
],
$ cat ip.txt
1
2
3

我建议避免使用i 命令并使用r 命令,无论文件内容如何,​​该命令都非常强大

$ # to insert before first line
$ cat f1 ip.txt
"desc_test":[
"id",
"name",
],
1
2
3

$ # to insert any other line number, use line_num-1
$ # for example, to insert before 2nd line, use 1r
$ # r command will read entire contents from a file
$ sed '1r f1' ip.txt
1
"desc_test":[
"id",
"name",
],
2
3

【讨论】:

  • 谢谢!但是如何直接在 ip.txt 文件中设置结果呢?
  • 在使用sed时使用-i...第一个使用cat f1 ip.txt > tmp && mv tmp ip.txt..
  • 非常感谢!
猜你喜欢
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
相关资源
最近更新 更多