Welcome to the monastery. From the task description I'd say perl is very well suited. I'll give you an example for a program that roughly does what you describe
#!/usr/bin/perl
use warnings;
use strict;
my $filename = "whateveryourfileiscalled.txt";
my $newfile = "whateveryouwantthechangedfiletobecalled.txt";
open (my $rfh,"<",$filename) or die "Can't open file $filename : $!";
open (my $wfh,">",$newfile) or die "Can't open file $newfile : $!";
while (my $line = <$rfh>) {
if ($line =~ m/^HEADER/) {
chomp $line;
my $number = 42; # change to whatever number you want to use
$line .= $number."\n";
}
if ($line =~ m/^REMARK/) {
print {$wfh} "Extra line\n" # Change to whatever extra line yo
+u want
}
print {$wfh} $line;
}
close $rfh or die "Can't close $filename : $!";
close $wfh or die "Can't close $newfile : $!";
Or you could do this in a perl oneliner (which will change the original file):
perl -pi -e 'chomp;s/^(HEADER.*)$/${1}42/;s/^(REMARK.*)$/Extra line\n$
+1/;$_.="\n"' whateveryourfileiscalled.txt
Caveat: Both of these are for systems where the line ending is "\n" (i.e. not Windows), adjust appropriately for other OSes. Update: the caveat is not actually correct, as pointed out by naikonta and wfsp, except possibly for the case outlined by Sixtease, also fixed in his update. Thanks to all of you.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|