【发布时间】:2018-02-02 18:18:27
【问题描述】:
我有以下格式的数据
Room : L1,
Name : Test,
DOB : 2017-10-12,
Section : ABC,
Name : BTest2,
Status : APPROVE,
DOB : 2017-10-12,
Rule : forward,
Comment : Testing comment,
Room : L2,
Name : Test2,
DOB : 2017-10-11,
Rule : Backward,
我正在尝试以以下格式获取输出
Room : L1,Name : Test,DOB : 2017-10-12,Section : ABC,
Room : L1,Name : BTest2,Status : APPROVE,DOB : 2017-10-12,Rule : forward,Comment : Testing comment,
Room : L2,Name : Test2,DOB : 2017-10-11,Rule : Backward,
我尝试使用 awk 和 sed 来格式化数据,但由于模式不是很一致,我无法这样做。无论如何,是否仅基于 Name 的出现来做到这一点。在下面的示例中,我同时使用了 Name 和 Comment,因此它失败了。
awk '/^Room/{s=$0} /^Name/ && match(p,/^Comment/){$0=sprintf("%s%s%s",s,ORS,$0)} {print;p=$0}' test.txt | sed ':a;/Comment/!{N;s/\n//g;ba}'
非常感谢您的帮助。
【问题讨论】:
-
一组较小的样本数据可能会简化您的问题描述。您是否试图将所有“字段”放在一条线上?请更新您的 Q,以便您的需求更清晰。祝你好运。
-
嗨。我不想将所有字段合并到一行中。但是将 Room 与两次出现 Name 之间的所有记录合并。这是我能做的最小的数据集。
-
@user2854333,但
DOB键在Room: L1中也出现了两次 - 考虑更新您的输出 -
-
@user2854333,详细说明你的逻辑:应该只将
Name键视为分离的原因吗?