sed - 使用带特殊字符的 sed

标签 sed character cut

我尝试使用带有特殊字符的 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`

一个想法? 谢谢你!

最佳答案

$ 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

关于sed - 使用带特殊字符的 sed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48404898/

相关文章:

regex - 使用 bash 查找文件时提取部分文件名

Linux SED 命令等于字符串不包含

linux - 如何替换字符串(字符和数字)

c - 粘贴字符作为标记

awk - 根据bash中的两列计算唯一值的数量

linux - awk 要求合并两个文件

c - 如何从utf8字符串中获取字符

java - 如何在 switch-case 中使用 char 作为 case?

linux - 变量不通过 ssh 存储值

linux - Bash:我的输出在一行