LoginSignup
4
7

More than 5 years have passed since last update.

bash > 行を取り出す > headとtailの組合せ > コメントに改良案あり

Last updated at Posted at 2015-02-04

動作環境

CentOS 6.5
bash version 4.1.2(1)

あるテキストファイルがあり、その中身を1行単位で処理したいとする。その場合、bashで行を取り出す方法としてはheadとtailの組合せがある。

#!/bin/env bash

file=out.dat
numline=$(wc $file | awk '{print $1}')
#echo $numline

for idx in $(seq 1 $numline)
do
   line=$(head -n $idx $file | tail -n 1)
   echo $idx $line
done

問題はheadとtailの組合せだと行が多い時のオーバヘッドが大きそうだということ。

perlやrubyなどを使わずに行単位の取り出し方法が別にあれば知りたい。

4
7
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7