Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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_Shell_Sed - Fatal编程技术网

Bash 在匹配模式的行上使用sed中的变量

Bash 在匹配模式的行上使用sed中的变量,bash,shell,sed,Bash,Shell,Sed,我正在尝试执行以下命令,但是sed不喜欢它。我确信修复很简单,但我还没有找到任何解决方案 在不以http开头的行中,我试图将$currentlink的内容放在行的开头。Sed抛出了一个错误。任何帮助都将不胜感激——谢谢 请试试这个: sed'/^http/!"(echo${currentlink}sed's/@/\\\@/g)/" 请试试这个: sed'/^http/!"(echo${currentlink}sed's/@/\\\@/g)/" 单引号不插入shell变量。有几种选择,其中之一是使

我正在尝试执行以下命令,但是sed不喜欢它。我确信修复很简单,但我还没有找到任何解决方案

在不以http开头的行中,我试图将$currentlink的内容放在行的开头。Sed抛出了一个错误。任何帮助都将不胜感激——谢谢

请试试这个:

sed'/^http/!"(echo${currentlink}sed's/@/\\\@/g)/"

请试试这个:

sed'/^http/!"(echo${currentlink}sed's/@/\\\@/g)/"


单引号不插入shell变量。有几种选择,其中之一是使用双引号

sed -e "/^http/!s/^/$currentlink/"

单引号不插入shell变量。有几种选择,其中之一是使用双引号

sed -e "/^http/!s/^/$currentlink/"

我没有弄错$currentlink是shell脚本中的变量吗?是的,这是一个bash shell变量我没有弄错$currentlink是shell脚本中的变量吗?是的,这是一个bash shell变量谢谢。我尝试了您的解决方案,现在收到:sed:-e表达式#1,char 19:s的未知选项谢谢。我尝试了您的解决方案,现在收到:sed:-e expression#1,char 19:s的未知选项如果$currentlink包含“#”,此解决方案将被中断。记住这一点我认为这样引用可能会更好:
sed'/^http/!s@^@'${currentlink}/'@g'
是,但链接的查询部分中可能有
@
。最正确的方法是:
sed'/^http/!s@^@'$(echo${currentlink}sed's/@/\\\@/g')/“@”
。我更新了答案。示例中
s
命令的
g
选项与替换行开头的匹配项无关。但在我的上一个版本的“内部”sed调用中,它很重要,因为它必须替换链接中的所有符号。如果$currentlink包含“#”,则此解决方案将被破坏。记住这一点我认为这样引用可能会更好:
sed'/^http/!s@^@'${currentlink}/'@g'
是,但链接的查询部分中可能有
@
。最正确的方法是:
sed'/^http/!s@^@'$(echo${currentlink}sed's/@/\\\@/g')/“@”
。我更新了答案。示例中
s
命令的
g
选项与替换行开头的匹配项无关。但在我上一个版本的“内部”sed调用中,它很重要,因为它必须替换链接中的所有符号