【发布时间】:2021-07-29 00:52:29
【问题描述】:
我目前正在努力处理一个查询,在该查询中,我需要从具有 2 列或更多列的文件中获取数据,并且不仅要提取数据,还要根据给定的数据为每一列添加标题。这是文件数据的示例。
--
test_AA.98
test_AA.99+
8 35=Apples
16 35=Pears
test_AA.100
14 35=Apples
12 35=Pears
test_AB.101-
15 35=Banannas
-- 如果我 cat 和 awk 第一列 - cat testfile | awk '{ print $1 }' - 结果
test_AA.98
test_AA.99+
8
16
test_AA.100
14
12
test_AB.101-
15
-- 如果我 cat 和 wak 第二列 - cat testfile | awk '{ print $2 }' - 结果
35=Apples
35=Pears
35=Apples
35=Oranges
35=Banannas
-- 谁能提供一个解决方案,我可以根据模式匹配(test*)将标题标题添加到我的数据到 $1 位置,匹配 35=*,移动到 $2 位置,同时保持计数和值在地方?我想要的结果。
Suppliers, Red=35=Apples, Green=35=Pears, Yellow=35=Banannas
test_AA.98, 0, 0, 0
test_AA.99+, 8, 16, 0
test_AA.100, 14, 12, 0
test_AB.101-, 0, 0, 15
我只找到了一些添加标题的解决方案,但还没有想办法在标题下放置正确的值。
sed '1iSuppliers, Red=35=Apples, Green=35=Pears, Yellow=35=Banannas' testfile
Suppliers, 35=Apples, 35=Pears, 35=Banannas
test_AA.98
test_AA.99+
8 35=Apples
16 35=Pears
test_AA.100
14 35=Apples
12 35=Oranges
test_AB.101-
15 35=Banannas
非常感谢!!
【问题讨论】:
标签: awk sed formatting counting