Remove & insertion of data in a same file

I am iterating record by record through a file as below,

A,B
A,C
A,D
B,E
B,F
E,G
E,H

The same file should look like in the final output as below,

A,B
B,E
E,G
E,H
B,F
A,C
A,D

So while iterating, it should remove records from the same file & paste inside in the same file.

How can we achieve this functionality of removing & pasting records in the same file?

I can think of pseudo code as below;
CODE START
cat File1.txt | while read row; do

col1=`echo $row | awk -F","'{print $1}'`
col2=`echo $row | awk -F","'{print $2}'`

#if col2 has first column occurance in the file
awk /^"$col2"/ filename > Temp.txt

# HOW TO REMOVE CONTENTS from File1.txt which starts with $col2

# will echo Temp.txt >> File1.txt ... will paste the records in between?
done < File1.txt
CODE END

I hope I am clear in passing my problem.

awk -F"," '
{ 
 print ; 
 str=$1 ;
 while ( getline )
 {
	while( str == $1 ) 
	{ str1 = str1 "\n" $0 ; getline } 
	txt = str1 txt
	str1=""
	print ;
	str=$1 ;
 } 
}
END { sub("^ *\n","",txt );print txt } ' < file > file

This has deleted all the contents from the file. It is not working as expected.

Try this

$ cat file
A,B
A,C
A,D
B,E
B,F
E,G
E,H
$ awk -F"," '
> {
> print > "file" ;
>  str=$1 ;
>  while ( getline )
>  {
>         while( str == $1 )
>         { str1 = str1 "\n" $0 ; getline }
>         txt = str1 txt
>         str1=""
>         if( $0 != "" ) print >"file";
>         str=$1 ;
>  }
> }
> END { sub("^ *\n","",txt );print txt > "file"} ' < file
$ cat file
A,B
B,E
E,G
E,H
B,F
A,C
A,D

Hi Anbu, with hardcoded filename it works fine. How to make it work with filename indicated by a variable?

Say at the place of file if wish to use $fname?

Thanks for your co-operation. I tried using -v option but then I am getting some awk error.

try this

awk -F"," -v file="$filenm" '
{
print > file  ;
 str=$1 ;
 while ( getline )
 {
        while( str == $1 )
        { str1 = str1 "\n" $0 ; getline }
        txt = str1 txt
        str1=""
        if( $0 != "" ) print > file;
        str=$1 ;
 }
}
END { sub("^ *\n","",txt );print txt > file} ' < $filenm

Hi Anbu

But the same program could not work for the following combination -

A,B
A,C
A,D
B,F
B,E
E,G
E,H

For this output is appearing as -
A,B
B,F
E,G
E,H
B,E
A,C
A,D

Expected as -

A,B
B,F
B,E
E,G
E,H
A,C
A,D

If I have to say in text,
If you encounter some text in 2nd column & the same available as first column in the file those records should come after that in the same file.

I fought with the script you provided, but I couldnt succeed.

awk -F"," -v file="$filenm" '
{
print > file;
 str=$1 ; str_2nd_fld = $2;
 while ( getline )
 {
        while( str_2nd_fld != $1 &&  str == $1 )
        { str1 = str1 "\n" $0 ; str_2nd_fld = $2; getline }
	if ( str_2nd_fld == $1 ) { n=split(str1,arr,"\n"); print arr[n] > file; sub("\n[^\n]*$","",str1) }
        txt = str1 txt
        str1=""
        if( $0 != "" ) print > file;
        str=$1 ;
        str_2nd_fld = $2;
 }
}
END { sub("^ *\n","",txt );print txt > file} ' < $filenm

This script is eating up the very first record of the expected output.

Output I received as -
B,F
B,E
E,G
E,H
A,C
A,D

record A,B got omitted. How can it be done?

Also, I will be thankful, if you can point me to the site where I can have a decent start to learn inside programming of the awk.

I dont see any problems

$ cat file
A,B
A,C
A,D
B,F
B,E
E,G
E,H
$ filenm=file
$ awk -F"," -v file="$filenm" '
> {
> print > file;
>  str=$1 ; str_2nd_fld = $2;
>  while ( getline )
>  {
>        while( str_2nd_fld != $1 &&  str == $1 )
>        { str1 = str1 "\n" $0 ; str_2nd_fld = $2; getline }
>         while( str_2nd_fld != $1 &&  str == $1 )
>         { str1 = str1 "\n" $0 ; str_2nd_fld = $2; getline }
>       if ( str_2nd_fld == $1 ) { n=split(str1,arr,"\n"); print arr[n] > file; sub("\n[^\n]*$","",str1) }
>         txt = str1 txt
>         str1=""
>         if( $0 != "" ) print > file;
>         str=$1 ;
>         str_2nd_fld = $2;
>  }
> }
> END { sub("^ *\n","",txt );print txt > file} ' < file
$ cat file
A,B
B,F
B,E
E,G
E,H
A,C
A,D

check this post for awk tutorial
http://www.unix.com/showthread.php?p=302108502\#post302108502

You were right, I was missing the very first "print > file;" line.

Your link helped me to locate that. Thanks a lot.