简体   繁体   English

读取Bash脚本文件,bash删除空格

[英]Bash script file read, bash deletes spaces

This questions is a follow up to my previous question about reading certain lines of a file. 这个问题是我之前关于读取文件某些行的问题的跟进。 I read my file as follows: 我按如下方式读取文件:

while read line 
do
    if [ "$line" == "SUMMARY OF POLARIZATION CALCULATION" ]; then
        for i in {1..9}
        do
            read line
            echo $line >> ../../Summary.out
        done
        break
    fi
done < ../Tutorial1_1.out

Which looks like this: 看起来像这样:

.
.
.
 O  (0.0000, 0.5000, 0.4834)  sp(1)  6.00 [ 0.000000e+00,  1.884956e+01,  1.822516e+01]
---------------------------------------------------------------------------------------
Total ionic phase (rad)       sp(1)       [ 7.539822e+01,  7.539822e+01,  1.119960e+02]
Total ionic phase wrap. (rad) sp(1)       [ 8.633172e-11,  8.633172e-11, -1.101384e+00]
Ionic polarization (C/m2)     sp(1)       [ 1.365657e-11,  1.365657e-11, -1.760570e-01]
=======================================================================================

SUMMARY OF POLARIZATION CALCULATION
=======================================================================================
Value                           |  spin   |    dir(1)    |    dir(2)    |    dir(3)
---------------------------------------------------------------------------------------
Electronic polarization (C/m2)     sp(1)  [-8.783054e-12, -8.828765e-13,  4.803326e-01]
Ionic polarization (C/m2)          sp(1)  [ 1.365657e-11,  1.365657e-11, -1.760570e-01]
Tot. spin polariz.=Pion+Pel (C/m2) sp(1)  [ 4.873517e-12,  1.277369e-11,  3.042756e-01]
---------------------------------------------------------------------------------------
TOTAL POLARIZATION (C/m2)          both   [ 4.873517e-12,  1.277369e-11,  3.042756e-01]
=======================================================================================

Completed using BerryPI version: +++Version 1.2 (Mar 12, 2014)
...

So the code above just take the 9 lines after the "SUMMARY.." and writes to Summary.out file. 因此,上面的代码仅在“ SUMMARY ..”之后采用9行,并写入Summary.out文件。 However, when it writes it screws up all the spacing, so i get the following: 但是,当它写时,它弄乱了所有间距,所以我得到以下信息:

=======================================================================================
Value | spin | dir(1) | dir(2) | dir(3)
---------------------------------------------------------------------------------------
Electronic polarization (C/m2) sp(1) [-8.783054e-12, -8.828765e-13, 4.803326e-01]
Ionic polarization (C/m2) sp(1) [ 1.365657e-11, 1.365657e-11, -1.760570e-01]
Tot. spin polariz.=Pion+Pel (C/m2) sp(1) [ 4.873517e-12, 1.277369e-11, 3.042756e-01]
---------------------------------------------------------------------------------------
TOTAL POLARIZATION (C/m2) both [ 4.873517e-12, 1.277369e-11, 3.042756e-01]
=======================================================================================

How can i fix that, is there a flag that would read and write a string exactly as it is in the input file? 我该如何解决,是否有一个标志可以完全按照输入文件中的内容读写字符串?

I suggest to use 我建议使用

echo "$line" >> ../../Summary.out

instead of 代替

echo $line >> ../../Summary.out

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM