January 28th, 2009 View Comments
주로 하는 짓거리가 텍스트를 가지고 조물락 거리는 거라,
Unix에서 find, sed, awk 조합으로 어지간 한거는
bash/perl/ruby까지 가지 않고 처리하고 있지만,
파일을 열어, 수정하고, 다시 저장하려면, open 한 파일과 save할 파일이 동일해서,
파이프( | ) 를 이용해서 tmp파일을 만들고는 하지만,
find -exec 속에서 파이프를 쓸 수가 없어서 이럴때는 perl one liner가 제격이다.
자주 쓰는 Perl one liner…
find . -name "file.*" -exec perl -i -pe 's|BEFORE|AFTER|g' {} ;
그 밖의 perl one liner
링크1, 링크2
태그: codesnipet, perl, unix, 팁
카테고리: IT
November 13th, 2007 View Comments
회사서 Class를 조사할 일이 있는데 reflect를 까먹어서 한동안 googling 해서 다시 학습…
java.lang.String의 method를 출력하는 루틴의 골자만 요약하면….
import java.lang.reflect.*;
public class ClassExam{
public void printClassInfo(String className){
try{
Class a = this.getClass().getClassLoader().loadClass(className);
Method[] methods = a.getDeclaredMethods();
for(int i=0; i<methods.length; i++){
System.out.println(methods[i].toString());
}
}catch(Exception e){
}
}
public static void main(String[] args){
ClassExam a = new ClassExam();
a.printClassInfo("java.lang.String");
}
}
태그: codemonkey, codesnipet, IT, java, 프로그래밍
카테고리: IT
August 30th, 2006 View Comments
Unix스타일의 프로그램 파라미터 처리를 해주는 모듈이 Getopt인데
자주 사용하다 보니 codesnipet으로 사용하려고, 내가 사용하는 템플릿을 여기에 등록해 둔다.
main::HELP_MESSAGE(), main::VERSION_MESSAGE() 를 구현해 두면 – -help, – -version 옵션에 반응하고, getopts(c:hv)에 사용할 옵션들을 나열하면 된다.
위 예에서 c뒤에 : 의 의미는 파라미터를 받는 다는 의미이다.
입력된 파라미터는 $Getopt::Std::opt_c로 사용할 수 있다.
#!/usr/bin/perl
use strict;
use Getopt::Std;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
sub main::VERSION_MESSAGE(){
print "$0 Version 0.5 hyeonkwen@gmail.com";
}
sub main::HELP_MESSAGE(){
print <<END;
Description of this program.
Usage: $0 OPTION DIRECTORY
OPTION
-c description of c option
-h view help message
-v view version information
END
}
sub main(){
if(!getopts("c:hv") || $Getopt::Std::opt_h){
main::HELP_MESSAGE();
exit;
}elsif($Getopt::Std::opt_v){
main::VERSION_MESSAGE();
exit;
}
}
main();
태그: codesnipet, perl, 프로그래밍
카테고리: IT