sed命令给出错误:sed: -e表达式#1,char 50:未终止的“s”命令。

时间:2021-03-21 15:30:44

I used grep to find out whether a particular line exists in a file or not like this:

我使用grep来查找文件中是否存在某个特定的行:

my $grepval=`grep "$conn[$i]" filename`; 

which is correct but after this I now want to delete the grepval from the file using sed like this:

这是正确的,但在此之后,我现在想要从文件中删除grepval,使用如下方法:

sed -i "s/$grepval//g" filename`; 

which gives me error sed: -e expression #1, char 50: unterminateds' command`. Please help on how to resolve it.thanks

它给了我错误sed: -e表达式#1,char 50: unterminateds'命令'。请帮忙解决,谢谢。

there's probably a / in $grepval also it can be multilines – RC. 39 mins ago

也有可能是a / in $grepval也可以是multilines - RC。39分钟前

SA291 ERROR CODE---->>> -3 ERROR_NO_RESULT_FOUND .this is my $grepval.I used a different delimiter also like this :sed -i "s|$grepval||g" /home/abc/Desktop/xyz.txt.but its still giving me the same error.

SA291错误代码——>>> -3 ERROR_NO_RESULT_FOUND,这是我的$grepval。我使用了一个不同的分隔符,也像这样:sed -i ' s|$grepval| g" /home/ abc/desktop/xyz.txt。但它还是给了我同样的错误。

5 个解决方案

#1


0  

U'll have to use sed -i instead of sed -e to make the changes in the file .. -e is used for expressions whereas -i is required when u need to modify the files and save 'em after the modifications.. also chomp would also work to resolve this issue.. The grepval var might ave a newline character at the end.

你将不得不使用sed -i而不是sed -e来对文件进行更改。-e用于表达式,而当需要修改文件并在修改后保存时,我是需要的。同时chomp也将致力于解决这个问题。grepval var在最后可能会有一个换行符。

Sammy

萨米

#2


1  

Put this in your code just before the sed invocation:

在sed调用之前将其放入代码:

print "[$grepval]\n";

and I guarantee there'll be a / in there somewhere, probably around character number 48.

我保证会有一个/在某个地方,大概在第48个字符周围。

One way to solve it is to preprocess $grepval so that you turn it into a properly formatted search string for sed or, if possible, choose a delimiter that you know won't appear in the string (sed allows different delimiters):

解决它的一种方法是预处理$grepval,以便将其转换为sed的适当格式的搜索字符串,或者,如果可能的话,选择一个您知道不会出现在字符串中的分隔符(sed允许不同的分隔符):

sed -i "s?$grepval??g" filename

#3


1  

If the replacement/pattern contain /, then you can't use / as the delimiter.

如果替换/模式包含/,那么就不能使用/作为分隔符。

Use a different delimiter:

使用不同的分隔符:

sed -i "s|$grepval||g" filename

#4


0  

you could get a list of file names that contain the string in question

您可以得到一个包含有问题字符串的文件名列表。

my @files = qx#grep -l $search_string $filenameExpression#;

foreach my $f ( @files )
{
  chomp($f);
    # make sure your input doesn't have slash "/" then use hash "#"
  my $ret = `perl -pi -e's#$search_string##g' $f`;  
}

#5


0  

There's a newline in $grepval (or another "hidden" character). Get rid of it with chomp.

$grepval(或另一个“隐藏”字符)中有一条换行符。用chomp处理掉它。

Also you might consider using the list form of system() to avoid problems with shell metacharacters (just putting arguments into "" does not help always) and try to escape possible regexp metacharacters with quotemeta() to get the script more robust. Even better to do the whole thing completely in Perl. For example, sed -i is not portable and works probably only on Linux systems.

您还可以考虑使用system()的列表形式来避免shell元字符的问题(只是将参数放入“”并不总是有效),并尝试使用quotemeta()来避免可能的regexp元字符,从而使脚本更加健壮。更好的做法是完全用Perl完成整个事情。例如,sed -i不能移植,而且可能只在Linux系统上工作。

#1


0  

U'll have to use sed -i instead of sed -e to make the changes in the file .. -e is used for expressions whereas -i is required when u need to modify the files and save 'em after the modifications.. also chomp would also work to resolve this issue.. The grepval var might ave a newline character at the end.

你将不得不使用sed -i而不是sed -e来对文件进行更改。-e用于表达式,而当需要修改文件并在修改后保存时,我是需要的。同时chomp也将致力于解决这个问题。grepval var在最后可能会有一个换行符。

Sammy

萨米

#2


1  

Put this in your code just before the sed invocation:

在sed调用之前将其放入代码:

print "[$grepval]\n";

and I guarantee there'll be a / in there somewhere, probably around character number 48.

我保证会有一个/在某个地方,大概在第48个字符周围。

One way to solve it is to preprocess $grepval so that you turn it into a properly formatted search string for sed or, if possible, choose a delimiter that you know won't appear in the string (sed allows different delimiters):

解决它的一种方法是预处理$grepval,以便将其转换为sed的适当格式的搜索字符串,或者,如果可能的话,选择一个您知道不会出现在字符串中的分隔符(sed允许不同的分隔符):

sed -i "s?$grepval??g" filename

#3


1  

If the replacement/pattern contain /, then you can't use / as the delimiter.

如果替换/模式包含/,那么就不能使用/作为分隔符。

Use a different delimiter:

使用不同的分隔符:

sed -i "s|$grepval||g" filename

#4


0  

you could get a list of file names that contain the string in question

您可以得到一个包含有问题字符串的文件名列表。

my @files = qx#grep -l $search_string $filenameExpression#;

foreach my $f ( @files )
{
  chomp($f);
    # make sure your input doesn't have slash "/" then use hash "#"
  my $ret = `perl -pi -e's#$search_string##g' $f`;  
}

#5


0  

There's a newline in $grepval (or another "hidden" character). Get rid of it with chomp.

$grepval(或另一个“隐藏”字符)中有一条换行符。用chomp处理掉它。

Also you might consider using the list form of system() to avoid problems with shell metacharacters (just putting arguments into "" does not help always) and try to escape possible regexp metacharacters with quotemeta() to get the script more robust. Even better to do the whole thing completely in Perl. For example, sed -i is not portable and works probably only on Linux systems.

您还可以考虑使用system()的列表形式来避免shell元字符的问题(只是将参数放入“”并不总是有效),并尝试使用quotemeta()来避免可能的regexp元字符,从而使脚本更加健壮。更好的做法是完全用Perl完成整个事情。例如,sed -i不能移植,而且可能只在Linux系统上工作。