Solved

Insert a line in an order TEXT file

Posted on 1998-07-22
5
179 Views
Last Modified: 2010-03-04
Hi,

I would like to know if they have a way to insert a line in
an order TEXT file
example :
Line to insert = "HAA|123"

TEXT file:
AAA|111
BBB|222
>> insert here HAA|123 by looking the 3 first characters
NAA|555
PPP|666
QQQ|777
0
Comment
Question by:woaini
  • 2
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 50 total points
ID: 1208623
use strict;

open(SRC, "<orig.file.name") || die "Couldn't open source";
open(DST, ">orig.file.name.TMP") || die "Couldn't open destination";

my($target) = $ARGV[1]; ## The first parameter is the line to insert
while (<SRC>) {
   chomp;
   if ($target le $_) {
      print DST "$target\n";
      print DST "$_\n";
     while (<SRC>) {
         print DST $_;
    }
   print "$_\n";
}
close(SRC);
close(DST);
unlink ("orig.file.name");
rename("orig.file.name.TMP", "orig.file.name");


0
 
LVL 84

Expert Comment

by:ozo
ID: 1208624
#or
$target = (shift)."\n";
{local $^I = ".TMP"; local @ARGV=("orig.file.name");
 while( <> ){
   print $target if substr($target,0,3) le substr($_,0,3) && ??;
   print;
 }
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208625
Oops, that doesn't work if $target comes after all the lines in orig.file.name
(actually, neither does the original answer,
but b2pi's version is easier to fix)
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208626
Good point, Ozo.

Add to above: (directly before
close(SRC);

the following:

print "$target\n" if $target gt $_;
close(SRC);
etc...


0
 

Author Comment

by:woaini
ID: 1208627
Thank a lot b2pi !!
Thank ozo too.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now