シェルスクリプトを使用してファイルを読み込み、プロジェクト名に基づいて2つの新しいファイルを作成します。

シェルスクリプトを使用してファイルを読み込み、プロジェクト名に基づいて2つの新しいファイルを作成します。

私は次の文書を持っています: -

====== 20160606:034441 ====== Mango(Test)
TestName     MangoT
Row  0
Season N
Name Safeda
Location    Delhi

====== 20160606:034441 ====== Mango(Result)
TestName     MangoR
Result  0
No_of_Mango 13
Quantity    2
Quantity    3
Quantity    6
Quantity    0
Quantity    1
Quantity    9
Quantity    54
Quantity    2
Quantity    4
Quantity    6
Quantity    76
Quantity    0
Quantity    99
Price   50
Price   70
Price   40
Price   30
Price   40
Price   30
Price   20
Price   60
Price   70
Price   80
Price   90
Price   30
Price   30


====== 20160606:034441 ====== Mango(Test)
TestName     MangoT
Row  0
Season N
Name Alphonso
Location    Mumbai


====== 20160606:034441 ====== Mango(Result)
TestName     MangoR
Result  0
No_of_Mango 13
Quantity    5
Quantity    3
Quantity    1
Quantity    0
Quantity    7
Quantity    8
Quantity    70
Quantity    3
Quantity    23
Quantity    43
Quantity    734
Quantity    2
Quantity    929
Price   50
Price   70
Price   40
Price   30
Price   40
Price   30
Price   20
Price   60
Price   70
Price   80
Price   90
Price   30
Price   30

これで、上記のファイルのMango名に基づいて2つの入力ファイルが必要です。たとえば、次のようになります。 - ファイル名: -safeda.txt

TestName     MangoR
Result  0
No_of_Mango 13
Quantity    2
Quantity    3
Quantity    6
Quantity    0
Quantity    1
Quantity    9
Quantity    54
Quantity    2
Quantity    4
Quantity    6
Quantity    76
Quantity    0
Quantity    99
Price   50
Price   70
Price   40
Price   30
Price   40
Price   30
Price   20
Price   60
Price   70
Price   80
Price   90
Price   30
Price   30

2番目のファイル名: -Alphonso.txt

TestName     MangoR
Result  0
No_of_Mango 13
Quantity    5
Quantity    3
Quantity    1
Quantity    0
Quantity    7
Quantity    8
Quantity    70
Quantity    3
Quantity    23
Quantity    43
Quantity    734
Quantity    2
Quantity    929
Price   50
Price   70
Price   40
Price   30
Price   40
Price   30
Price   20
Price   60
Price   70
Price   80
Price   90
Price   30
Price   30

シェルスクリプトを使用してこれら2つのファイルを生成する必要があります。

ベストアンサー1

1つの方法は次のとおりです。

$ awk '(/=====/){a=0}
       (/\(Result\)\s*$/){a=1; next} 
       ($1=="Name"){n=$2}
       (a==1){print >> n".txt"}' file 

説明する

  • (/=====/){a=0}:現在行が一致した場合に======設定されます。a0
  • (/\(Result\)\s*$/){a=1; next}: if the current line ends with(結果)1 followed by 0 or more whitespace, set`を入力しtoて次の行にジャンプします。
  • ($1=="Name"){n=$2}:最初のフィールドがある場合は、変数を2番目のフィールドの値にName設定します。n
  • (a==1){print >> n".txt"aそうであれば、拡張子(name)を持つファイルにこの行を印刷します1n.txt

おすすめ記事