shell脚本:如何非对称地合并2个文件

时间:2014-05-26 03:08:18

标签: shell merge

我需要合并两个文件才能创建报告摘要,如下例所示:

文件1 包含工作流程名称:

Workflow_name.log
---------------------------------------------
workflow Wf_s_m_DAI_IFDS_Account_Stage
workflow Wf_s_m_DAI_IFDS_Txn_Map

文件2 包含匹配的工作流程运行摘要 - 也就是说,第n个摘要块对应于名称文件中的第n行:

*************** Summary ***************
Objects provided for validation: 10
Objects skipped: 7
Objects that were invalid before the validation: 0
Objects successfully validated: 3
Objects that are still invalid after the validation: 0
Validated objects that were Saved/Checked in: 0
Cannot save objects due to lock conflict: 0


*************** Summary ***************
Objects provided for validation: 14
Objects skipped: 11
Objects that were invalid before the validation: 0
Objects successfully validated: 3
Objects that are still invalid after the validation: 0
Validated objects that were Saved/Checked in: 0
Cannot save objects due to lock conflict: 0
validate completed successfully.

合并文件后的预期输出:

workflow Wf_s_m_DAI_IFDS_Account_Stage
*************** Summary ***************
Objects provided for validation: 10
Objects skipped: 7
Objects that were invalid before the validation: 0
Objects successfully validated: 3
Objects that are still invalid after the validation: 0
Validated objects that were Saved/Checked in: 0
Cannot save objects due to lock conflict: 0

workflow Wf_s_m_DAI_IFDS_Txn_Map
*************** Summary ***************
Objects provided for validation: 14
Objects skipped: 11
Objects that were invalid before the validation: 0
Objects successfully validated: 3
Objects that are still invalid after the validation: 0
Validated objects that were Saved/Checked in: 0
Cannot save objects due to lock conflict: 0
validate completed successfully.

请让我知道获得所需输出的正确方法。

提前致谢。

Ahshan

1 个答案:

答案 0 :(得分:0)

awk解决方案getline

我们假设文件f1包含工作流程名称和f2摘要:

awk -v namesFile=f1 '
  $0 == "*************** Summary ***************" {
    getline name < namesFile  # read next name from names file
    print name                # print name
  }
  1                           # print each input line
  ' f2
  • 开头{必须在SAME行上作为模式($0 == "..."),以便将块与其关联。
  • 1{print}的(有效)简写;即它只是输出线。

请注意,没有错误处理 - 名称数量应与摘要块的数量相匹配。