Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert line of text into the middle of file

Status
Not open for further replies.

unomateo

IS-IT--Management
May 9, 2006
38
US
I am new to perl. I can append a line of text to a file easily using perl, but I need to insert a line of text in the middle of the text file.

Example text file:

line 1
line 2
line 3
line 4

I need to insert a line of text after line 2.
so the output will be

line 1
line 2
new inserted line
line 3
line 4



 
in the middle or after line number N? These are two different concepts. To know where the middle is implies you know the number of lines, and files with an odd number of lines will not have a exact middle.

But to do what you want can be done few ways.

1. Use the Tie::File module
2. Read the entire file into an array
3. Use perls inplace editor

Using perls inplace editor :

Code:
[gray]#!perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file[/blue] = [red]'[/red][purple]path/to/file.txt[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$newline[/blue] = [red]"[/red][purple]In general, if you think something isn't in Perl, try it out, because it usually is.[/purple][red]"[/red][red];[/red]
[red]{[/red]
   [url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]@ARGV[/blue] = [red]([/red][blue]$file[/blue][red])[/red][red];[/red]
   [black][b]local[/b][/black] [blue]$^I[/blue] = [red]'[/red][purple].bac[/purple][red]'[/red][red];[/red]
   [olive][b]while[/b][/olive][red]([/red]<>[red])[/red][red]{[/red]
      [olive][b]if[/b][/olive] [red]([/red][blue]$.[/blue] == [fuchsia]2[/fuchsia][red])[/red] [red]{[/red]
         [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$newline[/blue][blue]$/[/blue][/purple][red]"[/red][red];[/red]
      [red]}[/red]
      [olive][b]else[/b][/olive] [red]{[/red]
         [black][b]print[/b][/black][red];[/red]
      [red]}[/red]
   [red]}[/red]
[red]}[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm sorry, I do not mean in the exact middle of the file...

I meant I need to insert text after a string.

this is line 1
This is some more text
Another line
yet another text line

example would be find

insert "new line" after"Another line"

the result:

this is line 1
This is some more text
Another line
new line
yet another text line

Sorry for the confusion...

 
OK, let's see the code you have been trying. Is this student work?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
No this is not student work.
I am able to run a sed command to do it...how would I call the sed command in perl?



This appends - no problem
Code:
open (IN, ">>filename") || or die "error";
print IN<<EOM;
some text
EOM;
close(IN);

That is where my perl skills end

here is the code that I managed to figure out for inserting text:
Code:
open (IN, "<filename") || or die "error";
while(<IN>)
{
my($line) = $_;
print "$line\n";
}
close(IN);
 
I can see how to read each line...

the problems I'm having is finding the line that has the specific text and then writing to the file.

I think that I have to write to a temp file, then replace the original file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top