如何用另一个文本文件(OSX Sierra,bash)中的数字替换文本文件中的矩阵条目 [英] How to replace a matrix entry in a text file with a number from another text file (OSX Sierra, bash)

查看:114
本文介绍了如何用另一个文本文件(OSX Sierra,bash)中的数字替换文本文件中的矩阵条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用行号替换文本文件中的整行

上面链接中的问题询问如何替换文本文件中的一行.作为Mac用户,nakeer(从底部开始的第二个答案)提供了一个很好的答案:

The question at the link above asks how to replace a line in a text file. nakeer (second answer from bottom) has provided an answer that works well for me as a Mac user:

sed -i '' -e 's/text-on-line-to-be-changed.*/text-to-replace-the=whole-line/' file-name

但是,我无法弄清楚如何针对我的特定情况对其进行修改.如果有人能指出正确的方向,我将不胜感激.

However, I cannot figure out how to modify it for my particular situation. If anyone can point me in the right direction, I would be very grateful.

  1. 我有很多文件.每个文件都包含此矩阵,如下所示:

  1. I have many files. Each file contains this matrix, exactly as appears below:

12.345678    0.000000    0.000000    
 0.000000   12.345678    0.000000    
 0.000000    0.000000   12.345678

  • 我还有一个其他文件,其中包含一列数字,例如:

  • I have one additional file that contains a column of numbers, like:

    87.654321   
    18.765432    
    21.876543
    ...
    

  • 我想从(2)中的每一行取一个数字.我想用它来替换(1)中一个矩阵的非零值(保留零值).因此,第一个文件中的第一个矩阵应如下所示:

  • I want to take one number from each line of the column in (2). I want to use it to replace the non-zero values of one matrix in (1) (preserving the zero values). So the first matrix in the first file should look like:

    87.654321    0.000000    0.000000    
     0.000000   87.654321    0.000000    
     0.000000    0.000000   87.654321
    

  • 第二个文件中的第二个矩阵的非零值应使用"18.765432".

    The second matrix in the second file should use "18.765432" for its non-zero values.

    1. 我还没有bash脚本方面的经验,但是到目前为止(我在(1)中的ic是我的文件,其中包含原始矩阵,然后将其复制到新目录中,可以在其中进行更改(3)的矩阵):

    1. I'm not experienced as far as bash scripting, but I so far have (where ic is my file in (1) that contains the original matrix and I copy it into a new directory where I can change that matrix to (3)):

    #!/bin/bash
    
    let timesteps=20000
    for ((step=0; step <= timesteps ; step++))
    do
      mkdir $step/results
      cp ic $step/ic
    
      cat X >> X # <--Here I'd like to modify nakeer's expression. Any hints would be much appreciated.
    


    更新:


    Update:

    1. 我设法建立并运行了Ed的非常清晰的解决方案.但是,有一个问题.包含矩阵的文件(请参阅上面的(1))也包含其他数据.例如(在执行Ed的代码之前):

        12.345678    0.000000    0.000000    
         0.000000   12.345678    0.000000    
         0.000000    0.000000   12.345678
       0.5   
       abc.xyx 
       90
       900
       0.125
       90
       6
    

    Ed的代码成功将矩阵中的12.345678更改为新值.但是,矩阵下方的数字列表中的0.125也更改为该新值.我不想更改0.125.

    Ed's code successfully changes 12.345678 in the matrix to a new value. However, 0.125 in the list of numbers below the matrix is also changed to that new value. I do not want 0.125 to be changed.

    Ed代码似乎使用数字格式来标识要更改的数字,并且0.125似乎属于应更改的数字类别.如果有人对如何从更改中排除0.125有任何想法,我将不胜感激!

    Ed's code following match seems to use the format of numbers to identify which numbers to change and it looks like 0.125 falls into the category of numbers that should be changed. If anyone has any ideas about how to exclude 0.125 from the change, I'd be grateful to know!

    1. 在每个矩阵文件都位于其自己的目录中的情况下,如何修改Ed的代码;例如0/file0, 1/file1, 2/file2等?

    推荐答案

    $ ls
    file1  file2  file3  numbers  tst.awk
    

    .

    $ cat tst.awk
    NR==FNR { a[NR]=$1; next }
    
    FNR==1 {
        close(out)
        out = FILENAME ".new"
        fileNr++
    }
    
    match($0,/[0-9.]+[1-9][0-9.]+/) {
        $0 = substr($0,1,RSTART-1) a[fileNr] substr($0,RSTART+RLENGTH)
    }
    
    { print > out }
    

    .

    $ tail -n +1 numbers file*
    ==> numbers <==
    87.654321
    18.765432
    21.876543
    
    ==> file1 <==
    12.345678    0.000000    0.000000
     0.000000   12.345678    0.000000
     0.000000    0.000000   12.345678
    
    ==> file2 <==
    12.345678    0.000000    0.000000
     0.000000   12.345678    0.000000
     0.000000    0.000000   12.345678
    
    ==> file3 <==
    12.345678    0.000000    0.000000
     0.000000   12.345678    0.000000
     0.000000    0.000000   12.345678
    

    .

    $ awk -f tst.awk numbers file1 file2 file3
    

    .

    $ ls
    file1  file1.new  file2  file2.new  file3  file3.new  numbers  tst.awk
    

    .

    $ tail -n +1 file*.new
    ==> file1.new <==
    87.654321    0.000000    0.000000
     0.000000   87.654321    0.000000
     0.000000    0.000000   87.654321
    
    ==> file2.new <==
    18.765432    0.000000    0.000000
     0.000000   18.765432    0.000000
     0.000000    0.000000   18.765432
    
    ==> file3.new <==
    21.876543    0.000000    0.000000
     0.000000   21.876543    0.000000
     0.000000    0.000000   21.876543
    

    如果安装GNU awk,则可以使用-i inplace进行就地"编辑,而不是创建新的输出文件(如果这对您有用).

    If you install GNU awk then you can use -i inplace to do "inplace" editing instead of creating new output files if that's useful to you.

    这篇关于如何用另一个文本文件(OSX Sierra,bash)中的数字替换文本文件中的矩阵条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆