使用标题值(文件中的多个标题)将列添加到文本文件

时间:2016-04-21 10:45:29

标签: bash shell awk sed

今天早上一直在搜索各种溶液。找不到足够接近让我咀嚼的东西。 (我是一个系统管理员,但不是一个深刻的脚本开发人员,但熟悉awk / sed / etc)

我的偏好是Bash甚至是perl

我有这样的数据:(数据是假的)

====================================================================================
Person Name: Joe Soap
Code: 0
====================================================================================
Purchases:12th June 2013 - Awk for Dummies Vol1
Purchases:42th June 2013 - Awk for Dummies Vol2
Purchases:22th June 2013 - Awk for Dummies Vol3
====================================================================================

====================================================================================
Person Name: Mary Soap
Code: 0
====================================================================================
Purchases:52th June 2013 - Sed for Microsoft Lovers Vol1
Purchases:82th June 2013 - Sed for Microsoft Lovers Vol2
====================================================================================

====================================================================================
Person Name: Don Soap
Code: 0
====================================================================================
Purchases:3rd March 2013 - Self Healing

我需要得到:

Joe Soap:Purchases:12th June 2013 - Awk for Dummies Vol1
Joe Soap:Purchases:42th June 2013 - Awk for Dummies Vol2
Joe Soap:Purchases:22th June 2013 - Awk for Dummies Vol3
Mary Soap:Purchases:52th June 2013 - Sed for Microsoft Lovers Vol1
Mary Soap:Purchases:82th June 2013 - Sed for Microsoft Lovers Vol2
Don Soap:Purchases:3rd March 2013 - Self Healing

我会想象它直截了当......但是会很感激指针:)P

全部谢谢!

1 个答案:

答案 0 :(得分:2)

使用awk你可以这样做:

awk -F ': *' -v OFS=: '$1~/Person Name/{p=$2} $1~/Purchases/{print p, $0}' file

Joe Soap:Purchases:12th June 2013 - Awk for Dummies Vol1
Joe Soap:Purchases:42th June 2013 - Awk for Dummies Vol2
Joe Soap:Purchases:22th June 2013 - Awk for Dummies Vol3
Mary Soap:Purchases:52th June 2013 - Sed for Microsoft Lovers Vol1
Mary Soap:Purchases:82th June 2013 - Sed for Microsoft Lovers Vol2
Don Soap:Purchases:3rd March 2013 - Self Healing