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

Hello..how could i extract this ones? 2

Status
Not open for further replies.

Alfio75

Programmer
Oct 25, 2005
25
MX
I got a big file what is making of 8 lines blocks. What I need is to take one block at a time and print it in a file named, for example, 1.dat, take the second one and put it in a 2.dat file.

this is the input file to work:
5.04786152982032E+00 4.23000449311482E+00 8.04732158465680E+00
8.06191440232750E+00 5.82244841863038E+00 4.95787603533274E+00
5.04638324236482E+00 8.33705252525774E+00 7.32433413587005E+00
2.04108475715077E+00 5.82001901588952E+00 5.05415800574768E+00
5.13925614436763E+00 8.40867652716315E+00 2.75714159286964E+00
2.61800142894251E+00 1.77738175002289E+00 5.02054486805876E+00
4.94928280233908E+00 4.07302530110812E+00 2.01973663276942E+00
7.22636291779700E+00 1.72292369993206E+00 5.01056990979596E+00
OCHO
5.04984575451512E+00 4.16899949437203E+00 8.38913015254607E+00
8.41311365271435E+00 5.90089795783486E+00 4.95471182062348E+00
5.03415065613991E+00 8.59685365002286E+00 7.55813423208373E+00
1.74698681832614E+00 5.90648520608937E+00 5.06338313646141E+00
5.18293202209829E+00 8.65797235431727E+00 2.55146839659064E+00
2.37498157493418E+00 1.52435602504604E+00 5.05472507160296E+00
4.94013649443927E+00 3.99148837619247E+00 1.70888099796613E+00
7.45281058614491E+00 1.44078631178572E+00 5.02800797156495E+00

5.06121397319380E+00 4.12058032588327E+00 8.68447945343327E+00
8.72702972867216E+00 5.97125344607309E+00 4.95356174377168E+00
5.02853185579037E+00 8.82735666386533E+00 7.78573599371909E+00
1.49572020273345E+00 5.99391275314096E+00 5.06982138334711E+00
5.21922310707857E+00 8.88757063849985E+00 2.36236099078587E+00
2.13898312096346E+00 1.27787829294346E+00 5.08900607800080E+00
4.93737798070308E+00 3.92065650633274E+00 1.45340060600900E+00
7.66219575537295E+00 1.17896720121153E+00 5.04410388655584E+00

5.07797828387780E+00 4.08080474548203E+00 8.86062092660662E+00
8.92790713427544E+00 6.01972706387360E+00 4.95639755111023E+00
5.01696025227494E+00 9.02715627759038E+00 8.00002249576090E+00
1.35384197118449E+00 6.08692117177528E+00 5.08473103672434E+00
5.25890722173210E+00 9.07644460069736E+00 2.19213416384504E+00
1.93720762154327E+00 1.04452684624935E+00 5.12834330393892E+00
4.93616876462326E+00 3.86635176118538E+00 1.31277147702432E+00
7.86411891303825E+00 9.36328546679487E-01
5.05737978263407E+00

This is what i did last night.

$count=$count+1;
$outfile = $output_directory."/".$infile."_".$count;


#****************************************************
while(<INFILE>) {

open (OUTFILE, ">>$outfile") || die "no se puede abrir $outfile para escribir";
print OUTFILE "$_";
last if ($_ =~ /OCHO/);

close OUTFILE;

}
close INFILE;


But this just print 1 blcok to 1 file..OCHO is just a string to finish the loop.\
Any advice?
 
try
Code:
$/="\n\n";  # Set input record seperator to two lines, 
            #  this may need a tweak on windows
            #  $/="\r\n\r\n";
open INFILE, "<WHATEVER.TXT";
$loop=1;
while(<INFILE>) {
   open (OUTFILE, ">>$outfile.$loop") || die "no se puede abrir  $outfile para escribir";
   print OUTFILE "$_";
   close OUTFILE;
}

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Ok, see if this does what you want:
Code:
#!/usr/bin/perl -w
use strict;

my $number = 1;
open FH, ">$number.dat" or die "Failed to open file [$number.dat]";
while(<DATA>) {
  if(/^(?:OCHO)?\s*$/) {
    close FH;
    $number++;
    open FH, ">$number.dat" or die "Failed to open file [$number.dat]";
    next;
  }
  print FH $_;
}
close FH;
__DATA__
  5.04786152982032E+00  4.23000449311482E+00  8.04732158465680E+00
  8.06191440232750E+00  5.82244841863038E+00  4.95787603533274E+00
  5.04638324236482E+00  8.33705252525774E+00  7.32433413587005E+00
  2.04108475715077E+00  5.82001901588952E+00  5.05415800574768E+00
  5.13925614436763E+00  8.40867652716315E+00  2.75714159286964E+00
  2.61800142894251E+00  1.77738175002289E+00  5.02054486805876E+00
  4.94928280233908E+00  4.07302530110812E+00  2.01973663276942E+00
  7.22636291779700E+00  1.72292369993206E+00  5.01056990979596E+00
OCHO
  5.04984575451512E+00  4.16899949437203E+00  8.38913015254607E+00
  8.41311365271435E+00  5.90089795783486E+00  4.95471182062348E+00
  5.03415065613991E+00  8.59685365002286E+00  7.55813423208373E+00
  1.74698681832614E+00  5.90648520608937E+00  5.06338313646141E+00
  5.18293202209829E+00  8.65797235431727E+00  2.55146839659064E+00
  2.37498157493418E+00  1.52435602504604E+00  5.05472507160296E+00
  4.94013649443927E+00  3.99148837619247E+00  1.70888099796613E+00
  7.45281058614491E+00  1.44078631178572E+00  5.02800797156495E+00

  5.06121397319380E+00  4.12058032588327E+00  8.68447945343327E+00
  8.72702972867216E+00  5.97125344607309E+00  4.95356174377168E+00
  5.02853185579037E+00  8.82735666386533E+00  7.78573599371909E+00
  1.49572020273345E+00  5.99391275314096E+00  5.06982138334711E+00
  5.21922310707857E+00  8.88757063849985E+00  2.36236099078587E+00
  2.13898312096346E+00  1.27787829294346E+00  5.08900607800080E+00
  4.93737798070308E+00  3.92065650633274E+00  1.45340060600900E+00
  7.66219575537295E+00  1.17896720121153E+00  5.04410388655584E+00

  5.07797828387780E+00  4.08080474548203E+00  8.86062092660662E+00
  8.92790713427544E+00  6.01972706387360E+00  4.95639755111023E+00
  5.01696025227494E+00  9.02715627759038E+00  8.00002249576090E+00
  1.35384197118449E+00  6.08692117177528E+00  5.08473103672434E+00
  5.25890722173210E+00  9.07644460069736E+00  2.19213416384504E+00
  1.93720762154327E+00  1.04452684624935E+00  5.12834330393892E+00
  4.93616876462326E+00  3.86635176118538E+00  1.31277147702432E+00
  7.86411891303825E+00  9.36328546679487E-01  5.05737978263407E+00
If it does then all you have to do is swap "<DATA>" for "<>" and pass the name of the file to process on the command line as a parameter.


Trojan.
 
I assumed its only coz he put in OCHO for testing
Alfio75 said:
OCHO is just a string to finish the loop.

my bad

can you set the record seperator to a choice '\n\n||\r\n\r\n';

Tired, ...

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thanks...what a great solution.
Thanks guys I owe u one
Alfred
 
My new question guys.
Now i got this 1,2, 3...600.dat files.
I have a fortran program to work with those files, but i think i sould do a perl script to to get something like next;

sphere=< x,y,z>

where xy and z r all those rows splitting in colums.

Do u think i can do that convert before i print to 1.dat and 2.dat...?

Thanks
 
for each line processed, split on whitespace

($x,$y,$z)=split /\s+/, $_;
print OUTFILE "sphere=<$x,$y,$z>";

HTH
--Paul

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Try this:
Code:
#!/usr/bin/perl -w
use strict;

my $number = 1;
open FH, ">$number.dat" or die "Failed to open file [$number.dat]";
while(<DATA>) {
  chomp;
  if(/^(?:OCHO)?\s*$/) {
    close FH;
    $number++;
    open FH, ">$number.dat" or die "Failed to open file [$number.dat]";
    next;
  }
  print FH "sphere=<", join(",", split),">\n";
}
close FH;
__DATA__
  5.04786152982032E+00  4.23000449311482E+00  8.04732158465680E+00
  8.06191440232750E+00  5.82244841863038E+00  4.95787603533274E+00
  5.04638324236482E+00  8.33705252525774E+00  7.32433413587005E+00
  2.04108475715077E+00  5.82001901588952E+00  5.05415800574768E+00
  5.13925614436763E+00  8.40867652716315E+00  2.75714159286964E+00
  2.61800142894251E+00  1.77738175002289E+00  5.02054486805876E+00
  4.94928280233908E+00  4.07302530110812E+00  2.01973663276942E+00
  7.22636291779700E+00  1.72292369993206E+00  5.01056990979596E+00
OCHO
  5.04984575451512E+00  4.16899949437203E+00  8.38913015254607E+00
  8.41311365271435E+00  5.90089795783486E+00  4.95471182062348E+00
  5.03415065613991E+00  8.59685365002286E+00  7.55813423208373E+00
  1.74698681832614E+00  5.90648520608937E+00  5.06338313646141E+00
  5.18293202209829E+00  8.65797235431727E+00  2.55146839659064E+00
  2.37498157493418E+00  1.52435602504604E+00  5.05472507160296E+00
  4.94013649443927E+00  3.99148837619247E+00  1.70888099796613E+00
  7.45281058614491E+00  1.44078631178572E+00  5.02800797156495E+00

  5.06121397319380E+00  4.12058032588327E+00  8.68447945343327E+00
  8.72702972867216E+00  5.97125344607309E+00  4.95356174377168E+00
  5.02853185579037E+00  8.82735666386533E+00  7.78573599371909E+00
  1.49572020273345E+00  5.99391275314096E+00  5.06982138334711E+00
  5.21922310707857E+00  8.88757063849985E+00  2.36236099078587E+00
  2.13898312096346E+00  1.27787829294346E+00  5.08900607800080E+00
  4.93737798070308E+00  3.92065650633274E+00  1.45340060600900E+00
  7.66219575537295E+00  1.17896720121153E+00  5.04410388655584E+00

  5.07797828387780E+00  4.08080474548203E+00  8.86062092660662E+00
  8.92790713427544E+00  6.01972706387360E+00  4.95639755111023E+00
  5.01696025227494E+00  9.02715627759038E+00  8.00002249576090E+00
  1.35384197118449E+00  6.08692117177528E+00  5.08473103672434E+00
  5.25890722173210E+00  9.07644460069736E+00  2.19213416384504E+00
  1.93720762154327E+00  1.04452684624935E+00  5.12834330393892E+00
  4.93616876462326E+00  3.86635176118538E+00  1.31277147702432E+00
  7.86411891303825E+00  9.36328546679487E-01  5.05737978263407E+00


Trojan.
 
Hello there trojan.
I'm trying to get a line in this format:

#include "background.inc"

#declare atomos=
union{

sphere{< -3.5400 , 2.8462 , 0.0708 >Re}

So i change this line and i get blank lines in the files.
What's wrong with that?

as well I need to put another lines on the top of each file.


print FH "sphere={<", join(",", split),"Re}",">\n";

This is my file.

Would be great to calculate something inside this script.
Sorry guys I'm a Fortran programmer and i met that perl it's a great choose to do this job.

#!/usr/bin/perl -w
#Este script separa en bloques la informacion de las posiciones atomicas obtenidas
#Al usar fil2xyz, es decir usa un archivo que contiene posiciones atomicas en bloques.

#use strict;
print "El archivo de posiciones debe estar en el directorio actual\n";
system pwd;
print "\n";
print "Dame el nombre del archivo de entrada:\n";
$infile = <STDIN>;
chomp $infile;
# checamos si el archivo existe
until (-f $infile) { # -f significa checar si hay un archivo con este nombre
print "Ese archivo no existe\n";
$infile = <STDIN>;
chomp $infile;
}
# Se crea el directorio para los archivos de salida
print "\n";
print "Nombre del directorio que contendra los archivos xyz?\n";

$output_directory = <STDIN>;
chomp $output_directory;
# Verifico que no exista ese nombre de directorio.

while (-d $output_directory) { #
print "Ya existe ese directorio, intenta otro nombre\n";
$output_directory = <STDIN>;
chomp $output_directory;
}
# Si no existe ese directorio, entonces procede a crearse
mkdir ($output_directory, 0777) || die "No se puede crear $output_directory";
opendir (OUTPUTDIR, $output_directory) || die "No se puede abrir $output_directory";
# parametros
print "\n";


###################################################################
# principal
###################################################################

#Creo archivos de acuerdo al numero de pasos que tengo.

open (INFILE, $infile);

my $number = 1;
open FH, ">$output_directory/$number.dat" or die "No puedo abrir [$number.dat]";
#print FH " #include "background.inc"\n";
#print FH "#declare atomos=\n";
#print FH "union {\n";


#abro el archivo como escritura.
while(<INFILE>) {
chomp;
if(/\s*$/) {
# \s indica espacio, caracter en blanco,tab o newline
close FH;
$number++;
open FH, ">$output_directory/$number.dat" or die "No se puede abrir [$number.dat]";
next;
}
print FH "sphere={<", join(",", split),"Re}",">\n";
}
close FH;

close FH;
close INFILE;
print "***** Se acabo el proceso*****\n";
print "$number Archivos creados\n";
 
Hello there trojan.

You did it the next code and that is working but i got a new question.
*********************
#!/usr/bin/perl -w
use strict;

my $number = 1;
open FH, ">$number.dat" or die "Failed to open file [$number.dat]";
while(<DATA>) {
if(/^(?:OCHO)?\s*$/) {
close FH;
$number++;
open FH, ">$number.dat" or die "Failed to open file [$number.dat]";
next;
}
print FH $_;
}
close FH;
__DATA__
A 5.04786152982032E+00 4.23000449311482E+00 8.04732158465680E+00
B 8.06191440232750E+00 5.82244841863038E+00 4.95787603533274E+00
A 5.04638324236482E+00 8.33705252525774E+00 7.32433413587005E+00
B 2.04108475715077E+00 5.82001901588952E+00 5.05415800574768E+00
B 5.13925614436763E+00 8.40867652716315E+00 2.75714159286964E+00
B 2.61800142894251E+00 1.77738175002289E+00 5.02054486805876E+00
B 4.94928280233908E+00 4.07302530110812E+00 2.01973663276942E+00
B 7.22636291779700E+00 1.72292369993206E+00 5.01056990979596E+00
OCHO
B 5.04984575451512E+00 4.16899949437203E+00 8.38913015254607E+00
B 8.41311365271435E+00 5.90089795783486E+00 4.95471182062348E+00
B 5.03415065613991E+00 8.59685365002286E+00 7.55813423208373E+00
A 1.74698681832614E+00 5.90648520608937E+00 5.06338313646141E+00
A 5.18293202209829E+00 8.65797235431727E+00 2.55146839659064E+00
B 2.37498157493418E+00 1.52435602504604E+00 5.05472507160296E+00
B 4.94013649443927E+00 3.99148837619247E+00 1.70888099796613E+00
B 7.45281058614491E+00 1.44078631178572E+00 5.02800797156495E+00

**************
In the data set i got now a new column those are labels A
and B.
I need to read that first column and if the labes is A then print :
print FH "sphere={<", join(",", split),"Ra}",">\n";

but if the label is B i want to print

print FH "sphere={<", join(",", split),"Rb}",">\n";

thanks in advice
 
Try this:
Code:
#!/usr/bin/perl -w
use strict;

my $number = 1;
open FH, ">$number.dat" or die "Failed to open file [$number.dat]";
while(<DATA>) {
  if(/^(?:OCHO)?\s*$/) {
    close FH;
    $number++;
    open FH, ">$number.dat" or die "Failed to open file [$number.dat]";
    next;
  }
  my $firstchar  = lc substr($_,0,1);
  substr($_,0,2) = "";
  print FH "sphere={<", join(",", split),",R$firstchar}",">\n";
}
close FH;
__DATA__
A  5.04786152982032E+00  4.23000449311482E+00  8.04732158465680E+00
B  8.06191440232750E+00  5.82244841863038E+00  4.95787603533274E+00
A  5.04638324236482E+00  8.33705252525774E+00  7.32433413587005E+00
B  2.04108475715077E+00  5.82001901588952E+00  5.05415800574768E+00
B 5.13925614436763E+00  8.40867652716315E+00  2.75714159286964E+00
B  2.61800142894251E+00  1.77738175002289E+00  5.02054486805876E+00
B  4.94928280233908E+00  4.07302530110812E+00  2.01973663276942E+00
B  7.22636291779700E+00  1.72292369993206E+00  5.01056990979596E+00
OCHO
B  5.04984575451512E+00  4.16899949437203E+00  8.38913015254607E+00
B 8.41311365271435E+00  5.90089795783486E+00  4.95471182062348E+00
B  5.03415065613991E+00  8.59685365002286E+00  7.55813423208373E+00
A  1.74698681832614E+00  5.90648520608937E+00  5.06338313646141E+00
A  5.18293202209829E+00  8.65797235431727E+00  2.55146839659064E+00
B  2.37498157493418E+00  1.52435602504604E+00  5.05472507160296E+00
B  4.94013649443927E+00  3.99148837619247E+00  1.70888099796613E+00
B  7.45281058614491E+00  1.44078631178572E+00  5.02800797156495E+00
I assumed that you did not want the "A" or "B" prefix in the output AND that you wanted the last field comma separated so I tinkered to deal with that.
Let me know if this is what you wanted.
As I have said before, if you post sample OUTPUT then you stand a better chance of getting what you want.


Trojan.
 
Yeah, trojan u r the man.
but how about if i got more than 2 labels?

 
another question trojan.
I have another post in here about distance between 2 points.
Do u think using ur code can be easy to do that calculation?.
I gonna give it a try but i need ur opinion.
 
Answer 1) How many labels do you have? What do you need the program to do in each case?
Answer 2) I have not been following that other thread at all. The simple answer is to try it. You never know, it may work and you might learn something too!

:)



Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top