替换数据文件中的值

时间:2014-07-28 20:08:13

标签: linux fortran octave scilab

我试图替换文件中的现有数据值。该文件用于通过FEM代码计算运动材料硬化。每个元素的文本都是这样的。我有几个要修改的元素:


13499         8        10         1         1                   0         0
1.500360e+08    1.451964e+07    3.728601e+08   -49529.937500   -1.930966e+07
4.499323e+06    4.427284e-03    2.107339e+06   -5.031084e+06    2.923746e+06
15250.131836   -1.833509e+06    1.992212e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
1.377865e+08    2.413883e+07    3.754904e+08    7.427730e+05   -2.500246e+07
2.964798e+06    4.422367e-03    1.855410e+06   -4.713138e+06    2.857730e+06
13224.164062   -2.054056e+06    1.624522e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
1.373486e+08    2.433924e+07    3.757281e+08   -4.595526e+06   -2.536434e+07
1.499050e+06    4.435852e-03    1.867437e+06   -4.746126e+06    2.878688e+06
-1.195839e+05   -2.081042e+06    1.472030e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
1.495412e+08    1.476622e+07    3.731083e+08   -5.636892e+06   -1.967031e+07
3.060086e+06    4.440636e-03    2.109059e+06   -5.047602e+06    2.938542e+06
-1.202835e+05   -1.849329e+06    1.847072e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
1.586573e+08    1.004740e+07    3.687111e+08   -6.710480e+05   -2.547207e+07
6.056666e+06    3.514634e-03    1.647607e+06   -3.689882e+06    2.042273e+06
  417.019592   -1.201436e+06    1.671364e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
1.461364e+08    2.007670e+07    3.712027e+08    66494.984375   -3.248405e+07
4.652704e+06    3.476992e-03    1.358743e+06   -3.303262e+06    1.944519e+06
 1658.200684   -1.316609e+06    1.339788e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
1.457837e+08    2.024077e+07    3.713914e+08   -5.707908e+06   -3.274767e+07
3.163598e+06    3.490632e-03    1.395223e+06   -3.361252e+06    1.966029e+06
-1.027537e+05   -1.351428e+06    1.227283e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
1.581765e+08    1.031220e+07    3.689273e+08   -6.650824e+06   -2.571731e+07
4.596290e+06    3.530744e-03    1.661256e+06   -3.721140e+06    2.059885e+06
-1.082121e+05   -1.219397e+06    1.555895e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000

我想通过使用一些智能命令,脚本等将所有元素的文本更改为此文件。我使用的是Linux,但如果推荐使用Windows 7或Mac OS X也可以使用。我知道FORTRAN,Scilab和Octave的基础知识,但欢迎在这些环境之外的解决方案。


13499         8        10         1         1                   0         0
       0.000           0.000           0.000           0.000           0.000
       0.000           0.000    2.107339e+06   -5.031084e+06    2.923746e+06
15250.131836   -1.833509e+06    1.992212e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
       0.000           0.000           0.000           0.000           0.000
       0.000           0.000    1.855410e+06   -4.713138e+06    2.857730e+06
13224.164062   -2.054056e+06    1.624522e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
       0.000           0.000           0.000           0.000           0.000
       0.000           0.000    1.867437e+06   -4.746126e+06    2.878688e+06
-1.195839e+05   -2.081042e+06    1.472030e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
       0.000           0.000           0.000           0.000           0.000
       0.000           0.000   2.109059e+06   -5.047602e+06    2.938542e+06
-1.202835e+05   -1.849329e+06    1.847072e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
       0.000           0.000           0.000           0.000           0.000
       0.000           0.000   1.647607e+06   -3.689882e+06    2.042273e+06
  417.019592   -1.201436e+06    1.671364e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
       0.000           0.000           0.000           0.000           0.000
       0.000           0.000    1.358743e+06   -3.303262e+06    1.944519e+06
 1658.200684   -1.316609e+06    1.339788e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
       0.000           0.000           0.000           0.000           0.000
       0.000           0.000    1.395223e+06   -3.361252e+06    1.966029e+06
-1.027537e+05   -1.351428e+06    1.227283e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000
       0.000           0.000           0.000           0.000           0.000
       0.000           0.000    1.661256e+06   -3.721140e+06    2.059885e+06
-1.082121e+05   -1.219397e+06    1.555895e+05       20.000000           0.000
    1.000000    1.009244e-10           0.000           0.000           0.000

请告知。

2 个答案:

答案 0 :(得分:2)

这是MATLAB / Octave中的解决方案。首先,我们将使用textread来读取实际数据。我看到你的文字数据不均匀。第一行有7个元素,而下一行有5个元素。 textread将对此进行zeropad并将数据作为单个矩阵读入。因此,第6列和第7列将具有除第一行之外的零。然后我提取出数字数据,我们只是跳过第一行,只抓取前5列。

在此之后,我循环遍历行,将第一行设置为零,将第一行和第二行设置为0的第二行,然后在此之后跳过接下来的两行。我重复这个过程,直到我们到达文本文件的末尾。我将第一列和这个新的数字数据保存到文本文件中。我将使用dlmwrite来促进这一写作。

因此,假设输入数据在testData.txt中,我将新文件写入testOut.txt,并遵循我们刚才谈到的上述规则。就这样:

%// Read in data and extract numeric data only
testData = textread('testData.txt');
numericData = testData(2:end,1:5);
count = 1;

%// From the first row until the last
while(count <= size(numericData,1))
    numericData(count,:) = 0; %// Set first row to zero
    %// Ensure we don't go out of bounds
    if (count + 1 > size(numericData,1))
        break;
    end
    numericData(count+1,1:2) = 0; %// Set first two elements of next row to zero
    count = count + 4; %// Skip over two rows
end

%// Save the file.  Write the first row first
dlmwrite('testOut.txt', testData(1,:), '\t');
%// Append on top of the file the modified numeric data
dlmwrite('testOut.txt', numericData, '-append', 'delimiter', '\t', 'precision', '%.6e');

我在这个文本文件中得到的输出是:

13499   8   10  1   1   0   0
0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    2.107339e+06    -5.031084e+06   2.923746e+06
1.525013e+04    -1.833509e+06   1.992212e+05    2.000000e+01    0.000000e+00
1.000000e+00    1.009244e-10    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    1.855410e+06    -4.713138e+06   2.857730e+06
1.322416e+04    -2.054056e+06   1.624522e+05    2.000000e+01    0.000000e+00
1.000000e+00    1.009244e-10    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    1.867437e+06    -4.746126e+06   2.878688e+06
-1.195839e+05   -2.081042e+06   1.472030e+05    2.000000e+01    0.000000e+00
1.000000e+00    1.009244e-10    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    2.109059e+06    -5.047602e+06   2.938542e+06
-1.202835e+05   -1.849329e+06   1.847072e+05    2.000000e+01    0.000000e+00
1.000000e+00    1.009244e-10    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    1.647607e+06    -3.689882e+06   2.042273e+06
4.170196e+02    -1.201436e+06   1.671364e+05    2.000000e+01    0.000000e+00
1.000000e+00    1.009244e-10    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    1.358743e+06    -3.303262e+06   1.944519e+06
1.658201e+03    -1.316609e+06   1.339788e+05    2.000000e+01    0.000000e+00
1.000000e+00    1.009244e-10    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    1.395223e+06    -3.361252e+06   1.966029e+06
-1.027537e+05   -1.351428e+06   1.227283e+05    2.000000e+01    0.000000e+00
1.000000e+00    1.009244e-10    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    0.000000e+00    1.661256e+06    -3.721140e+06   2.059885e+06
-1.082121e+05   -1.219397e+06   1.555895e+05    2.000000e+01    0.000000e+00
1.000000e+00    1.009244e-10    0.000000e+00    0.000000e+00    0.000000e+00

请注意,这是制表符分隔文件。祝你好运!

答案 1 :(得分:0)

我的问题的一个成功解决方案是由Andreas de Blanche http://www.hv.se/en/research/researchers/alphabetically/andreas-de-blanche提供的以下脚本:

#!/usr/bin/perl

use strict;

my $start = time;

my $inputName = $ARGV[0];

if($inputName)
{
   print "\n   Converting file: $inputName\n";
   print "   Output file: $inputName.out.\n\n";
}
else
{
   print "\n   Please enter input filename on commandline\n";
   print "              ./script.pl <input.txt>\n";
   print "   Output file: <input.out>.\n\n";
   exit;
}


open INFILE, "<$inputName" or die $!;
open OUTFILE, ">$inputName.out" or die $!;

my $cardRow=33; #Each card is 33 rows long.
my $count=0;
my $row=0;


while (<INFILE>)
{
#   print "$row: $_"; 

   if($_=="")
   {
   }
   elsif($cardRow==33)
   {
      #print Reset counter $count
      $count=-1;
      $cardRow=0;

      #Just print line to outfile
      print OUTFILE "$_";
   }
   elsif($count==0) 
   {
      #These rows seems to always be identical
      print OUTFILE " 0.000000000E+00 0.000000000E+00 0.000000000E+00 0.000000000E+00 0.000000000E+00\n";
   }
   elsif($count==1) 
   {
      my $fragment =  substr $_, 32;
      print OUTFILE " 0.000000000E+00 0.000000000E+00$fragment";
   }
   elsif($count==2 || $count==3) 
   {
      #These rows should not be changed
      print OUTFILE "$_";
   }
   else
   {
      print "ERROR: This wrong sized subset!\n";
   }

   $count++;
   if($count==4)
   {
      #There are 4 rows in each subset (0-3)
      $count=0;
   }
   $cardRow++;
   $row++;
}


close INFILE;
close OUTFILE;

my $duration = time - $start;
print "   Execution time: $duration s\n";