使用标题值将列添加到文本文件(文件中的多个标题) [英] Add Column to text file using Heading value ( mutliple Heading in the file )
本文介绍了使用标题值将列添加到文本文件(文件中的多个标题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
今天早晨一直在搜寻各种解决方案.找不到足够接近的东西让我继续咀嚼. (我是Sys Admin,但不是脚本的深入开发者,但熟悉awk/sed/等)
Have been searching through various solns this morning. Finding nothing close enough for me to chew on. ( Im a Sys Admin but not a profound developer of scripts but am familiar with awk/sed/ etc )
我的首选是Bash甚至perl
My preference is Bash or even perl
我有这样的数据:(数据是虚拟的)
I have data like this : ( data is dummy )
====================================================================================
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
I would imagine its straight forward.. but would appreciate pointers :)P
谢谢!
推荐答案
使用awk,您可以执行以下操作:
Using awk you can do:
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
这篇关于使用标题值将列添加到文本文件(文件中的多个标题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文