ひゃまだのblog

ひゃまだ(id:hymd3a)の趣味のブログ

Sedで途中の行を抽出する

(2023-06-11 初稿 - 2023-06-12 修正)

sedを用いて、途中の行を抽出する。自分用のメモ。

$ cat test.txt
1: a
2: b
3: c
4: d
5: e

上記のようなファイル(test.txt)がある場合、3〜4行を抽出する場合、sedでは以下のように記述する。

$ cat test.txtt | sed -n '3, 4p'

$ cat test.txt | sed -n '3, 4p'
3: c
4: d

ちなみに、tailコマンドと同様に、3から最終行まで抽出する場合は、以下のとおり。

($pを入力するときは、シングルクォートで囲ってね。)

$ cat test.txt | sed -n '3, $p'

$ cat test.txt | sed -n '3, $p'
3: c
4: d
5: e

head、tailコマンドとあわせて使えば、すべての行が抽出できるね。