BASH 文本编辑

BASH 文本编辑

目前正在做一个项目,需要编辑一堆几乎相同的位置的文本。

基本上,我需要把这个

30
SCF Done
Ti   52.3941   48.0198   49.5622
Ti   49.4797   49.061   50.7034
Ti   46.7489   51.0193   48.6784
Ti   49.2209   52.9008   48.3155
Ti   49.7834   46.7277   48.4888
Ti   52.6251   47.6201   52.5869
Ti   50.3918   52.7865   51.1559
Ti   47.6514   51.5936   51.7062
Ti   52.6228   50.7423   51.3614
Ti   49.082   49.5289   47.4413
O   50.6524   50.7772   50.9831
O   49.7273   51.3294   47.4124
O   46.2227   51.3762   50.4682
O   51.1925   48.1984   51.2227
O   53.004   49.4764   52.6582
O   53.4681   47.2549   50.9251
O   49.01   52.6769   52.4943
O   48.6199   52.3487   50.1333
O   49.0692   47.6549   47.0357
O   51.6141   46.4856   48.7282
O   48.8938   47.2992   50.0114
O   48.317   49.9127   49.2982
O   52.4147   46.508   53.9714
O   50.5076   53.6822   49.4331
O   53.0515   49.783   49.8138
O   50.5672   48.7353   48.7478
O   47.3746   52.6644   47.9578
O   52.2093   52.5518   51.6378
O   47.2328   50.0047   47.1441
O   48.2864   49.8647   51.9765

变成这样:

SCF DONE
1.0
15.0 0 0
0 15.0 0
0 0 15.0
Ti O
1 2 
Direct
52.3941   48.0198   49.5622
49.4797   49.061   50.7034
46.7489   51.0193   48.6784
49.2209   52.9008   48.3155
49.7834   46.7277   48.4888
52.6251   47.6201   52.5869
50.3918   52.7865   51.1559
47.6514   51.5936   51.7062
52.6228   50.7423   51.3614
49.082   49.5289   47.4413
50.6524   50.7772   50.9831
49.7273   51.3294   47.4124
46.2227   51.3762   50.4682
51.1925   48.1984   51.2227
53.004   49.4764   52.6582
53.4681   47.2549   50.9251
49.01   52.6769   52.4943
48.6199   52.3487   50.1333
49.0692   47.6549   47.0357
51.6141   46.4856   48.7282
48.8938   47.2992   50.0114
48.317   49.9127   49.2982
52.4147   46.508   53.9714
50.5076   53.6822   49.4331
53.0515   49.783   49.8138
50.5672   48.7353   48.7478
47.3746   52.6644   47.9578
52.2093   52.5518   51.6378
47.2328   50.0047   47.1441
48.2864   49.8647   51.9765

编辑;更加清晰

但就我一生而言,我无法编写一个可以实现这一目标的脚本。

任何帮助都值得感激,我将在 Mac 上的 Bash 中编写代码,但在 Linux 机器上运行它。

答案1

根据您的要求,您需要类似这个带有输出文件的单行程序:

(echo -e "SCF DONE\n1.0\n15.0 0 0\n0 15.0 0\n0 0 15.0\nTi O\n1 2\nDirect"; awk '/Ti|O/ {print $2 " " $3 " " $4}' foobar) > output_file

且不包含:

echo -e "SCF DONE\n1.0\n15.0 0 0\n0 15.0 0\n0 0 15.0\nTi O\n1 2\nDirect"; awk '/Ti|O/ {print $2 " " $3 " " $4}' foobar

foo或者创建一个包含以下内容的脚本:

#!/bin/bash
echo "SCF DONE"
echo "1.0"
echo "15.0 0 0"
echo "0 15.0 0"
echo "0 0 15.0"
echo "Ti O"
echo "1 2"
echo "Direct"
awk '/Ti|O/ {print $2 " " $3 " " $4}' "$1"

例子

您的输入文件foobar

30
SCF Done
Ti   52.3941   48.0198   49.5622
Ti   49.4797   49.061   50.7034
Ti   46.7489   51.0193   48.6784
Ti   49.2209   52.9008   48.3155
Ti   49.7834   46.7277   48.4888
Ti   52.6251   47.6201   52.5869
Ti   50.3918   52.7865   51.1559
Ti   47.6514   51.5936   51.7062
Ti   52.6228   50.7423   51.3614
Ti   49.082   49.5289   47.4413
O   50.6524   50.7772   50.9831
O   49.7273   51.3294   47.4124
O   46.2227   51.3762   50.4682
O   51.1925   48.1984   51.2227
O   53.004   49.4764   52.6582
O   53.4681   47.2549   50.9251
O   49.01   52.6769   52.4943
O   48.6199   52.3487   50.1333
O   49.0692   47.6549   47.0357
O   51.6141   46.4856   48.7282
O   48.8938   47.2992   50.0114
O   48.317   49.9127   49.2982
O   52.4147   46.508   53.9714
O   50.5076   53.6822   49.4331
O   53.0515   49.783   49.8138
O   50.5672   48.7353   48.7478
O   47.3746   52.6644   47.9578
O   52.2093   52.5518   51.6378
O   47.2328   50.0047   47.1441
O   48.2864   49.8647   51.9765

输出./foo foobar

SCF DONE
1.0
15.0 0 0
0 15.0 0
0 0 15.0
Ti O
1 2
Direct
52.3941 48.0198 49.5622
49.4797 49.061 50.7034
46.7489 51.0193 48.6784
49.2209 52.9008 48.3155
49.7834 46.7277 48.4888
52.6251 47.6201 52.5869
50.3918 52.7865 51.1559
47.6514 51.5936 51.7062
52.6228 50.7423 51.3614
49.082 49.5289 47.4413
50.6524 50.7772 50.9831
49.7273 51.3294 47.4124
46.2227 51.3762 50.4682
51.1925 48.1984 51.2227
53.004 49.4764 52.6582
53.4681 47.2549 50.9251
49.01 52.6769 52.4943
48.6199 52.3487 50.1333
49.0692 47.6549 47.0357
51.6141 46.4856 48.7282
48.8938 47.2992 50.0114
48.317 49.9127 49.2982
52.4147 46.508 53.9714
50.5076 53.6822 49.4331
53.0515 49.783 49.8138
50.5672 48.7353 48.7478
47.3746 52.6644 47.9578
52.2093 52.5518 51.6378
47.2328 50.0047 47.1441
48.2864 49.8647 51.9765

或者在输出文件中进行重定向:

./foo foobar > output_file

答案2

Awk 一行代码:

awk 'BEGIN {print "SCR Done\n1.0\n15.0 0 0\n0 15.0 0\n0 0 15.0\nTi O\n1 2\nDirect\n\n"} NR > 1 {$1="";print } ' testfile

在此处输入图片描述

相关内容