rm画面に印刷された「==」の右側にあるすべてのファイル

rm画面に印刷された「==」の右側にあるすべてのファイル

以下を印刷するスクリプトがあります。

dhcp-18-189-47-44:CE06_getting_new_fit myname$ ./find-duplicate-structures.sh custom_structures new_GS_calculation/selected/POSCAR_00*
../CE05-structures_recombined/enum-00135/POSCAR.ideal == new_GS_calculation/selected/POSCAR_0011 (RMSD = 1.15475827927e-06, max. displacement = 1.41428428091e-06)
../CE05-structures_recombined/enum-00146/POSCAR.ideal == new_GS_calculation/selected/POSCAR_0022 (RMSD = 1.16051714442e-06, max. displacement = 1.42835572031e-06)
../CE05-structures_recombined/enum-00150/POSCAR.ideal == new_GS_calculation/selected/POSCAR_0027 (RMSD = 3.40556388834e-16, max. displacement = 6.04819551804e-16)
../CE05-structures_recombined/enum-00151/POSCAR.ideal == new_GS_calculation/selected/POSCAR_0027 (RMSD = 4.01650747941e-16, max. displacement = 5.4726685759e-16)
../CE05-structures_recombined/enum-00163/POSCAR.ideal == new_GS_calculation/selected/POSCAR_0037 (RMSD = 1.99174954223e-06, max. displacement = 2.44948961046e-06)

内部的には、スクリプトは次のようになります。

dirs=$1
shift
while read dir
do
if [ -f $dir/POSCAR.ideal ]
then poscar=$dir/POSCAR.ideal
else poscar=$dir/POSCAR
fi
mg match --just-match $poscar $@
done < $dirs

exit 0

印刷はmg match --just-match $ poscar $ @行で行われます。

しかし、実際には右側の「==」まですべてを削除したいと思います。つまり:

rm new_GS_calculation/selected/POSCAR_0011
rm new_GS_calculation/selected/POSCAR_0022
rm new_GS_calculation/selected/POSCAR_0027
rm new_GS_calculation/selected/POSCAR_0037

シェルスクリプトでこのプロセスをどのように自動化できますか?ありがとうございます。

ベストアンサー1

sed次のコマンドを使用して、プログラムの出力を解析して実行できます。

$(./YOUR_PROGRAM | sed s/^.*==/rm/ | sed s/\ \(.*//)

(あなたの例では)実行されます

rm new_GS_calculation/selected/POSCAR_0011
rm new_GS_calculation/selected/POSCAR_0022
rm new_GS_calculation/selected/POSCAR_0027
rm new_GS_calculation/selected/POSCAR_0027
rm new_GS_calculation/selected/POSCAR_0037


sed次の構文を使用します。

sed s/string1/string2/

string1に交換してくださいstring2

ここで何が起こるかは次のとおりです。

  1. プログラムの出力は、sed最初のパイプを介して次に送信されます。|
  2. 出力の各行について、sed最初(表示^)から記号までの==すべての内容が文字に置き換えられますrmsed2番目のパイプを使用して、残りの回線を別の呼び出しに送信します。
  3. sed次に、空白と開いている括弧(でマーク)\ \(とそれに続く括弧(で示す)を見つけて削除します.*
  4. bash実行が出力されるようにコマンド全体がラップされます$(...)(この場合はコマンド文字列)rm new_GS_calculation/...

おすすめ記事