それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

シェルコマンド、sed で改行の置換して、一行にする。

sed で改行の置換をする方法

 sed ':a;N;$!ba;s/\n/<replacement_txt>/g' 

いろいろフラグを追加したら出来るみたい。

記号にギョっとするけど、ちゃんと出来るよ

takuya@aqua-iot$ cat -  | sed ':a;N;$!ba;s/\n/ /g'
Hello
world
I
am
Hello world I  am

一瞬記号に面食らって何だこれ?ってなると思うんだけど、ちゃんと出来るよ

これは次のsedオプションを置換指定に入れたGNU sed 記法です

なので、ぎょっとする記号は、次のオプションと同じですね。

sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g' file

実行サンプル

実際にヤってみた。

takuya@atom:/var/www/epgrec$ curl -s 'http://hatenablog.com/oembed?url=http%3A%2F%2Ftakuya-1st.hatenablog.jp%2Fentry%2F20120229%2F1330519953&format=xml' |  sed ':a;N;$!ba;s/\n/<text>/g'
<?xml version="1.0" encoding="utf-8" standalone="yes"?><text><oembed><text>  <author_name>takuya_1st</author_name><text>  <author_url>http://blog.hatena.ne.jp/takuya_1st/</author_url><text>  <blog_title>それマグで!</blog_title><text>  <blog_url>http://takuya-1st.hatenablog.jp/</blog_url><text>  <categories><text>  </categories><text>  <description>XMLをもらったけど、階層ツリーがむちゃくちゃでわからない tidy -utf8 -i --input-xml true --indent-cdata true ./sample.xml しかも昔にエントリ書いてるし。www tidy のインストール。 mac はじめから linux debian gnu/linuxは aptで windows コンパイル済みバイナリをどこかでダウンロードしてくる cygwin setup.exeや apt-cyg で XMLの整形 tidyでxmlを整形するには・ -xml オプションを付ける。tidy -xml ./sample.xml または tidy-…</description><text>  <height>190</height><text>  <html>&lt;iframe src=&quot;http://takuya-1st.hatenablog.jp/embed/20120229/1330519953&quot; title=&quot;XMLを整形(tidy)して読みやすく、貼りつけやすくする。 - それマグで!&quot; class=&quot;embed-card embed-blogcard&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;&quot;&gt;&lt;/iframe&gt;</html><text>  <image_url></image_url><text>  <provider_name>Hatena Blog</provider_name><text>  <provider_url>http://hatenablog.com</provider_url><text>  <published>2012-02-29 21:52:33</published><text>  <title>XMLを整形(tidy)して読みやすく、貼りつけやすくする。</title><text>  <type>rich</type><text>  <url>http://takuya-1st.hatenablog.jp/entry/20120229/1330519953</url><text>  <version>1.0</version><text>  <width>100%</width><text></oembed>

参考資料

http://www.canbike.org/information-technology/sed-delete-carriage-returns-and-linefeeds-crlf.html

https://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed

2018/03/05

追記した

2019/05/16

カテゴリ整理

ruby でDateTime(日付時刻)を1時間進める

ruby で日付時刻を進める。

方法はとても簡単です。

DateTime.now + 1.0/24 # 1時間進める
DateTime.now + 2.0/24 # 2時間進める

ポイントは、Floatで与えること。

DateTime.now + 1/24 # だめ! Integerだから。
DateTime.now + 1/24.0 # 可能! Floatだから。

超かんたん。

単純に四則演算で、時間を操作できるのは素晴らしい。加算だけでいいから楽ね。

サンプル

=> #<DateTime: 2015-03-19T16:38:12+09:00 ((2457101j,27492s,679731000n),+32400s,2299161j)>
>> (d).strftime("%F %T")
=> "2015-03-19 16:38:12"
>> (d + 1.0).strftime("%F %T")
=> "2015-03-20 16:38:12"
>> (d + 1.0/24).strftime("%F %T")
=> "2015-03-19 17:38:12"
>> (d + 1.0/24*3).strftime("%F %T")
=> "2015-03-19 19:38:12"
>>

ics カレンダを作る。サンプルデータ

ics ファイルを作るには

予定表を登録するためにicsのカレンダファイルを作る

ICS ファイルの構造

BEGINーENDで囲まれた内部にテキストを作る。

一番外側の BEGIN:VCALENDAR

BEGIN:VCALENDAR
VERSION:2.0
PRODID:takuya_1st-ics-events.rb
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME;VALUE=TEXT:takuyaの予定
BEGIN:VTIMEZONE
TZID:Asia/Tokyo
BEGIN:STANDARD
DTSTART:19700101T000000
TZOFFSETFROM:+0900
TZOFFSETTO:+0900
TZNAME:JST
END:STANDARD
END:VTIMEZONE
#{ics_events}
END:VCALENDAR

予定は VEVENT

BEGIN:VEVENT - END:VEVENTをブロックスとして、複数個の予定を入れていく。

BEGIN:VEVENT
DTSTAMP:20150319T172703Z
UID:5c9f8d9330dd09857532a82dfd579a48
DTSTART:20150430T030000
DTEND:20150430T040000
DESCRIPTION: お買い物
SUMMARY:牛乳を買いに行く(牛乳、バター、チーズを買う)
END:VEVENT

日付時刻のフォーマットサンプル

ruby の strftime でフォーマットするとこんな感じ

events = [ [ [],[],[],[],], [ [],[],[],[],]  ... ]
events = event.map{|e| 
    { time_at:DateTime.parse( e[1] + " " + e[2] + " JST"),
      name:e[4],
    }
}


ics_events =  fx_major_events.map{|e|
ical_event =  <<-"DATA"
BEGIN:VEVENT
DTSTAMP:#{DateTime.now.strftime("%Y%m%dT%H%M%SZ")}
UID:#{Digest::MD5.hexdigest(e.inspect)}
DTSTART:#{(e[:time_at]).strftime("%Y%m%dT%H%M%S")}
DTEND:#{(e[:time_at]+1/24.0).strftime("%Y%m%dT%H%M%S")}
DESCRIPTION: #{e[:name]}
SUMMARY: #{e[:name]}
END:VEVENT
DATA
}.join("\n")