Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert new line if different number appears in a column

Tags:

linux

bash

I have a column

1
1
1
2
2
2

I would like to insert a blank line when the value in the column changes:

1
1
1
                <- blank line
2
2
2
like image 983
user3270725 Avatar asked Aug 10 '14 18:08

user3270725


1 Answers

I would recommend using awk:

awk -v i=1 'NR>1 && $i!=p { print "" }{ p=$i } 1' file

On any line after the first, if value of the "i"th column is different to the previous value, print a blank line. Always set the value of p. The 1 at the end evaluates to true, which means that awk prints the line. i can be set to the column number of your choice.

like image 87
Tom Fenech Avatar answered Nov 14 '22 23:11

Tom Fenech