時系列データファイルから必要な時間のデータを取り出すスクリプト

| | Comments (1) | TrackBacks (0)

久しぶりにshell (bash) script。一つのデータが一行ごとに並んでいるファイルから必要な部分を取り出すスクリプト。今回はbashの組み込み関数である getopts を使ってみた。

#!/bin/bash
#
# clip.sh -f FROM -t TO FILE
#

while getopts "f:t:" i;do
	case $i in
		f) FROM=$OPTARG;; # Time From
		t) TO=$OPTARG;;   # Time To
	esac
done

shift $(($OPTIND - 1))
FILE=$1

LINES=`wc --line $FILE | cut -d " " -f 1`
FLINE=`grep -n $FROM $FILE | cut -d : -f 1 | head -n 1`
TLINE=`grep -n $TO $FILE | cut -d : -f 1 | head -n 1`

if [ $FLINE -ge $TLINE ]; then
	TMP=$FLINE;
	FLINE=$TLINE;
	TLINE=$TMP;	
fi

HEAD=$(($TLINE-$FLINE))
TAIL=$(($LINES-$FLINE+1))

tail -n $TAIL $FILE | head -n $HEAD

 たとえばこのようなファイルがあるとすると

11:00:17 24.93 72.9
11:00:17 24.94 72.8
11:00:17 24.94 72.7
11:00:18 24.95 72.4
11:00:19 24.96 72.1
11:00:20 24.97 71.9
11:00:21 24.97 71.8
11:00:22 24.98 71.7
11:00:23 24.99 71.4
11:00:24 25.00 71.2
....

このファイルをdata.txtというファイル名として、この中から12:00:00から13:00:00まで取り出したいとすると、

$ clip.sh -f 12:00:00 -t 13:00:00 data.txt

とすれば標準出力に出力される。

 ふ、例によって誰もついて来れまい。あ、ぴらふとCZDの二氏は除く。ってかこのくらいのコマンドは、textutilあたりにありそう。良い解法があればコメントよろ。

Categories

0 TrackBacks

Listed below are links to blogs that reference this entry: 時系列データファイルから必要な時間のデータを取り出すスクリプト.

TrackBack URL for this entry: http://www.argv.org/~chome/blog/mt-tb.cgi/87

1 Comments

Bodyc said:

Ugh, I liked! So clear and positively.
Bodyc

About Me

中尾 圭佐(chomy)
千葉県船橋市在住のモノクロ写真に目覚めた研究者。
Twitter ID: jm6xxu
Skype: chomy

My Tweets

jm6xxu: よし。仕事終了。これから早咲きの桜を撮影してくる。

jm6xxu: もしかして、東京都はアキバをふつーのビジネス街にしたいから、非実在青少年なんたら条例を改正したいのかなぁ

jm6xxu: [ぐっときた]その人物が悶々と妄想するのは自由であり、それは決して犯してはならない権利である。RT @ShinyaMatsuura 「緊急!東京都が児童ポルノ規制の美名の下、思想統制への道を開こうとしている」:松浦晋也「人と技術と情報の界 http://bit.ly/dxJ82j

jm6xxu: 論破というのは論を重んじる相手には鬼に金棒だが、そうでない相手には糠に釘。404 Blog Not Foundより http://bit.ly/bcZyfZ

jm6xxu: The Big Picture による、バンクーバーパラリンピックの写真。オリンピックよりも面白そうと思うのは私だけ?こっちも放送してくれないものか。 http://bit.ly/b9b3Xa

About this Entry

This page contains a single entry by chomy published on October 11, 2009 2:05 AM.

Linux BoxにTime Machineを使ってバックアップする方法 [UPDATE] was the previous entry in this blog.

JavaScriptでのクロージャーのテスト is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.261