Insert lines at specific location in file using perl script

问题内容:

Want to add a new line in a file using perl script. I have tried but no luck

 if($correctedContents[$i] ~= /second line/){
 @data = $correctedContents[$i]
  push (@data, '\n TEST Line');

For example: add a “TEST Line” into a file whenever it find ‘second line’ line.

file1.txt,

first line
second line
third line

first line 
second line 
third line

…..

After added a new line
expected output:

first line
second line
TEST Line
third line

first line 
second line 
TEST Line
third line

Thanks in Advance

问题评论:

    
using if condition i found the ‘second line’, then assigned to a array. finally i used push command to add a line but it was not worked
– Beginner
3 hours ago
    
Could you please go through the answers which you have received since @dave cross mentioned already Tie::File module in his answer. There is a few samples available in search engine based on the modules.

答案:

答案1:

You may use Tie::File can get the output what you have expect

use Tie::File;

my $inputFile = "test.txt";

my @array;
tie @array, 'Tie::File', $inputFile or die "Error: Couldn't tie the \"$inputFile\" file: $!";
my $len = join "\n", @array;

if($len=~m/secondline/i) {  $len=~s/secondline/TEST Line/ig;  }

@array = split/\n/, $len;
untie @array;

Tie::File will replace the string in a same input file.

答案评论:

    
1. Using || instead of or here might lead to precedence issues. 2. Using join and split seems a very strange approach. 3. You haven’t added a line, you have overwritten it.
    
@DaveCross: #1 will update in answer. #2. I don’t know how to change instead of join/split using in code. #3. Tie::File will replace the string in a same input file. I have mentioned this.
    
You could fix your solution by changing the substitution to: s/second line/second line\nTEST line/. Because currently, your answer doesn’t do what @beginner asked for. I have no idea why he accepted it.
    
@DaveCross: I know why he accepted since definitely he will modify the answer what he wants to do. He needs only logic how to do. We are beginners and waves are matched than you who are very brilliant.

答案2:

You haven’t really shown us enough of your code for us to be very much help. But, in particular, you haven’t shown any code where you write the changed data back to an output file.

In general, tasks like this boil down to three stages:

  1. Read data from file.
  2. Update data in some way.
  3. Write changed data back to file.

One common approach is to have two filehandles open – one to your input file and another to a new output file. That makes it simple to process the file a line at a time.

while (<$input_fh>) {
  if (this is a line you need to change) {
    # make changes to line (which is in $_)
    # Perhaps print an extra line here.
  }
  print $output_fh $_;
}

Another approach (which trades speed for ease of use) is to use the Tie::File module (which is part of all Perl distributions since 5.8).

As always, the Perl FAQ is a good place for more information. In this case, you probably want to look at perlfaq5, which contains the question How do I change, delete, or insert a line in a file, or append to the beginning of a file?

Update: You already have one (slightly problematic) Tie::File-based solution. So here’s mine:

#!/usr/bin/perl

use strict;
use warnings;

use Tie::File;

tie my @lines, 'Tie::File', 'somefile.txt'
  or die "Can't tie file: $!\n";

for (@lines) {
  $_ .= "\nTEST line" if $_ eq 'second line';
}

答案评论:

    
i dont want to change the existing line. Need to add a new line
– Beginner
3 hours ago
    
dont want print the output in display, need to insert a line into a file
– Beginner
3 hours ago
    
@Beginner Dave showed you here to do exactly that. It seems that you need to perhaps start by learning some perl and perhaps you would understand more what has been said here.
    
And if you took the time to read my answer and the document that I linked to, then you would realise that I have explained how to do everything you ask. If you have questions about my answer, then feel free to ask them. But don’t just assume I haven’t answered your question.

原文地址:

https://stackoverflow.com/questions/47753065/insert-lines-at-specific-location-in-file-using-perl-script

添加评论

友情链接:蝴蝶教程