tech::hexagram

personal note for technical issue.

bibtex用のちょっとしたツールを作った

研究室の論文紹介用ページをリニューアルすることになりまして,今週はその準備作業に追われていました.

手作業で論文のbibファイルを何個も作ると,

@article{samlple hoge hoge,
  title={sample hoge hoge},
  author={piyo and puyo},
  ...
}

このように,論文のタイトル名をそのままコピペすると,citeする時にエラーになってしまいます.
(1行目の論文名にスペースが含まれてしまっているので)

そこで,perlを使って簡単な置換プログラムを書きました.やることはただひとつで,

  • 1行目の半角スペースを抜く

ただそれだけ.

#!/usr/bin/perl

use strict;
use File::Copy;

my $dir_from='bibtex/';
my $dir_to='bibtex_conv/';
my $line_counter=0;
opendir DH,$dir_from or die "$dir_from:$!";


while(my $file = readdir DH){
    next if $file =~ /^\.{1,2}$/;
    print $file,"\n";
    $line_counter=0;

    my $src_file=$dir_from.$file;
    open(IN,$src_file);
    my $dst_file=$dir_to.$file;
    open(OUT,">> ".$dst_file);
    while(<IN>){
	my $buf=$_;
	chomp($buf);
	if($line_counter==0){ $buf=~ s/\s//g;}
	print(OUT $buf."\n");
	$line_counter++;
    }

    #close
    close(IN);
    close(OUT);
}

closedir DH;

いつぞやのリネームプログラムを参考に,ちょろっと書き換えた程度ですw