用Bash代替垂直线 [英] Substitute vertical lines in Bash
问题描述
我很难完成我的脚本,因为其中的一部分无法按我希望的方式发挥作用.
I'm having a hard time finishing my script since there's this part which doesn't function the way I wanted it to be.
我的脚本中有这一行:
cat /home/tmp/temp1.txt | awk '{gsub("~",RS);gsub("*",RS);print}' > /home/tmp/temp.txt
工作正常,是的.但是当我做这样的事情时:
It works fine, yes. But when I do something like this:
cat /home/tmp/temp1.txt | awk '{gsub("|",RS);print}' > /home/tmp/temp.txt
这根本不起作用.我想将所有竖线更改为新行,但我无法实现.请帮我解决一下这个.谢谢
It's not working at all. I wanted to change all my vertical bars into new line and yet I can't achieve it. Please help me with this. Thanks
推荐答案
您可以像这样在一个awk中进行所有替换:
You can do all the replacements in a single awk like this:
awk '{gsub(/[*~|]/, RS)} 1' /home/tmp/temp1.txt
否则,Pipe用于需要转义的正则表达式替换.但是,在字符类 [...]
内部,不需要如上所示对管道或星号进行转义.
Pipe is otherwise used for regex alternation that needs escaping. However inside the character class [...]
pipe or asterisk need not be escaped as shown above.
最好在 gsub
函数中使用/.../
正则表达式文字而不是带引号的字符串.
It is also better to use /.../
regex literal in gsub
function instead of quoted string.
这篇关于用Bash代替垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!