如何使用文本处理工具在每N行之后插入新行?
N=2示例:
投入:
sadf
asdf
yxcv
cxv
eqrt
asdf产出:
sadf
asdf
yxcv
cxv
eqrt
asdf发布于 2018-10-09 20:06:19
使用(GNU) sed:
sed '0~2G'简称(丑陋的N=100):
sed 'n;G'文思解释道:
第一步匹配第一步开始的每一步线。例如,sed -n 1~2p'‘将在输入流中打印所有奇数行,地址2~5将匹配第五行,从第二行开始。首先可以是零;在这种情况下,sed的操作就好像它等于步骤一样。(这是一个分机。)
与其他sed (计算新行数):
sed -e 'p;s/.*//;H;x;/\n\{2\}/{g;p};x;d'或者,为了更便于移植,编写为(删除某些版本的sed的注释):
sed -e ' # Start a sed script.
p # Whatever happens later, print the line.
s/.*// # Clean the pattern space.
H # Add **one** newline to hold space.
x # Get the hold space to examine it, now is empty.
/\n\{2\}/{ # Test if there are 2 new lines counted.
g # Erase the newline count.
p # Print an additional new line.
} # End the test.
x # match the `x` done above.
d # don't print anything else. Re-start.
' # End sed script.对于awk,可能:
awk '1 ; NR%2==0 {printf"\n"} '发布于 2015-01-27 17:50:25
使用paste
paste -d'\n' - - /dev/null <file发布于 2015-01-27 10:18:33
sed n\;G <infile..。你只需要..。
例如:
seq 6 | sed n\;G1
2
3
4
5
6.(在6后面还有一个空白)...or.
seq 5 | sed n\;G1
2
3
4
5(第5条后面没有空白)
如果在最后一行情况下总是应该省略空白:
sed 'n;$!G' <infilehttps://unix.stackexchange.com/questions/23545
复制相似问题