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.
我想用文件中的换行符@@替换^和替换。¤¤为此,我编写了下面的代码,但感觉有一个更优雅的解决方案,然后调用 gawk 两次。谁能告诉我有没有?
@@
^
¤¤
cat test.txt | gawk '{ gsub("@@", "^"); print }' | gawk '{ gsub("¤¤", "\r\n"); print }'
首先,剥去cat. 除了文件连接之外,它没有用,这是它的目的。你的awk命令是
cat
awk
awk '{gsub("@@","^");gsub("¤¤","\r\n");print}' file
如果您想在执行上述操作之前删除所有换行符
tr -d '\r\n' <file > temp && mv temp file
只需在打印前调用 gsub() 两次。
gawk '{ gsub("@@", "^"); gsub("¤¤", "\r\n"); 打印 }'