0
0

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.

tsvファイルからMarkdownテーブルフォーマットを作成するperlスクリプト

0
Posted at

tsvファイルを読み込んで、Markdownのテーブルフォーマットにエクスポートするスクリプト

実行方法

$perl make_mdtable.pl inputfile.tsv > output.md

備考

  • Markdownのテーブル列の形式には、左詰、中央揃え、右詰がありますが、下記スクリプトでは全ての列が左詰になっています。

開発目的

最近何でもマークダウンで書いていますが、tableフォーマットを手書きするのが面倒になってきたことと、perlの練習のために作成してみました。

スクリプト

make_mdtable.pl

# !/usr/bin/perl

use strict;
use warnings;
use utf8;

my $rec_id =0;

while (my $line =<>){
	chomp($line);

	#レコードの数をカウント
	$rec_id ++;

	my @F = split/\t/,$line,-1;

	# フィールドの数をカウント
	my $cnt =@F;

	my $ic =0;
	print "|";
	while($ic < $cnt){
		print "$F[$ic]|";
		$ic++;
	}
	
	print "\n";
	if($rec_id ==1){
		$ic=0;
		while( $ic < $cnt){
			print "|:----------";
			$ic++;
		}
		print "|\n";
	}
}

開発環境

  • macOS Sierra
  • perl 5, version 18, subversion 2 (v5.18.2)
0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?