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.
下面的代码将第一次出现的苹果替换为香蕉。我如何使用 awk / gawk 达到同样的效果?
sed -i "0,/apple/s//banana/" myfile.txt
这就是我想出的:
awk '!x{x=sub("apple","banana")}7' file
例如:
kent$ cat f foo apple foo apple apple kent$ awk '!x{x=sub("apple","banana")}7' f foo banana foo apple apple
对于sed -i(就地更改)部分,如果您使用 gawk 4.1.0,您也有该选项。否则,您必须使用临时文件。
sed -i