sed(GNU)

sed(GNU)

テキスト処理ツールを使用してN行ごとに新しい行を挿入するにはどうすればよいですか?

N=2 の例:

入力する:

sadf
asdf
yxcv
cxv
eqrt
asdf

出力:

sadf
asdf

yxcv
cxv

eqrt
asdf

ベストアンサー1

そしてawk

awk ' {print;} NR % 2 == 0 { print ""; }' inputfile

そしてsedGNU拡張):

sed '0~2 a\\' inputfile

そしてbash

#!/bin/bash
lines=0
while IFS= read -r line
do
    printf '%s\n' "${line}"
    ((lines++ % 2)) && echo
done < "$1"

おすすめ記事