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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner, just need to manipulate a txt file

Status
Not open for further replies.

chep80

Technical User
Jul 22, 2005
10
US
I have a fairly simple problem, but I have no experience with Perl and little time to learn it. I have this text file as an input:

Log file 'LOG\atgt.txt'
ELAPSED_SIM_TIME Seconds
AACS_Mode state
ASGN_SlewMode state
ATT_Mode state
ECI2IRU_Q[0] none
ECI2IRU_Q[1] none
ECI2IRU_Q[2] none
ECI2IRU_Q[3] none
RWA[0].Rpm rpm
RWA[1].Rpm rpm
RWA[2].Rpm rpm
RWA[3].Rpm rpm
ANAV_Eclipsed flag
1 1 0 3 8.54E-08 2.59E-08 -5.98E-08 1 1.295752618 1.2989038 5.495479291 -9.591810973 0
2 1 0 3 1.89E-06 5.20E-08 1.07E-06 1 5.612838988 5.626486779 23.80152215 -41.80307628 0
3 1 0 3 5.36E-06 1.96E-07 9.96E-06 1 9.926691625 9.950824754 42.08872415 -74.01484423 0
4 1 0 3 9.08E-05 4.12E-07 8.60E-06 1 14.2371683 14.27177551 60.35674405 -106.1841529 0
5 1 0 5 1.54E-05 7.62E-07 1.50E-05 1 18.54407687 18.58914691 78.62846211 -138.3115435 0
6 1 0 5 2.69E-05 2.27E-06 6.30E-05 0.999999999 22.84718311 22.90270475 96.88993192 -170.3976624 0
7 1 0 5 2.94E-05 1.70E-06 3.28E-05 0.999999999 27.1611309 27.21218229 115.1304895 -202.4432368 0
8 1 0 5 6.29E-05 2.29E-06 6.43E-05 0.999999998 31.472361 39.51729109 133.3494285 -234.4490483 0
9 1 0 5 7.82E-05 2.80E-06 5.74E-05 0.999999996 38.77891735 35.81773271 151.5460174 -269.4159032 0



And all I need from this file is the first column and the fifth through the eigth column. Then I want to take this data and put it into a file that looks like this:

stk.v.4.2
BEGIN Attitude
NumberofAttitudePoints 9
ScenarioEpoch 1 Sep 2007 12:00:00.00
AttitudeTimeQuaternions
1 8.54E-08 2.59E-08 -5.98E-08 1
2 1.89E-06 5.20E-08 1.07E-06 1
3 5.36E-06 1.96E-07 9.96E-06 1
4 9.08E-05 4.12E-07 8.60E-06 1
5 1.54E-05 7.62E-07 1.50E-05 1
6 2.69E-05 2.27E-06 6.30E-05 0.999999999
7 2.94E-05 1.70E-06 3.28E-05 0.999999999
8 6.29E-05 2.29E-06 6.43E-05 0.999999998
9 7.82E-05 2.80E-06 5.74E-05 0.999999996

Where the "NumberofAttitudePoints" must display the number of rows of data copied over (9). If anyone has any input I would really appreciate it, I've been having trouble finding help on this probably because it is such a basic task. Thanks
 
Very quick and dirty last thing on a Friday afternoon....

Code:
#!/usr/bin/perl
use strict;
my $input = 'C:\mytemp\atgt.txt';
my @output;
open (FILE, "$input");
my @array = <FILE>;
for (@array) {
	my ($col1, $col2, $col3, $col4, $col5, $col6, $col7, $col8) = split(/\s+/, $_);
	my $line = "$col1 $col5 $col6 $col7 $col8";
	if ($line=~m/^\d/) {
		push (@output, $line);
	}
}

my $NoAtP=@output-9;
my $out_file = 'C:\mytemp\out_atgt.txt';
open (OUTFILE, ">$out_file") or print "cannot create $out_file: $!";
print OUTFILE "stk.v.4.2\n";
print OUTFILE "BEGIN Attitude\n";
print OUTFILE "NumberofAttitudePoints $NoAtP\n";
print OUTFILE "ScenarioEpoch 1 Sep 2007 12:00:00.00\n";
print OUTFILE "AttitudeTimeQuaternions\n";

foreach my $entry(@output) {
	print OUTFILE "$entry\n";
}

You'll need to replace the $input and $outfile definitions with the correct addresses of your file(s).
 
Code:
[b]#!/usr/bin/perl[/b]

$pad = "  ";

$bigNum = `tail -n 1 input.txt`;
$bigNum =~ s/^(\d+).*$/$1/;

print $bigNum;

open (IN, "< input.txt");
chomp (@lines = <IN>);
close IN;

foreach (@lines) {
  if (/^\d+/) {
    @components = split(/ +/);
    push @footer, "$components[4]$pad$components[5]$pad$components[6]$pad$components[7]";
  }
}

print <<HERE;
stk.v.4.2
BEGIN Attitude
NumberofAttitudePoints $bigNum
ScenarioEpoch 1 Sep 2007 12:00:00.00
AttitudeTimeQuaternions
HERE

print join("\n", @footer);


Kind Regards
Duncan
 
tonykent,

I replaced my filename and location on the lines for my $input and my $output, but I get the following error:

syntax error at C:\Perl\STK scripts\hds_stk_script1.pl line 17, near "open "
Global symbol "$out_file" requires explicit package name at C:\Perl\STK scripts\
hds_stk_script1.pl line 17.
Global symbol "$out_file" requires explicit package name at C:\Perl\STK scripts\
hds_stk_script1.pl line 17.
Execution of C:\Perl\STK scripts\hds_stk_script1.pl aborted due to compilation e
rrors.
 
a bit of a mish-mash of both of our scripts:-

Code:
[b]#!/usr/bin/perl[/b]

$p = "  ";

open (IN, "< atgt.txt");
chomp (@lines = <IN>);
close IN;

foreach (@lines) {
  if (/^\d+/) {
    @c = split(/ +/);
    push @footer, "$c[4]$p$c[5]$p$c[6]$p$c[7]";
  }
}

$NoAtP = @lines - 13;

print <<HERE;
stk.v.4.2
BEGIN Attitude
NumberofAttitudePoints $NoAtP
ScenarioEpoch 1 Sep 2007 12:00:00.00
AttitudeTimeQuaternions
HERE

print join("\n", @footer);


Kind Regards
Duncan
 
Chep80, when using the \ it either needs to be inside ' symbols or has to be escaped with another \ i.e.

'C:\somedir\file';

or

"C:\\somedir\\file";

Duncan's solution is nicer anyway!
 
Thank you Tony - a very kind comment!

Chep - As you can see in my last posting, i have nicked a couple of bits from Tony's script. He had a nice idea of $NoAtP = @lines - 13 - whereas i stupidly went and ran a system command to look at the last line of the file, and steal the first digit(s)... for example.

So, despite Tony's kind words on my script being 'the best solution' - i think an amalgamation of our scripts looks most concise


Kind Regards
Duncan
 
There is also the input line number variable "$." ($INPUT_LINE_NUMBER) that can possibly be used in situations like this:

Code:
open (IN, "< atgt.txt");
   while (<IN>) {
   next if $. < 14; #skips first 13 lines of file
   ...
}
 
Thanks to all who responded! It worked, I appreciate the help!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top