简体   繁体   English

如何从命令行中的多行sed命令转到脚本中的单行

[英]How to go from a multiple line sed command in command line to single line in script

I have sed running with the following argument fine if I copy and paste this into an open shell: 如果将其复制并粘贴到打开的外壳中,则可以使用以下参数运行sed:

cat test.txt | sed '/[,0-9]\{0,\}[0-9]\{1,\}[acd][0-9]\{1,\}[,0-9]\{0,\}/{N
s/[,0-9]\{0,\}[0-9]\{1,\}[acd][0-9]\{1,\}[,0-9]\{0,\}\n\-\-\-//}'

The problem is that when I try to move this into a KornShell (ksh) script, the ksh throws errors because of what I think is that new line character. 问题是,当我尝试将其移入KornShell(ksh)脚本时,由于我认为这是换行符,因此ksh会引发错误。 Can anyone give me a hand with this? 有人可以帮我吗? FYI: the regular expression is supposed to be a multiple line replacement. 仅供参考:正则表达式应该是多行替换。

Thank you! 谢谢!

This: \\{0,\\} can be replaced by this: * 此: \\{0,\\}可以替换为: *

This: \\{1,\\} can be replaced by this: \\+ 此: \\{1,\\}可以替换为: \\+

It's not necessary to escape hyphens. 不必转义连字符。

The newline can be replaced by -e (or by a semicolon) 换行符可以用-e (或分号)代替

The cat can be replaced by using the filename as an argument to sed 可以通过使用文件名作为sed的参数来替换cat

The result: 结果:

sed -e '/[,0-9]*[0-9]\+[acd][0-9]\+[,0-9]*/{N' -e 's/[,0-9]*[0-9]\+[acd][0-9]\+[,0-9]*\n---//}' test.txt

or 要么

sed '/[,0-9]*[0-9]\+[acd][0-9]\+[,0-9]*/{N;s/[,0-9]*[0-9]\+[acd][0-9]\+[,0-9]*\n---//}' test.txt

(untested) (未试)

您可以尝试将正则表达式放入文件中并使用-f选项调用sed吗?

cat test.txt | sed -f file.sed

您可以尝试用`echo -e \\\\ r`替换换行符吗?

The Korn Shell - unlike the C Shell - has no problem with newlines in strings. 与C Shell不同,Korn Shell对字符串中的换行符没有任何问题。 The newline is very unlikely to be your problem, therefore. 因此,换行符不太可能是您的问题。 The same comments apply to Bourne and POSIX shells, and to Bash. 相同的注释适用于Bourne和POSIX外壳以及Bash。 I've copied your example and run it on Linux under both Bash and Korn shell without any problem. 我已经复制了您的示例,并在Linux上的Bash和Korn Shell下运行了它,没有任何问题。

If you use C Shell for your work, are you sure you're running ' ksh ./script ' and not ' ./script '? 如果您使用C Shell进行工作,是否确定运行的是' ksh ./script '而不是' ./script '?

Otherwise, there is some other problem - an unbalanced quote somewhere, perhaps. 否则,还有其他问题-也许某处的报价不平衡。

Check out the ' -v ' and ' -n ' options as well as the ' -x ' option to the Korn Shell. 在Korn Shell程序中检出-v-n选项以及-x选项。 That may tell you more about where the problem is. 这可能会告诉您更多有关问题所在的信息。

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

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