Bash 从输入文件中回显附加数据

Bash 从输入文件中回显附加数据

我目前有数千个 .jdx 文件,我设法找到一种方法将文件中需要的行回显到新文件中。我似乎无法做的是获取需要回显的额外数据。

这就是我希望的最终结果。

---
title: '1,2-Pentadiene'
visible: true
---

* DATA TYPE=MASS SPECTRUM
* CAS REGISTRY NO=591-95-7
* MOLFORM=C5H8
* MW=137.3

Download JDX: [10574-36-4-Mass16b9.jdx](10574-36-4-Mass16b9.jdx)

这是正在读取的文件..

##TITLE=1,2-Pentadiene
##JCAMP-DX=4.24
##DATA TYPE=UV/VIS SPECTRUM
##ORIGIN=INSTITUTE OF ENERGY PROBLEMS OF CHEMICAL PHYSICS, RAS
##CAS REGISTRY NO=591-95-7
##MOLFORM=C5H8
##MP=-137.3
##BP=44.9
##SOURCE REFERENCE=RAS UV No. 816
##$NIST SQUIB=1955JON/TAY228-237
##$NIST SOURCE=TSGMTE
##SPECTROMETER/DATA SYSTEM=Beckman DU
##XUNITS=Wavelength (nm)
##YUNITS=Logarithm epsilon
##XFACTOR=1.000000
##YFACTOR=1.000000
##FIRSTX=170.0000
##LASTX=199.0000
##FIRSTY=3.2
##MAXX=199
##MINX=170
##MAXY=3.38
##MINY=2.02
##NPOINTS=30
##$REF AUTHOR=Jones, L.C., Jr.; Taylor, L.W.
##$REF TITLE=Far ultraviolet absorption spectra of unsaturated and aromatic hydrocarbons
##$REF JOURNAL=Anal. Chem.
##$REF VOLUME=27
##$REF NUMBER=2
##$REF PAGE=228-237
##$REF DATE=1955
##XYPOINTS=(XY..XY)
170.0000,3.200002
171.0000,3.190000
172.0000,3.190000
173.0000,3.200000
174.0000,3.240000
175.0000,3.290000
176.0000,3.340000
177.0000,3.380000
178.0000,3.370000
179.0000,3.320000
180.0000,3.320001
181.0000,3.340000
182.0000,3.130000
183.0000,2.940000
184.0000,2.840000
185.0000,2.760000
186.0000,2.700000
187.0000,2.660000
188.0000,2.620000
189.0000,2.570000
190.0000,2.500003
191.0000,2.440000
192.0000,2.370000
193.0000,2.310000
194.0000,2.250000
195.0000,2.220000
196.0000,2.190000
197.0000,2.150000
198.0000,2.080000
199.0000,2.020000
##END=

到目前为止我想出的批处理文件是这个。

#!/bin/bash
while read -r line; do
echo \r --- > new.txt;
 echo \r visible: true >> new.txt;
        if [[ $line =~ TITLE ]] ; then echo \ title: $$line > new.txt; fi
        if [[ $line =~ DATA ]] ; then echo \ $$line >> new.txt; fi
        if [[ $line =~ CAS ]] ; then echo \ $$line >> new.txt; fi
        if [[ $line =~ MOLFORM ]] ; then echo \ $$line >> new.txt; fi
        if [[ $line =~ MW ]] ; then echo \ $$line >> new.txt; fi
 done<*.jdx
yourfilenames=`ls *.jdx`
for eachfile in $yourfilenames
do
   echo \ \($eachfile\)\[$eachfile\] >> new.txt; done

现在我似乎无法让它发挥作用。在我变得更加灰心之前,有没有人能帮助我?

谢谢

答案1

现在,我似乎无法让它发挥作用。

echo \r它并没有像你想象的那样发挥作用。

使用以下方法检查代码ShellCheck – shell脚本分析工具并修复错误/警告:

$ shellcheck myscript

Line 3:
echo \r --- > new.txt;
     ^-- SC1012: \r is just literal 'r' here. For carriage return, use "$(printf "\r")" instead.

Line 4:
 echo \r visible: true >> new.txt;
      ^-- SC1012: \r is just literal 'r' here. For carriage return, use "$(printf "\r")" instead.

Line 11:
yourfilenames=`ls *.jdx`
              ^-- SC2006: Use $(..) instead of legacy `..`.
                  ^-- SC2035: Use ./*glob* or -- *glob* so names with dashes won't become options.

Line 14:
   echo \ \($eachfile\)\[$eachfile\] >> new.txt; done
            ^-- SC2086: Double quote to prevent globbing and word splitting.
                         ^-- SC2086: Double quote to prevent globbing and word splitting.

$ 

答案2

好的。终于搞定了……

抱歉,我不知道如何使用这个地方。

#!/bin/bash
echo --- > new.txt;
 echo visible: true >> new.txt &&
while read -r line; do
        if [[ $line =~ TITLE ]] ; then echo \ title: \'$line\' >> new.txt; fi
        if [[ $line =~ DATA ]] ; then echo \ --- >> new.txt; fi
        if [[ $line =~ DATA ]] ; then echo \ $line >> new.txt; fi
        if [[ $line =~ CAS ]] ; then echo \ $line >> new.txt; fi
        if [[ $line =~ MOLFORM ]] ; then echo \ $line >> new.txt; fi
        if [[ $line =~ MW ]] ; then echo \ $line >> new.txt; fi
 done<*.jdx
echo -en '\n' >> new.txt
echo -en '\n' >> new.txt

yourfilenames=`ls *.jdx`
for eachfile in $yourfilenames
do
   echo Download JDX \ \($eachfile\)\[$eachfile\] >> new.txt; done
        sed -i 's/##DATA TYPE/ * DATA TYPE/g' *.txt
        sed -i 's/ title: ##TITLE=/ title: /g' *.txt
        sed -i 's/##CAS/ * CAS/g' *.txt
        sed -i 's/##MOLFORM/ * MOLFORM/g' *.txt
        sed -i 's/##MW/ * MW/g' *.txt

相关内容