echo 全局替换的解决方案

发布于 2024-12-22 04:45:46 字数 490 浏览 1 评论 0原文

如何将所有 ksh 脚本中的 echo 替换为 echo -e。 示例:

输入

脚本包含以下几行,

echo "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"

输出:

我不应干扰 set echo off 命令,而仅更改打印 echo 命令。如果已经更改为echo -e,我不应该再次更换。我应该一次性替换根目录中的所有脚本。

echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"

How do I replace echo in all my ksh scripts to echo -e.
Example:

Input

Script containing following lines,

echo "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"

OUTPUT:

I should not disturb set echo off command and only change the printing echo commands. If already it is changed to echo -e, I should not replace again. I should replace it in a single shot for all the scripts from root directory.

echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屌丝范 2024-12-29 04:45:46

这应该有效 -

sed 's/^echo "/echo -e "/' filename

测试:

[jaypal:~/Temp] cat tmp
echo "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
[jaypal:~/Temp] sed 's/^echo "/echo -e "/' tmp
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"

findexec

find /path/to/directory/ -type f -name "*.ksh" -exec sed -i"bak" 's/^echo "/echo -e "/' {} \;

This should work -

sed 's/^echo "/echo -e "/' filename

Test:

[jaypal:~/Temp] cat tmp
echo "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
[jaypal:~/Temp] sed 's/^echo "/echo -e "/' tmp
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"

find and exec:

find /path/to/directory/ -type f -name "*.ksh" -exec sed -i"bak" 's/^echo "/echo -e "/' {} \;
友谊不毕业 2024-12-29 04:45:46
sed '/echo [^-]/s/echo/echo -e/'

查找 echo 后跟一个空格和破折号之外的其他内容;将 echo 替换为 echo -e。将其循环应用到您要更改的文件。

for file in *.sh
do
    sed '/echo [^-]/s/echo/echo -e/' $file > tmp.$
    mv tmp.$ $file
done
sed '/echo [^-]/s/echo/echo -e/'

Look for echo followed by a space and something other than a dash; replace the echo with echo -e. Apply it in a loop to the files you want to change.

for file in *.sh
do
    sed '/echo [^-]/s/echo/echo -e/' $file > tmp.$
    mv tmp.$ $file
done
爱情眠于流年 2024-12-29 04:45:46

更强大的正则表达式将在任何上下文中查找 echo 后面不跟有 -eoff
由于 sed 无法在正则表达式中进行环视,因此您必须使用 perl:

perl -pe 's/echo +(?!((-[A-Za-df-z]|--[a-z-]+) +)*(-e|off))/echo -e/'

这也可以处理您可能遇到的情况(例如)echo -n - e 其中一个-e,但不是直接在echo之后:你不想以<代码>echo -e -n -e。另外,如果您执行了(例如)echo --version,您就不会需要 echo -e --version。上面的正则表达式可以处理这些问题。

输入

echo "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
echo -n -e "what happens if the -e is already there?"
echo --help # don't want echo -e --help

输出

echo -e"\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
echo -n -e "what happens if the -e is already there?"
echo -e--help # don't want echo -e --help

A more robust regex would be look for echo not followed by -e or off in any context:
Since sed can't do lookarounds in regex you'd have to use perl:

perl -pe 's/echo +(?!((-[A-Za-df-z]|--[a-z-]+) +)*(-e|off))/echo -e/'

This also handles the case that you might have (e.g.) echo -n -e where there is a -e, but not directly after the echo: you don't want to end up with echo -e -n -e. Also, if you did (say) echo --version, you wouldn't want a echo -e --version. The above regex takes care of these.

INPUT

echo "\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
echo -n -e "what happens if the -e is already there?"
echo --help # don't want echo -e --help

OUTPUT

echo -e"\nE-MAILING TO INTERESTED PARTIES....\n"
set echo off
echo -e "\nE-MAILING TO INTERESTED PARTIES....\n"
echo -n -e "what happens if the -e is already there?"
echo -e--help # don't want echo -e --help
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文