これでうまくいくかもしれません (GNU と非 GNU sed の両方):
sed -n 'p;n' file # keep odd
sed -n 'n;p' file # keep even
-n :印刷を抑制
p :現在の行を表示
n :次の行
GNU sed の使用:
sed -i '0~2d' filename
ファイルから偶数行を削除します。
奇数行の削除:
sed -i '1~2d' filename
-i オプションを指定すると、変更がファイルのインプレースに保存されます。
マニュアルからの引用:
`FIRST~STEP'
This GNU extension matches every STEPth line starting with line
FIRST. In particular, lines will be selected when there exists a
non-negative N such that the current line-number equals FIRST + (N
* STEP). Thus, to select the odd-numbered lines, one would use
`1~2'; to pick every third line starting with the second, `2~3'
would be used; to pick every fifth line starting with the tenth,
use `10~5'; and `50~0' is just an obscure way of saying `50'.
わかりました
% モジュラス演算子であり、NR は現在の行番号なので、NR%2==0 偶数行に対してのみ true であり、それらのデフォルト ルールを呼び出します ({ print $0 } )。したがって、偶数行のみを保存する 、 awk からの出力をリダイレクトします 新しいファイルへ:
awk 'NR%2==0' infile > outfile
シード
sed でも同じことができます . devnulls answer は GNU sed でそれを行う方法を示しています .以下は sed のバージョンの代替です ~ を持たない オペレーター:
奇数行を維持
sed 'n; d' infile > outfile
均等な線を保つ
sed '1d; n; d' infile > outfile