计算具有不同开头的相邻行

use*_*719 0 grep sed awk text-processing

我有对话转录的文本文件,例如:

Speaker1: Hello. 
Speaker2: Hi. 
Speaker2: How are you? 
Speaker1: Well thanks.
Run Code Online (Sandbox Code Playgroud)

我想计算说话者从一行到另一行的变化次数。因此,对于本例,它将是 2(第 1 行到第 2 行,第 3 行到第 4 行,但不是第 2 行到第 3 行)。

Ed *_*ton 5

$ awk -F':' '(NR>1) && ($1!=prev){c++} {prev=$1} END{print c+0}' file
2
Run Code Online (Sandbox Code Playgroud)