펄을 이용해 특정 라인에 내용 바꿀려는데.. 모르겠어요ㅜㅜ

arna0320의 이미지

펄을 이용해 실험프로그램을 구성하는데 처음으로 코딩이라는 걸 해 보니 어렵습니다.
하나씩 검색하면서 여기서 도움 많이 받았는데 또 막혀서 결국 도움글까지 올리게 되었습니다ㅜㅜ

총 100여개의 실험 샘플에 대한 실험 결과1에 대한 결과값 파일이 있습니다.

#!MLF!#
"'*'/Bn036.rec" # 샘플1 파일명
0 264000000 benign -6817.687500 # 샘플 1에 대한 결과값
.
"'*'/BL5_44100.rec" # 샘플2 파일명
0 61600000 benign -1725.979614 # 샘플 2에 대한 결과값
.
# 100개의 샘플에 대한 파일명과 결과값이 연속으로 나타납니다(다 적기에는 양이 많아서..)

기본이 되는 위의 'result_clean.mlf' 파일의 내용으로 다른 실험을 위해 각 샘플의 결과값(0 264000000 benign -6817.687500)만을 변경하여 동일한 형식으로 'result_clean_ch.mlf'에 저장하고자 합니다.
변결 될 내용은 아래의 for문과 같습니다.

$REC_DIR=`cd`;
chop($REC_DIR);
 
open(fp1,"result_clean.mlf");
open(fp2,">result_clean_ch.mlf");
@line=<fp1>;
chop(@line);
close(fp1);
 
$basic = "@line";
for($j=2;$j<$#line;$j=$j+3)
{		
	$test= "$line[$j]";
	@line2=split(/ /,$line[$j]);	
	$ms_div=$line2[3];
	$ms=$line2[1]/100000;
	$result=$ms_div/$ms;
	print "$ms_div\/$ms=$result\n";
	$line2[1] = "100000";
	$line2[3] = $result;
	print fp2 "$line2[0] $line2[1] $line2[2] $line2[3]\n";
 
}

최종적으로는 다음과 같이 새로운 파일을 만들고 싶습니다.
아니면 원본 파일에 for문으로 수정한 내용만 치환 시킬 수 있어도 되고요

#!MLF!#
"'*'/Bn036.rec" # 샘플1 파일명
0 100000 benign -2.58245738636364 # 샘플 1에 대한 for문으로 변경된 결과값
.
"'*'/BL5_44100.rec" # 샘플2 파일명
0 61600000 benign -2. 80191495779221 # 샘플 2에 대한 for문으로 변경된 결과값

정말 기초적인 내용일 수 있고 복잡할 수도 있는데 자연계열의 저로서는 코딩이 너무 어렵네요..
고수님들 도움 부탁드립니다 꾸~벅

arna0320의 이미지

정말 간단하군요..
if 구문 쓰면 된다는 이야기를 들어 괜히 복잡하게 생각했네요..
펄은 참 다양한 방법이 있는 거 같습니다;;

$REC_DIR=`cd`;
chop($REC_DIR);
 
open(fp1,"result_clean.mlf");
open(fp2,">result_clean_ch.mlf");
@line=<fp1>;
chop(@line);
close(fp1);
 
print "#!MLF!#\n";
for($j=2;$j<$#line;$j=$j+3)
{		
	$name=$line[$j-1];
	@line2=split(/ /,$line[$j]);	
	$result=$line2[3]/line2[1]/100000;
	$line2[3] = $result;
	$line2[1] = "100000";
        print fp2 "$name\n";
	print fp2 "$line2[0] $line2[1] $line2[2] $line2[3]\n";
	print fp2 ".\n";
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.