私はいくつかのファイル(.ts)に特定の文字列をチェックし、一致する行を結果ファイルに出力するbashファイルの中でforループを実行しています。ここで結果ファイル内のFORループ内の処理ファイル名を印刷する - BASH
はコードです:
#! /bin/bash
for file in *.ts;
do awk -f test_function.awk $file > result.txt;
done
そして、これはtest_function.awk
ファイルです:
match($0, /<name>(.*)<\/name>/,n){ nm=n[1] }
match($0, /<source>(.*)<\/source>/,s){ src=s[1] }
/unfinished/{ print "name: " nm, "source: " src }
そして、これは「未完」が含まれる入力ファイルの一つであり、中に含まれる必要があります出力:
<context>
<name>AccuCapacityApp</name>
<message>
<source>Capacity</source>
<translation type="unfinished">Kapazität</translation>
</message>
<message>
<source>Charge Level</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Sel (Yes)</source>
<translation type="unfinished">Sel (Ja)</translation>
</message>
<message>
<source>Esc (No)</source>
<translation type="unfinished">Esc (Nein)</translation>
</message>
</context>
出力結果は次のようになります。
name: AccuCapacityApp source: Capacity
name: AccuCapacityApp source: Charge Level
name: AccuCapacityApp source: Sel (Yes)
そして、これは「未完」が含まれていない入力ファイルの一つであり、出力から除外する必要があります
<context>
<name>ATM FSM state</name>
<message>
<source>Hunting</source>
<translation>Sync-Suche</translation>
</message>
<message>
<source>Pre-Sync</source>
<translation>Pre-Sync</translation>
</message>
<message>
<source>Sync</source>
<translation>Sync</translation>
</message>
</context>
私は何をしたい処理ファイルを印刷することです
Processign file: alpha.txt
name: AccuCapacityApp source: Capacity
name: AccuCapacityApp source: Charge Level
name: AccuCapacityApp source: Sel (Yes)
Processing file: gamma.txt
name: AccuCapacityApp source: Capacity
name: AccuCapacityApp source: Charge Level
name: AccuCapacityApp source: Sel (Yes)
どのように私はこれを達成することができます:以下のように、一致する文字列が検出された場合にのみ、結果ファイルにマッチした行の各paragrapghの初めに名前を付けますか?
私は、ファイル名を追加することができますし、一致する行を結果ファイルに追加できることを知っています。しかし、私はbashファイルを実行するたびにブランクの結果ファイルを作成し、一致する文字列が見つかったときにのみファイル名と内容を書きます。だから私は、ファイル名を追加すると動作しないと思う。 echo ${file##*/}
、echo $file
、{print FILENAME};{print "\t" $0}
というファイル名で印刷しようとしましたが、必要に応じて印刷できませんでした。あなたの更新に基づいて
$(ls * .ts);でファイルが見つかった場所はわかりませんが、完全な反パターンです。 * .ts;の 'for file 'を使用し、ループ内で変数を引用します。 –
ありがとうございます。私は自分のコードを更新しました。 – John