【发布时间】:2015-03-27 21:18:22
【问题描述】:
我有 2 个文件,file1 和 file2。
# cat /tmp/file1
***** insert new text ****
# cat /tmp/file2
</table>
some text
</table>
<table name="test" description="test line">
some text
我想将 file1 中的文本插入到 file2 中,但只能在以下 2 行之前:
</table>
<table name="test" description="test line">
所以最终结果是:
</table>
some text
*** insert new text ****
</table>
<table name="test" description="test line">
some text
这是我正在尝试的 awk 语句/命令,但问题是 awk 正在为每个匹配项插入新文本。
# f1="$(</tmp/file1)"
# awk -vf1="$f1" '/<\/table>/,/<table name="test" description="test line">/{print f1;print;next}1' /tmp/file2
***** insert new text ****
</table>
***** insert new text ****
some text
***** insert new text ****
</table>
***** insert new text ****
<table name="test" description="test line">
some text
如何修复 awk 语句以仅在特定的 2 行之前插入来自 file1 的文本?提前致谢。
【问题讨论】:
标签: awk