Insert new line based on numerical number of column

My input file:
Class
Number Position Range
1 Initial 50
1 Initial 50
2 Terminal 150
2 Terminal 20
2 Single 10
3 Single 20
4 Double 50
5 Initial 50
5 Initial 60

Class
Number Position Range
1 Initial 150
2 Terminal 250
2 Terminal 200
3 Single 20
4 Double 50
4 Single 100
5 Initial 100
.
.
.

My output file:
Class
Number Position Range

1 Initial 50
1 Initial 50

2 Terminal 150
2 Terminal 20
2 Single 10

3 Single 20

4 Double 50

5 Initial 50
5 Initial 60

Class
Number Position Range

1 Initial 150

2 Terminal 250
2 Terminal 200

3 Single 20

4 Double 50
4 Single 100

5 Initial 100

.
.
.

I got a long list of file. How can I added new line by using awk,sed, or perl based on the numerical number of column 1?
Each section of the content begin with "Class".
Thanks a lot for suggestion.

number=1
outputFile="/tmp/output"

for line in `cat inputFile`
do
    if (`awk -F" " '{print $1}'` -eq $number )
    then
          echo $line >> $outputFile
    else
          echo "\n$line" >> $outputFile
          $number++;
    fi
done

Hi tene,
I just try the script that you suggestion, but it seems like can't work?
Do I got did anything wrong?
Thanks for your reply.

try this :

#!/bin/bash
NN=""
outputFile="/tmp/output"
while read N LINE
do
    [ "$NN" != "$N" ] && echo
    echo "$N $LINE"
    NN=$N
done < $inputfile > $outputfile

Or with awk:

awk '
   (NR > 2) && (X != $1) { print "" } { X = $1 } 1
' input_file > output_file

Number Position Range
1 Initial 50
1 Initial 50

2 Terminal 150
2 Terminal 20
2 Single 10

3 Single 20

4 Double 50

5 Initial 50
5 Initial 60

thanks a lot, frans.
Your code work perfectly :slight_smile:
Do you mind roughly to explain the back story behind your code?
Thanks again.

---------- Post updated at 03:04 AM ---------- Previous update was at 03:01 AM ----------

thanks scottn.
Your code is worked as well ^^
thanks for sharing :slight_smile:

I just gave you the logic.
Did u add #!/usr/bin/ksh at the top.

What error are you getting?

Thanks tene.
I think the error I get is because that I can't run the script with ksh :frowning:
Anywhere still thanks for your help.
Sorry for my mistakes.

---------- Post updated at 03:57 AM ---------- Previous update was at 03:33 AM ----------

Hi scottn,
By editing your awk code, I got try this way to achieve my another desired output result:

awk 'BEGIN {FS="Class"} (NR > 2) && (X != $1) { print "" } { X = $1 } 1' input_file > output_file

Unfortunately, it can't get my desired output result. Do you got any suggestion for me to improve your code?
thanks again.

awk '/^[0-9]/ && (X != $1) { print "" } { X = $1 } 1' input_file > output_file

Class
Number Position Range

1 Initial 50
1 Initial 50

2 Terminal 150
2 Terminal 20
2 Single 10

3 Single 20

4 Double 50

5 Initial 50
5 Initial 60

Class
Number Position Range

1 Initial 150

2 Terminal 250
2 Terminal 200

3 Single 20

4 Double 50
4 Single 100

5 Initial 100

Please don't change your requirement bit by bit. Try to think about everything you need to achieve. Thanks :wink:

#!/bin/bash
NN=""
outputFile="/tmp/output"
while read N LINE # Puts the first field (the number) in N and the rest in LINE
do
    [ "$NN" != "$N" ] && echo # insert newline if the N is different than the previous value
   # the && executes the next command if the return of the previous one returned TRUE
   # the "!" negates the "=" and  "[ ... ]" means "test if"
    echo "$N $LINE"
    NN=$N
done < $inputfile > $outputfile
my $flag = "-1.1";
while(<DATA>){
	my @tmp = split(" ",$_,2);
  if (($tmp[0] ne $flag) && /^[0-9]/){
  	print "\n";
  	$flag = $tmp[0];
  }
  print;
}
__DATA__
Class
Number Position Range
1 Initial 50 
1 Initial 50
2 Terminal 150
2 Terminal 20 
2 Single 10 
3 Single 20
4 Double 50
5 Initial 50
5 Initial 60

Class
Number Position Range
1 Initial 150 
2 Terminal 250
2 Terminal 200 
3 Single 20
4 Double 50
4 Single 100 
5 Initial 100

Thanks a lot for your explanation, frans.
I fully understand the back story of your script now :slight_smile:
Thanks again.