从一个文件复制选定的行,并将这些行插入选定行后的另一个文件中(Copy selected lines from one file and insert those lines in another file after selected line)
这里是我的问题让我们说我有一个文件file1.txt与内容:
abc.. def.. ghi.. def..和第二个文件file2.txt的内容:
xxx.. yyy.. zzz..现在我想将file1.txt以“def”开头的所有行复制到file2.txt并在file2.txt “yyy ...”行file2.txt
预期产出:
xxx... yyy... def... def... zzz...我是perl的新手,我已经尝试为此编写简单的代码,但最终的输出只能追加到文件末尾
#!/usr/local/bin/perl -w use strict; use warnings; use vars qw($filecontent $total); my $file1 = "file1.txt"; open(FILE1, $file1) || die "couldn't open the file!"; open(FILE2, '>>file2.txt') || die "couldn't open the file!"; while($filecontent = <FILE1>){ if ( $filecontent =~ /def/ ) { chomp($filecontent); print FILE2 $filecontent ."\n"; #print FILE2 $filecontent ."\n" if $filecontent =~/yyy/; } } close (FILE1); close (FILE2);perl程序的输出是
xxx... yyy... zzz... def... def...Here is my question let's say I have a file file1.txt with contents:
abc.. def.. ghi.. def..and second file file2.txt with contents:
xxx.. yyy.. zzz..Now I want to copy all the line starting with "def" in file1.txt to file2.txt and append after "yyy..." line in file2.txt
Expected Output:
xxx... yyy... def... def... zzz...I am pretty much new to perl, I've tried writing simple code for this but end up with output only appending at the end of file
#!/usr/local/bin/perl -w use strict; use warnings; use vars qw($filecontent $total); my $file1 = "file1.txt"; open(FILE1, $file1) || die "couldn't open the file!"; open(FILE2, '>>file2.txt') || die "couldn't open the file!"; while($filecontent = <FILE1>){ if ( $filecontent =~ /def/ ) { chomp($filecontent); print FILE2 $filecontent ."\n"; #print FILE2 $filecontent ."\n" if $filecontent =~/yyy/; } } close (FILE1); close (FILE2);output of the perl program is
xxx... yyy... zzz... def... def...最满意答案
我会使用临时文件。
从FILE2中读取并打印所有行(至temp),直到您点击“yyy” 从FILE1读取并打印所有“def”行(至temp) 读取并打印(温度)FILE2的其余部分 将临时文件重命名为FILE2use strict; use warnings; my $file1 = "file1.txt"; my $file2 = "file2.txt"; my $file3 = "file3.txt"; open(FILE1, '<', $file1) or die "couldn't open the file!"; open(FILE2, '<', $file2) or die "couldn't open the file!"; open(FILE3, '>', $file3) or die "couldn't open temp file"; while (<FILE2>) { print FILE3; if (/^yyy/) { last; } } while (<FILE1>) { if (/^def/) { print FILE3; } } while (<FILE2>) { print FILE3; } close (FILE1); close (FILE2); close (FILE3); rename($file3, $file2) or die "unable to rename temp file";I'd use a temp file.
Read and print all lines (to temp) from FILE2 until you hit "yyy" Read and print all "def" lines (to temp) from FILE1 Read and print (to temp) the rest of FILE2 Rename the temp file to FILE2use strict; use warnings; my $file1 = "file1.txt"; my $file2 = "file2.txt"; my $file3 = "file3.txt"; open(FILE1, '<', $file1) or die "couldn't open the file!"; open(FILE2, '<', $file2) or die "couldn't open the file!"; open(FILE3, '>', $file3) or die "couldn't open temp file"; while (<FILE2>) { print FILE3; if (/^yyy/) { last; } } while (<FILE1>) { if (/^def/) { print FILE3; } } while (<FILE2>) { print FILE3; } close (FILE1); close (FILE2); close (FILE3); rename($file3, $file2) or die "unable to rename temp file";
更多推荐
发布评论