简体   繁体   English

在 perl 中的 bash 中转义

[英]Escaping in bash within perl

I'm running something in perl and have the following command, which deletes consecutive duplicate lines (only keeping one of them)我在 perl 中运行一些东西并有以下命令,它删除连续的重复行(只保留其中之一)

system("sed -i '$!N; /^\\(.*\\)\\n\\1$/!p; d' *[13579].csv");

However, when I run this, I get the following error:但是,当我运行它时,出现以下错误:

sed: -e expression #1, char 11: unterminated address regex

I have a feeling it has to do with my escaping, but I'm not too certain as I am rather inexperienced with perl and bash.我有一种感觉,这与我的逃避有关,但我不太确定,因为我对 perl 和 bash 缺乏经验。 I know the dollar signs should be escaped, but what about the backslashes?我知道美元符号应该被转义,但是反斜杠呢? Does anyone have a good resource they can point me to to learn more about escaping bash within perl?有没有人有很好的资源可以指点我以了解有关在 perl 中转义 bash 的更多信息? Thanks!谢谢!

When putting sed in Perl it can be fussy, there's a couple things you could do.将 sed 放入 Perl 时可能会很麻烦,您可以做几件事。 The first would be to change the type of quotes you wrap around the command system is running and the sed pattern (flip outer to single, inner to double);第一个是更改您在命令系统运行时环绕的引号类型和 sed 模式(将外部翻转为单,内部翻转为双); the other option would be to escape the \\ characters in sed.另一种选择是转义 sed 中的\\字符。

system('sed -i "$!N;/^\(.*\)\n\1$/!p;d" *filename');

Note: since your filename uses a special characters there might be escaping needed for that to work with globbing (eg. *\\\\[13579].csv );注意:由于您的文件名使用特殊字符,因此可能需要转义才能使用通配符(例如*\\\\[13579].csv ); escaping would be something like this:转义将是这样的:

system("sed -i '$!N;/^\\(.*\\)\\n\\1\$/!p;d' *\\[13579].csv");

If your file name happens to include spaces then those would need escaping as well.如果您的文件名碰巧包含空格,那么这些也需要转义。

system("sed -i '$!N;/^\\(.*\\)\\n\\1\$/!p;d' *\\[12345]\\ \\[13579].csv");

sed would then find any files matching *[12345] [13579].csv and in-place edit them. sed然后会找到任何匹配*[12345] [13579].csv并就地编辑它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM