0

I have been trying to do this myself based on other examples but have failed. I want awk to reprint a tab delimited file but add an extra column at the end which starts at 1 and increases as long as column1 is the same, when column1 has a new value, last column gets reset to 1....

Brief example, input if columns 1-3 and output has last column:

A11R Q9Y5X3 SNX5  1
A12L Q9Y316 MEMO1 1
A12L Q70CQ2 USP34 2
A12L Q8N7H5 PAF1  3
A12L Q8IZL8 PELP1 4
A13L Q5SY16 NOL9  1
A13L Q6P1J9 CDC73 2
A13L Q8WVC0 LEO1  3
A13L Q9Y4W2 LAS1L 4
A13L Q6PD62 CTR9  5

I know I first need to read the file and make an array of column 1 values and then print the file based on an if condition where if column1 is the same as previous row, i++

I tried:

awk '{A[++c] = $1} END {d=1; for ( i = 1; NR == i; i++ ) {if (A[i] = A[i-1]) {d++} else {d=1}; print d, $0}}'

But clearly I am missing some basics of how this works.

3 Answers 3

2
$ awk -F '\t' 'BEGIN { OFS=FS } $1 != save { counter = 1; save = $1 } { print $0, counter++ }' file
A11R    Q9Y5X3  SNX5    1
A12L    Q9Y316  MEMO1   1
A12L    Q70CQ2  USP34   2
A12L    Q8N7H5  PAF1    3
A12L    Q8IZL8  PELP1   4
A13L    Q5SY16  NOL9    1
A13L    Q6P1J9  CDC73   2
A13L    Q8WVC0  LEO1    3
A13L    Q9Y4W2  LAS1L   4
A13L    Q6PD62  CTR9    5

The awk code compares the value of the 1st field to a remembered value in save. If the values differ, the counter counter is reset to 1, and the remembered value is updated with the value from the 1st field.

The line is then outputted with the counter added as a last field (and incremented).

0
2

Given your example, you only need to remember the value of the first field of the last run and a counter variable:

awk -F'\t' 'BEGIN{ OFS=FS }
{
  # if same field, increment counter, else reset counter
  count=(last==$1 ? ++count : 1)

  # remember first field
  last=$1
  
  print $0, count
}' file
0

Using Miller:

$ mlr --tsv -N step -a counter -f 1 -g 1 file

-N file is headerless.

step verb is used for calculating something(counter, ewma etc.) between successive records may be grouped by some field in records.

The following command gives similar result but counter is added as first field.

$ mlr --tsv -N cat -n -g 1 file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .