使用 awk 追加到文件的特定行,保留不匹配的行不变
Append to certain line of file with awk, leaving non-matching lines unchanged
给定文件测试及其内容:
bcd://dfl
sf
我想将额外信息附加到具有特定内容的行(以bcd开头)
虽然以下脚本有效
awk '/bcd*/ {print [=11=]", extra information"} ' test > test.old && mv test.old test
它删除了不匹配的行。 (sf)
是否可以将它们保留在输出文件中?
正如评论中所讨论的,在末尾附加 {..}1
将解决您的问题,
awk '/^bcd/ {print [=10=]", extra information"; next} 1' file
因为 /<pattern>/{<action>}
仅应用于匹配 <pattern>
的 行 ,其他行按原样打印,{..}1
是 always-true-no-matter-what 打印行的条件。
awk '/^bcd/ {[=10=] = [=10=] ", extra information"} 1' test
给定文件测试及其内容:
bcd://dfl
sf
我想将额外信息附加到具有特定内容的行(以bcd开头) 虽然以下脚本有效
awk '/bcd*/ {print [=11=]", extra information"} ' test > test.old && mv test.old test
它删除了不匹配的行。 (sf)
是否可以将它们保留在输出文件中?
正如评论中所讨论的,在末尾附加 {..}1
将解决您的问题,
awk '/^bcd/ {print [=10=]", extra information"; next} 1' file
因为 /<pattern>/{<action>}
仅应用于匹配 <pattern>
的 行 ,其他行按原样打印,{..}1
是 always-true-no-matter-what 打印行的条件。
awk '/^bcd/ {[=10=] = [=10=] ", extra information"} 1' test