How to print empty line when the a field is changed

Hi all,

I have this input file ..

BSS01 107 Swafieh 11/06/2008 12:06:57
BSS01 111 Ramada_Hotel 12/06/2008 11:37:20
BSS01 147 Kalha_Rep 11/06/2008 19:13:39
BSS01 147 Kalha_Rep 11/06/2008 19:13:39
BSS06 601 Abu_Alanda_Caragat 12/06/2008 02:57:11
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS08 802 Jada_SDH 12/06/2008 09:53:59
BSS08 805 Rakeen_Batter 12/06/2008 09:53:58
BSS08 805 Rakeen_Batter 12/06/2008 09:53:58
BSS08 805 Rakeen_Batter 12/06/2008 09:53:58
BSS08 811 Sarfa 12/06/2008 09:53:57
BSS08 821 Qaser_SDH 12/06/2008 09:54:00
BSS08 821 Qaser_SDH 12/06/2008 09:54:00
BSS08 821 Qaser_SDH 12/06/2008 09:54:00

I need to print empty line when the second field is changed.
like this

BSS01 107 Swafieh 11/06/2008 12:06:57

BSS01 111 Ramada_Hotel 12/06/2008 11:37:20

BSS01 147 Kalha_Rep 11/06/2008 19:13:39
BSS01 147 Kalha_Rep 11/06/2008 19:13:39

BSS06 601 Abu_Alanda_Caragat 12/06/2008 02:57:11
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37

BSS08 802 Jada_SDH 12/06/2008 09:53:59

BSS08 805 Rakeen_Batter 12/06/2008 09:53:58
BSS08 805 Rakeen_Batter 12/06/2008 09:53:58
BSS08 805 Rakeen_Batter 12/06/2008 09:53:58

BSS08 811 Sarfa 12/06/2008 09:53:57

BSS08 821 Qaser_SDH 12/06/2008 09:54:00
BSS08 821 Qaser_SDH 12/06/2008 09:54:00
BSS08 821 Qaser_SDH 12/06/2008 09:54:00

I tried this awk but still doesnt work ..
any help is appreciated ..

BEGIN { site = $2 }

/BSS/{ if ( $2 == site )

printf "%s\n",$0; site = $2

else

printf "\\n" ; site = $2 

}

Thanks

awk 'NR==1{v=$2}$2!=v{v=$2;print ""}1' file

Regards

I want to help you, but I don't quite understand the question. Can you re-phrase it?

Thanks for the quick response.

I tried your code, the ouput was like

BSS01 107 Swafieh 11/06/2008 12:06:57

BSS01 111 Ramada_Hotel 12/06/2008 11:37:20

BSS01 147 Kalha_Rep 11/06/2008 19:13:39

BSS01 147 Kalha_Rep 11/06/2008 19:13:39

BSS06 601 Abu_Alanda_Caragat 12/06/2008 02:57:11

BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37

BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37

BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37

BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37

BSS08 802 Jada_SDH 12/06/2008 09:53:59

BSS08 805 Rakeen_Batter 12/06/2008 09:53:58

BSS08 805 Rakeen_Batter 12/06/2008 09:53:58

BSS08 805 Rakeen_Batter 12/06/2008 09:53:58

BSS08 811 Sarfa 12/06/2008 09:53:57

BSS08 821 Qaser_SDH 12/06/2008 09:54:00

BSS08 821 Qaser_SDH 12/06/2008 09:54:00

BSS08 821 Qaser_SDH 12/06/2008 09:54:00

any suggestions ??????

Dear na5m

I mean when the second field is changed I want to print new line.
To group the lines that have the same second field together.

regards

Please Mr. Franklin Could you explain your code for me ...
Thanks very very much for your efforts. :slight_smile:

For what it's worth, here's a partial solution:

$ echo file | sort --key=2,2

I'll post a full solution, if I can make it that far :o

awk 'NR==1{v=$2}$2!=v{v=$2;print ""}1' file

I've tested the code with your file and it works fine for me....:rolleyes:

Explanation:

NR==1{v=$2}

Assign the value of the 2nd field to variable v if the line number = 1

$2!=v{v=$2;print ""}

If variable v (the value of the 2nd field of the previous line) don't match with the 2nd field, assign the value of the 2nd field to variable v and print an empty line.

1' file

awk evaluates after the last close brace the 1 as true and the prints the entire line by default.

Regards

Thanks very very very very much mannnnnnn, YOU ARE GREAT..... I changed it to nawk and it worked.

:b: :b: :b: :b: :b:

Here is one I just test, kind of late.

NR==1{print $0; f2=$2; next}
{if(NR!=1) 
        {
        if(f2==$2)
                print $0
        else
        {
                printf("\n%s\n", $0)
                f2=$2
        }
}
}