DEV/Perl
FILE
초록매실원액
2015. 12. 23. 13:10
파일 핸들
- Perl 프로그램과 프로그램 외부의 어떤 존재와 입출력을 위한 스트림
파일을 사용하기 위한 과정
- 대상파일을 열고 파일핸들과 연결
- 파일핸들을 통해 읽고 쓴다.
- 파일에 대한 사용이 끝나면 파일 핸들을 닫는다.
파일 읽기
- Open함수 사용
- Ex) open my $로, "<", "filename";
- Ex) open my $로, "<", "filename" or die "파일을 열수 없습니다.";
파일 닫기
- Close 함수 사용
- Ex) close $fh;
perl filename.pl "열고자 하는 파일"
파일 쓰기
Open 함수 사용
Ex) open my $fh,">","filename";
Ex) print $fh, "data save \n";
파일 추가해서 쓰기
Ex) open my $fh, ">>","filename";
Ex) print $fh " add data \n";
copy file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | use strict; use warnings; (my $file1, my $file2) = @ARGV; my $line; open my $fh, "<", $file1 or die "can't open file.\n"; open my $fh2,">",$file2; while($line =<$fh>){ print $fh2 $line; } close $fh; | cs |
# file1 file2 에서 다른 파일을 라인별로 비교
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | use strict; # file1 file2 에서 다른 파일을 라인별로 비교 =xxx (my $file1,my $file2) = ("file1.txt","file2.txt"); open my $fh1, "<", $file1 or die "can't open file.\n"; open my $fh2, "<", $file2 or die "can't open file.\n"; #open my $fh3, ">", "find.txt"; my $i=0; my @str1= <$fh1>; my @str2= <$fh2>; for($i=0;$i<=@str1;$i++){ if($str1[$i] ne $str2[$i]){ print $str1[$i]; print $str2[$i]; } } close $fh1; close $fh2; =cut # file1에 있는 단어중 file2에 있는 단어를 찾음 (my $file1,my $file2) = ("word1.txt","word2.txt"); open my $fh1, "<", $file1 or die "can't open file.\n"; open my $fh2, "<", $file2 or die "can't open file.\n"; my $i=0; my $j=0; my $line; my @str1= <$fh1>; my @str2= <$fh2>; my @str1_word; my @str2_word; for($i=0;$i<=@str1;$i++){ @str1_word = split ' ', @str1[$i]; for(my $j=0;$j<=@str1_word;$j++){ #print $str1_word[$j]; if($str1_word[$j] eq ""){ next; } for(my $k=0;$k<=@str2;$k++){ @str2_word = split ' ', @str2[$k]; for(my $m=0;$m<=@str2_word;$m++){ #print $str2_word[$m]; if($str1_word[$j] eq $str2_word[$m]){ print $str2_word[$m]; print "\n"; last; } } } } } close $fh1; close $fh2; | cs |
# file1 file2 에서 다른 파일을 라인별로 비교
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | use strict; # file1 file2 에서 다른 파일을 라인별로 비교 =xxx (my $file1,my $file2) = ("file1.txt","file2.txt"); open my $fh1, "<", $file1 or die "can't open file.\n"; open my $fh2, "<", $file2 or die "can't open file.\n"; #open my $fh3, ">", "find.txt"; my $i=0; my @str1= <$fh1>; my @str2= <$fh2>; for($i=0;$i<=@str1;$i++){ if($str1[$i] ne $str2[$i]){ print $str1[$i]; print $str2[$i]; } } close $fh1; close $fh2; =cut # file1에 있는 단어중 file2에 있는 단어를 찾음 (my $file1,my $file2) = ("word1.txt","word2.txt"); open my $fh1, "<", $file1 or die "can't open file.\n"; open my $fh2, "<", $file2 or die "can't open file.\n"; my $i=0; my $j=0; my @str1= <$fh1>; my @str2= <$fh2>; my $line; $a="apple"; $aa="a"; $a =~s//$aa/e; print $&; for($i=0;$i<=@str1;$i++){ my @str1_word = split ' ', $str1[$i]; for($j=0;$j<=@str1_word;$j++){ $str1_word[$j]; } } close $fh1; close $fh2; | cs |