1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bash + GNU coreutils (date) > 「27日範囲の日付区間」を「開始日が13日おき」に表示する | `--max-args`と`--replace=`の練習もかねて

Last updated at Posted at 2018-01-17
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.9を使用。
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
date (GNU coreutils) 8.4 

処理概要

  • 27日範囲の日付区間を表示する
    • 例: 2017/11/04から2017/12/01
  • 上記を開始日が13日おきに表示する
    • 例: 2017/11/04から...
    • 例: 2017/11/17から...
    • ...

参考

昨日〜9日前までの日付を空白区切りで返すサンプル
@nakataSyunsuke さんと@tukiyo3 さんのコメントを参考にしました。

(--max-args--replace=の練習もかねて)

code v0.1

show_range_180117_exec
#!/usr/bin/env bash

set -eu  # for safety

STARTS=$(seq 0 13 50 | xargs --max-args=1 | xargs --replace=@ -- date -d "2017/11/4 @days" +"%Y/%m/%d")
ENDS=$(echo $STARTS | xargs --max-args=1 | xargs --replace=@ -- date -d "@ 27days" +"%Y/%m/%d")

echo "--START--"
echo $STARTS | xargs --max-args=1
echo "--END--"
echo $ENDS | xargs --max-args=1
run
$ bash show_range_180117_exec 
--START--
2017/11/04
2017/11/17
2017/11/30
2017/12/13
--END--
2017/12/01
2017/12/14
2017/12/27
2018/01/09

備考

  • もっと短い書き方は未勉強
  • STARTとENDを横に並べるエレガントな書き方は知らない

code v0.2

show_range_180117_exec
#!/usr/bin/env bash

set -eu  # for safety

STARTS=$(seq 0 13 50 | xargs --max-args=1 | xargs --replace=@ -- date -d "2017/11/4 @days" +"%Y/%m/%d")
ENDS=$(echo $STARTS | xargs --max-args=1 | xargs --replace=@ -- date -d "@ 27days" +"%Y/%m/%d")

#echo "--START--"
#echo $STARTS | xargs --max-args=1
#echo "--END--"
#echo $ENDS | xargs --max-args=1

echo $STARTS ": START"
echo $ENDS ": END"
run
$ bash show_range_180117_exec 
2017/11/04 2017/11/17 2017/11/30 2017/12/13 : START
2017/12/01 2017/12/14 2017/12/27 2018/01/09 : END

横にSTART,ENDを並べるのをやめて、縦にしました。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?