マクロは陰で使うもの。生産性を上げても給料は上がらず仕事が増えるだけなので仕事をサボろう!

指定文字で文字列を分割する方法【strsplit】- Tera Term マクロ

目次

Tera Termマクロで文字列を分割しよう

Tera Term マクロでは、特定の文字で複数の要素が区切られ1行で表現されているような文字列を、対象文字を基準に分割して要素別の文字列を作成することができます。この機能を使用することで CSV などのデータを読み込みパースすることができます。

動作確認環境

  • Tera Term Version 4.104

特定文字を基準に文字列を分割する方法【strsplit】

Tera Term マクロで特定文字を基準にして文字列を分割するためには strsplit コマンドを使用します。

strsplit の使い方
  • strsplit <分割対象文字列> <セパレータ>
補足
  • <分割対象文字列>:分割の対象となる文字列
  • <セパレータ>:文字列分割の基準となる文字。1文字で指定する必要がある
  • 分割した結果の部分文字列は、それぞれシステム変数 groupmatchstr1~groupmatchstr9 に格納される
  • 分割した部分文字列は最大 9 個まで保存される。10個以上の部分文字列に分割される場合、10番目以降の部分文字列は切り捨てられることになる
  • システム変数 result に結果として作成された部分文字列の個数が格納される。10個以上に分割された場合「10」が格納される

以下のように分割対象文字列を「1,2,3,4,5」、セパレータを「,」として strsplit を実行してみます。

string = '1,2,3,4,5'
strsplit string ','

messagebox groupmatchstr1 "groupmatchstr1"
messagebox groupmatchstr2 "groupmatchstr2"
messagebox groupmatchstr3 "groupmatchstr3"
messagebox groupmatchstr4 "groupmatchstr4"
messagebox groupmatchstr5 "groupmatchstr5"
messagebox result "result"

結果として各システム変数には以下のように値が格納されます。

  • groupmatchstr1 ← 1
  • groupmatchstr2 ← 2
  • groupmatchstr3 ← 3
  • groupmatchstr4 ← 4
  • groupmatchstr5 ← 5
  • result ← 5

以下のように10個以上の部分文字列に分割される場合を考えてみます。

string = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15'
strsplit string ','

messagebox groupmatchstr1 "groupmatchstr1"
messagebox groupmatchstr2 "groupmatchstr2"
messagebox groupmatchstr3 "groupmatchstr3"
messagebox groupmatchstr4 "groupmatchstr4"
messagebox groupmatchstr5 "groupmatchstr5"
messagebox groupmatchstr6 "groupmatchstr6"
messagebox groupmatchstr7 "groupmatchstr7"
messagebox groupmatchstr8 "groupmatchstr8"
messagebox groupmatchstr9 "groupmatchstr9"
messagebox result "result"

結果として各システム変数には以下のように値が格納され、10番目以降の部分文字列は切り捨てられます。

  • groupmatchstr1 ← 1
  • groupmatchstr2 ← 2
  • groupmatchstr3 ← 3
  • groupmatchstr4 ← 4
  • groupmatchstr5 ← 5
  • groupmatchstr6 ← 6
  • groupmatchstr7 ← 7
  • groupmatchstr8 ← 8
  • groupmatchstr9 ← 9
  • result ← 10

【マクロ例】CSV ファイルを読み込み各要素を配列に格納する

以下のように「ホスト名,アドレス,ユーザ名,パスワード,接続プロトコル」の形式で記述された CSV ファイルを読み込み、要素別に配列に格納してみます。

host1,192.168.1.1,user1,pass1,ssh
host2,192.168.1.2,user2,pass2,ssh
host3,192.168.1.3,user3,pass3,telnet

マクロ例は以下の通りです。

;; 変数定義
filename = 'test.csv'
strdim hostname 10
strdim address 10
strdim username 10
strdim userpass 10
strdim protocol 10
hostcount = -1 ;ホストの数を格納する変数

;; ファイルから行を読み込み配列に値を格納
fileopen fhandle filename 0
while 1
 filereadln fhandle readline
 if result = 1 break
 
 ;; 文字列の分割
 hostcount = hostcount + 1
 strsplit readline ','
 hostname[hostcount] = groupmatchstr1
 address[hostcount] = groupmatchstr2
 username[hostcount] = groupmatchstr3
 userpass[hostcount] = groupmatchstr4
 protocol[hostcount] = groupmatchstr5
endwhile

;; ファイルクローズ
fileclose fhandle

;; 格納された値の確認
for i 0 hostcount
 sprintf2 message 'hostname=%s, address=%s, username=%s, userpass=%s, protocol=%s' hostname[i] address[i] username[i] userpass[i] protocol[i]
 sprintf2 title '%d番目' i
 messagebox message title
next

実行結果としては以下のように各配列要素に値が格納されます。

  • hostname
    • hostname[0] ← host1
    • hostname[1] ← host2
    • hostname[2] ← host3
  • address
    • address[0] ← 192.168.1.1
    • address[1] ← 192.168.1.2
    • address[2] ← 192.168.1.3
  • username
    • username[0] ← user1
    • username[1] ← user2
    • username[2] ← user3
  • userpass
    • userpass[0] ← pass1
    • userpass[1] ← pass2
    • userpass[2] ← pass3
  • protocol
    • protocol[0] ← ssh
    • protocol[1] ← ssh
    • protocol[2] ← telnet

まとめ

CHECK!
  • 指定文字を基準に文字列の分割をするためには strsplit コマンドを使用する
  • strsplit を使用することで CSV 等のデータをパースできる

Tera Term マクロ関連記事リスト


Amazon で買えるおすすめアイテム(アフィリエイト)

ブログ始めるなら 【アフィリエイトリンク】

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)

目次