Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用 perl one liner 仅替换文件中第一次出现的模式。
>touch tmp >perl -p -i.bak -e '++$seen if( !$seen && s/alpha/beta/);' tmp
在此之后,我按 Enter。屏幕上出现以下消息。
perl -p -i.bak -e '++$seen if( tmpseen && s/alpha/beta/);' tmp
请注意,已!$seen替换为tmpseen.
!$seen
tmpseen
为什么会这样?
谢谢。
正如 kjprice 提到的 !$ 被外壳扩展。一种可能的解决方案是使用运算符“not”而不是运算符 !。
perl -p -i.bak -e '++$seen if( (not $seen) && s/alpha/beta/);' tmp
括号存在是因为运算符“not”的优先级较低
您可以使用以下命令转义任何 csh 特殊字符\:
\
perl -p -i.bak -e '++$seen if( \!$seen && s/alpha/beta/);' tmp