Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.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
Bash 是否有一种通过sed从字符串中删除美元符号的单行方法?_Bash_Sed - Fatal编程技术网

Bash 是否有一种通过sed从字符串中删除美元符号的单行方法?

Bash 是否有一种通过sed从字符串中删除美元符号的单行方法?,bash,sed,Bash,Sed,我有一个文件,正在逐行阅读。有些行中有美元符号,我想使用sed删除它们。那么比如说, echo $line 返回 {On the image of {$p$}-adic regulators}, 另一方面, echo $line | sed 's/\$//g' 正确返回 {On the image of {p}-adic regulators}, 但是 返回 {On the image of {$p$}-adic regulators}, 用这个怎么样。这会产生

我有一个文件,正在逐行阅读。有些行中有美元符号,我想使用sed删除它们。那么比如说,

echo $line
返回

{On the image of {$p$}-adic regulators},
另一方面,

          echo $line | sed 's/\$//g'
正确返回

 {On the image of {p}-adic regulators},
但是

返回

 {On the image of {$p$}-adic regulators},
用这个怎么样。这会产生相同的结果,并且应该更有效,因为它避免了只需调用子shell来运行
sed

[lsc@aphek]$ echo ${line//$/}
{On the image of {p}-adic regulators},

如果您希望坚持使用
sed
。。。 您的问题是由于反斜杠语法(
`…`
)处理反斜杠的方式造成的。要避免此问题,请改用
$()
语法

[me@home]$ title=$(echo $line | sed 's/\$//g'); echo $title
{On the image of {p}-adic regulators},
请注意,
$()
语法可能不受不符合POSIX的旧版本bash的支持。如果需要支持较旧的Shell,请坚持使用反斜杠,但不要使用反斜杠,如中所示


有关更多详细信息,请参阅:。

在反斜杠中使用sed命令时,需要将其转义为反斜杠:

title=`echo $line | sed 's/\\$//g'` # note two backslashes before $

由于
sed
解决方案已经发布,这里是一个
awk
变体

[jaypal:~/Temp] awk '{gsub(/\$/,"",$0);print}' <<< $line
{On the image of {p}-adic regulators},

[jaypal:~/Temp]awk'{gsub(/\$/,“”,$0);print}'这不是OP预期的结果。
[jaypal:~/Temp] awk '{gsub(/\$/,"",$0);print}' <<< $line
{On the image of {p}-adic regulators},
[jaypal:~/Temp] title=$(awk '{gsub(/\$/,"",$0);print}' <<< $line); echo $title
{On the image of {p}-adic regulators},