challenge 条件を満たす行を取り除く

ファイルから1行ずつ読み込み、"#"で始まる行だけを取り除いてファイルに出力するコードを書いてください。

サンプル入力

hello!
# remove this
 # don't remove this
bye!
サンプル出力
hello!
 # don't remove this
bye!

Posted feedbacks - Groovy

1.1-beta-2で動作確認しました。
> groovy script filename
という感じで動作します。
引数チェックやクローズ処理はしてません。手抜きです。
1
2
3
4
5
new File( args[0] ).eachLine( ) {
    if ( it[0] != '#' ) {
        println it
    }
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def inputfile = new File("c:/in.txt")
def outputfile = new File("c:/out.txt")

def buffer = []
inputfile.eachLine{
    if( !(it ==~ /^\s*#.*/) )
        buffer << it
}

outputfile.write( buffer.join("\n") )

Index

Feed

Other

Link

Pathtraq

loading...