简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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