简体   繁体   English

如果行与上一行不匹配,如何使用awk打印计数器+1

[英]How to use awk to print counter+1 if line does not match previous line

I have been working in bash trying print text based on an input file. 我一直在bash中尝试根据输入文件打印文本。 My file contains the following text: 我的文件包含以下文本:

a
a
a
b
c
c
c
d
d
e
e

For each line that does not match the previous line, I want to add 1 to a counter and print the counter next to original entry. 对于与上一行不匹配的每一行,我想在计数器上加1并在原始条目旁边打印计数器。 For lines that are equal, I would like to print the counter as is, like so: 对于相等的行,我想按原样打印计数器,如下所示:

a 1  
a 1  
a 1  
b 2  
c 3  
c 3  
c 3  
d 4  
d 4  
e 5  
e 5   

I have tried the code below, but this only provides me with information for the lines that are equal to the previous lines and does not print the counter. 我已经尝试了下面的代码,但这仅向我提供了与前几行相同的信息,并且不打印计数器。

f=a
counter=1

awk '{ 
if ($0==f && NR>1) {print f, counter} {f=$0} ; 
next
elif ($0!=f && NR>1) 
{print f, ++counter} {f=$0}
}' file.txt

output: 输出:

a   
a   
c   
c   
d   
e   

This is all you need 这就是你所需要的

awk '
    $1 != prev {c++; prev=$1} 
    {print $0, c}
' file

On the first line of the file, both prev and c will be undefined. 在文件的第一行, prevc均未定义。 The $1 != prev test will be true. $1 != prev测试将为true。 awk will treat undefined c as the number zero in arithmetic context, so the ++ operator properly sets c to 1. 在算术上下文中,awk将未定义的c视为数字零,因此++运算符将c正确设置为1。

Two place you're going wrong: 您出错的两个地方:

  • expecting to be able to share shell and awk variables. 期望能够共享shell和awk变量。 You can't that way 你不能那样
  • syntax for the if command: you have way too many braces. if命令的语法:大括号过多。

This is your code restructured so that it works: 这是对您的代码进行重组,以使其起作用:

awk -v f=a -v counter=1 '{
    if ($0==f && NR>1) {print f, counter; f=$0 ; next}
    if ($0!=f && NR>1) {print f, ++counter; f=$0}
}' file

But it's written in a non-idiomatic way, and is pretty repetitive. 但是它是以非惯用的方式编写的,并且非常重复。 Plus you need to know the first line of the file. 另外,您需要知道文件的第一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM