![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to insert text between lines of an existing file using perl | madhul2002 | Shell Programming and Scripting | 3 | 01-19-2009 03:08 AM |
| Insert Title To Each Lines | tqlam | Shell Programming and Scripting | 2 | 11-05-2008 10:07 PM |
| Insert lines between delimiters | BMDan | Shell Programming and Scripting | 2 | 09-04-2008 06:04 PM |
| insert multiple lines into a file | c0mrade | Shell Programming and Scripting | 12 | 09-04-2008 05:04 PM |
| How to count lines - ignoring blank lines and commented lines | kthatch | UNIX for Dummies Questions & Answers | 6 | 05-25-2007 02:21 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Hi, New problem, or challenge as they prefer in the US. I need to insert some lines in a file before certain other lines. To make it more clear: Original file: Code:
aaaa bbbbb ccccc ddddd bbbbb fffff ggggg Now I want to insert the line "NEW_NEW_NEW" when I match "fffff", but I want it to be before the 5th line with "bbbbb". Like this: Code:
aaaa bbbbb ccccc ddddd NEW_NEW_NEW bbbbb fffff ggggg Notice that the line with "bbbbb" is more times present, so I cannot use that as search string. It is the last present line with "bbbbb" though, perhaps that helps. I tried: Code:
$file_to_update = "test.txt";
open (FILE_TO_UPDATE, "<$file_to_update");
my @lines = <FILE_TO_UPDATE>;
close (FILE_TO_UPDATE);
open (FILE_TO_UPDATE, ">$file_to_update");
for (@lines) {
if ($_ =~ m/fffff/) {
print FILE_TO_UPDATE "NEW_NEW_NEW";
}
print FILE_TO_UPDATE;
}
close (FILE_TO_UPDATE);
But then I get the wrong order: Code:
aaaa bbbbb ccccc ddddd bbbbb NEW_NEW_NEW fffff ggggg Anyone with some ideas ? E.J. |
|
||||
|
Thanks for the one-liner. For this example it works, but you understand it is part of a bigger universe :-) And I am having trouble incorporating it in the example code. Not to mention that I do not understand the wizardry in your one-liner. I understand it is a conditional substitute, but what is the $ and the & doing for example ? But let me make the problem a bit more real life. Here is the test.txt data file (edited for the forum). It is a ksh script and this is how it comes after an installation. I need to modify it and I would like to have this modification done by a script. Code:
if [ -f $topload_inprogress ] then blahlblah rm $inprogress exit 1 fi if [ ! -f $params ] then blahlblah rm $inprogress exit 1 fi if(($?==1)) then blahlblah rm $inprogress exit 1 fi if [[ -f $ftp_fin ]] then rm $ftp_fin else blahlblah rm $inprogress exit 1 fi files=`ls $CADATA/srcdata/$source/custom/BULK/after` for name in $files do blahlblah done rm $inprogress $CAROOT/prog/ca_log_message TOPOLOGY_STAT "Finished the bulk ftp topology load of $source data source." exit 0 I need to insert a block-text (6 lines) between last 'done' and the 'rm $inprogress'. Can your "(?!.*bbbbb)" be of any help there ? Greetings, E.J. |
|
||||
|
Quote:
And if I have to "prepair" it manually, then I can make the text insert manually too. But I don't want to. The last "rm $inprogress" is at the end of the file, so it is the last occurrence. The text-block needs to be between the last "done" and the last "rm $inprogress", that's a given. I use Perl, because I need to make many more modifications in other files and I need to start things, stop things, etc. Perl is perfect for this. This is one of the many sub-routines in the script. In fact I need to make 2 text-block insertions in this file and the second one has this problem I described. If necessary I could call a sed command from within Perl, right ? But I do not see why it could not be done with Perl. Okay, I can't, but I am not the brightest Perl programmer around ![]() Not yet. ![]() Now I am thinking about counting the "done"s, printing all lines including the last "done". Then print my text-block and then print the last lines. Or perhaps read 2 lines, test the 2nd line for "Finished the bulk ftp". Print all lines until the test is true, print my test-block and print the last lines. Something should be possible. E.J. |
|
|||||
|
Quote:
you can use something like this (you should add exception handling, of course): Code:
#!/usr/bin/perl
use warnings;
use strict;
my ( $shellscript, $pattern ) = ( shift, 'rm \$inprogress' );
my $newcode = <<_NEWCODE_;
line1
line2
...
line6
_NEWCODE_
{
local ( $^I, @ARGV, $/ ) = ( '', $shellscript );
while (<>) {
s/$pattern(?!.*$pattern)/$newcode\n$&/s;
print;
}
}
Quote:
![]() I'm not saying that you should not use Perl for this, but that sed is sufficient for this task. And yes, of course, if this is only one of the tasks and you have other code in Perl, you should do it all in Perl. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|