如何从命令行中的多行 sed 命令转为脚本中的单行命令
如果我将其复制并粘贴到打开的 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\-\-\-//}'
问题是,当我尝试将其移动到 KornShell (ksh) 脚本中时,ksh 会抛出错误,因为我认为这是新的行字符。谁能帮我解决这个问题吗?仅供参考:正则表达式应该是多行替换。
谢谢你!
I have sed running with the following argument fine if I copy and paste this into an open shell:
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. Can anyone give me a hand with this? FYI: the regular expression is supposed to be a multiple line replacement.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此:
\{0,\}
可以替换为:*
此:
\{1,\}
可以替换为:\+
不需要转义连字符。
换行符可以用
-e
(或分号)替换cat
可以通过使用文件名作为sed
的参数来替换结果:
或
(未经测试)
This:
\{0,\}
can be replaced by this:*
This:
\{1,\}
can be replaced by this:\+
It's not necessary to escape hyphens.
The newline can be replaced by
-e
(or by a semicolon)The
cat
can be replaced by using the filename as an argument tosed
The result:
or
(untested)
您可以尝试将正则表达式放入文件中并使用选项 -f 调用 sed 吗?
can you try to put your regex in a file and call sed with the option -f ?
您可以尝试用“echo -e \\r”替换新行字符吗
Can you try to replace the new line character with `echo -e \\r`
与 C Shell 不同,Korn Shell 对于字符串中的换行符没有问题。因此,换行符不太可能是您的问题。同样的注释也适用于 Bourne 和 POSIX shell 以及 Bash。我已经复制了您的示例并在 Bash 和 Korn shell 下的 Linux 上运行它,没有任何问题。
如果您使用 C Shell 进行工作,您确定正在运行“
ksh ./script
”而不是“./script
”吗?否则,还有其他一些问题——也许某个地方的报价不平衡。
查看 Korn Shell 的“
-v
”和“-n
”选项以及“-x
”选项。这可能会告诉您更多关于问题出在哪里的信息。The Korn Shell - unlike the C Shell - has no problem with newlines in strings. The newline is very unlikely to be your problem, therefore. The same comments apply to Bourne and POSIX shells, and to Bash. I've copied your example and run it on Linux under both Bash and Korn shell without any problem.
If you use C Shell for your work, are you sure you're running '
ksh ./script
' and not './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. That may tell you more about where the problem is.