【发布时间】:2021-03-28 13:01:28
【问题描述】:
我正在对我使用 pdfgrep 创建的一些文件进行排序,以列出我拥有的某些 PDF 的页码。它产生了以下输出:
./Buddhism in the Shadow of Brahmanism.pdf:111: Then, rising from his seat, covering one shoulder with his robe, the king
./Buddhism in the Shadow of Brahmanism.pdf:182:branch who has adopted the yellow robes of Buddhism; he is sur-
./Buddhism in the Shadow of Brahmanism.pdf:229: resolve that his body, his bowl, and his monastic robe (which had been
./Buddhism in the Shadow of Brahmanism.pdf:230:robe. In this way, Mahākāśyapa (or at least his body) is to act as a sort
./Buddhism in the Shadow of Brahmanism.pdf:230:corpse to his disciples and displays to them the Buddha’s robe, and they
./Buddhism in the Shadow of Brahmanism.pdf:230:offer him the robe that the Buddha had confided to him. Only then will
./Introduction to the History of Indian Buddhism.pdf:31:the robes of a Buddhist monk in an effort to convert them, he was Sciequia. For
./Introduction to the History of Indian Buddhism.pdf:54:monks, and in particular on retreat, robes, and chastity, p. 308.—On the life of
./Introduction to the History of Indian Buddhism.pdf:97:are the Kat.hināvadāna, which deals with the bowl, the staff, and the robes of
./Introduction to the History of Indian Buddhism.pdf:111:of a sort of robe.
./Introduction to the History of Indian Buddhism.pdf:112:cover his nakedness, and who rejects all other robes as superfluous.
./Introduction to the History of Indian Buddhism.pdf:127:noon, after having taken his robe and his bowl,
./Introduction to the History of Indian Buddhism.pdf:127:bowl and his robe, he went to the place where the Cāpāla caitya6 was located,
我想要做的是将第二列上与文件名匹配的页码组合在一起,我希望输出看起来像:
./Buddhism in the Shadow of Brahmanism.pdf:111, 182, 229, 230
./Introduction to the History of Indian Buddhism.pdf:31, 54, 97, 111, 112, 127
我尝试使用 awk 解析第一个值,然后在同一个文件上使用这些结果仅打印页码,这样我就可以 grep 结果并稍后附加到文件名之后,如下所示:
awk -F : '{print $1}' parsing_file | uniq | while read line; do awk -v number="$line" -F : '$1 == "$number" { print $2 }' parsing_file; done
但这并没有通过,我猜 uniq 和 while read 可能会被删除,也许只使用带有 awk 的数组?
我在这里看到过类似的事情:
https://unix.stackexchange.com/questions/167280/awk-group-by-and-sum-column-values
但我不想总结列上的值,而只是想将它们组合在一起。
谢谢
【问题讨论】:
-
您得到的第一个答案可能是最好的答案,也可能不是。通过立即接受它,您会阻止其他人发布答案,因此您可能永远找不到更好的方法来做您想做的事情。
-
感谢您指出这一点,Ed。确实,您是对的,我并没有过多关注该答案的完整输出,我很高兴您以另一种可能性回答了它,即使我已经将其标记为已解决。我仍然需要更好地掌握论坛使用的机制。感谢您的耐心和时间!
-
不客气。这部分至少很容易——提出一个问题,给它几个小时或一天的时间看看你得到什么答案(根据你的需要提供反馈/问题/cmets)然后然后接受一个你要使用的。
标签: awk