tags:

views:

65

answers:

3

hi all

I need to edit file , the main issue is to append text between two known lines in the file

for example I need to append the following text

a b c d e f

1 2 3 4 5 6

bla bla

Between the first_line and the second_line

first_line=")"

second_line="NIC Hr_Nic ("

remark: first_line and second_line argument can get any line or string

How to do this by perl ? ( i write bash script and I need to insert the perl syntax in my script)

lidia

A: 

Well to concenate strings you do

my $text = $first_line . $second_line;

or

my $text = $first_line;
$text .= $second_line;
kuroutadori
but I need to append text between the first_line and second_linethis not do this
lidia
remark: the perl syntax need to match the first line and the second line and after that need to insert the text line between the first line and the second line
lidia
A: 

You could read the file in as a single string and then use a regular expression to do the search and replace:

use strict;
use warnings;

# Slurp file myfile.txt into a single string
open(FILE,"myfile.txt") || die "Can't open file: $!";
undef $/;
my $file = <FILE>;

# Set strings to find and insert
my $first_line = ")";
my $second_line = "NIC Hr_Nic (";
my $insert = "hello world";

# Insert our text
$file =~ s/\Q$first_line\E\n\Q$second_line\E/$first_line\n$insert\n$second_line/;

# Write output to output.txt
open(OUTPUT,">output.txt") || die "Can't open file: $!";
print OUTPUT $file;
close(OUTPUT);

By unsetting $/ we put Perl into "slurp mode" and so can easily read the whole file into $file.

We use the s/// operator to do a search and replace using our two search lines as a pattern.

The \Q and \E tell Perl to escape the strings between them, i.e. to ignore any special characters that happen to be in $first_line or $second_line.

You could always write the output over the original file if desired.

The problem as you state it is not solvable using the -i command line option since this option processes a file one line at a time; to insert text between two specific lines you'll need to know about two lines at once.

Dave Webb
THX , another remark how to edit the file itself without to copy the edit on another file? (maybe with option i)
lidia
@lidia - have responded to your comments in an edit of the answer - but I don't think you can do it with `-i`.
Dave Webb
hi Dave THX allot , another question I have many text lines as insert1="something" , insert2="other ..." , insert3=...." ( what the best thing to do if I want to add all texts in to the file
lidia
+1  A: 

I'm not sure if I understand your question correctly. A "before and after" example of the file content would, I think, be easier. Anyhow, Here's my take on it, using splice instead of a regular expression. We must of course know the line numbers for this to work.

Load the file into an array:

my @lines;
open F, '<', 'filename' or die $!;
push @lines, $_ for <F>;
close F;

Insert the stuff (see perldoc -f splice):

splice @lines, 1, 0, ('stuff');

..and you're done. All you need to do now is save the array again:

open F, '>', 'filename' or die $!;
print F @lines;
close F;
gamen