如何使用 awk/sed 实现这一目标?
输入:
zero
one
two
three
four
输出:
zero
one
one-one
one-two
one-three
two
three
four
注意:我需要将实际选项卡包含在要添加的新行中。
使用 awk:
awk '1; /one/{print "\n\tone-one\n\tone-two\n\tone-three"}' file
zero
one
one-one
one-two
one-three
two
three
four
The title states 'after the first occurrence' (I presume occurene is a typo), however, other answers don't seem to cater this requirement and due to the unique nature of the sample set, it is not that obvious when you test.
If we change the sample set to
zero
one
three
one
four
five
one
six
seven
one
Then we would need something like awk '/one/ && !x {print $0; print "\tone-one\n\tone-two\n\tone-three"; x=1;next} 1'
, which produces
zero
one
one-one
one-two
one-three
two
three
one
four
five
one
six
seven
one
Actually, this and that answers provide some more options as well.