【发布时间】:2015-10-23 18:06:59
【问题描述】:
我正在使用 awk 处理文件以将行过滤到特定的感兴趣的行。使用生成的输出,我希望能够删除除最后一行以外的所有以相同字符串开头的行。
以下是生成的示例:
this is a line
duplicate remove me
duplicate this should go too
another unrelated line
duplicate but keep me
example remove this line
example but keep this one
more unrelated text
第 2 行和第 3 行应删除,因为它们以 duplicate 开头,第 5 行也是如此。因此应保留第 5 行,因为它是最后一行以 duplicate 开头。
第 6 行也是如此,因为它以 example 开头,第 7 行也是如此。因此应保留第 7 行,因为它是最后一行以 example 开头。
鉴于上面的例子,我想产生以下输出:
this is a line
another unrelated line
duplicate but keep me
example but keep this one
more unrelated text
我怎样才能做到这一点?
我尝试了以下方法,但无法正常工作:
awk -f initialProcessing.awk largeFile | awk '{currentMatch=$1; line=$0; getline; nextMatch=$1; if (currentMatch != nextMatch) {print line}}' -
【问题讨论】:
-
你的例子不清楚