Help with awk for a newbie

Hi there, greetingt to everybody.
I have configured syslog-ng to get messages over UDP saving logs into a text file, it works fine.
I need to store the content of this file in several files depending from some criteria that I try to explain you with some examples :
Suppose the content of my log file (source.txt) looks like this :

1;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;4#xxx;x;x;x;xxxx#xxx;x;xx;x;xxx#xxxx;xx;xxx;x;xxx#xxxx;xx;xx;x;x
1;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;3#xxx;x;x;x;xxxx#xxx;x;xx;x;xxx#xxxx;xx;xxx;x;xxx
2;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;2#xxx;x;x;x;xxxx#xxx;x;xx;x;xxx
1;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;1#xxx;x;x;x;xxxx
2;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;3#xxx;x;x;x;xxxx#xxx;x;xx;x;xxx#xxxx;xx;xxx;x;xxx

As you can see I have 3 different kind of separators ";" "|" "#", there is a fix number of columns ( first 7 columns ) while the lenght of each row depends from value reported in the column n. 7. Depending from the value reported in column 7 I have several blocks of 5 columns separated by "#".
The result must be something like this :

File1.txt :
1;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;4#xxx;x;x;x;xxxx#xxx;x;xx;x;xxx#xxxx;xx;xxx;x;xxx#xxxx;xx;xx;x;x
1;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;3#xxx;x;x;x;xxxx#xxx;x;xx;x;xxx#xxxx;xx;xxx;x;xxx
1;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;1#xxx;x;x;x;xxxx

File2.txt
2;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;2#xxx;x;x;x;xxxx#xxx;x;xx;x;xxx
2;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;3#xxx;x;x;x;xxxx#xxx;x;xx;x;xxx#xxxx;xx;xxx;x;xxx

I think could be done with 'awk' somethin similar to :
awk -F";" '$1 == "1"' source.txt > File1.txt
awk -F";" '$1 == "2"' source.txt > File2.txt
I do not know if it's possible to create both files with just one awk command line but I am quite sure it is possible.
Than I need to create other 2 files from File1.txt and File2.txt.
In this case what I need is to split every line into several lines depending from the number reported into the column n. 7, in each line of this new file must be report just 2 columns of the original file as reported in the example below :

Suppose this is the content of the ordinal file (File2.txt)
2;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;2#xxx;x;x;x;xxxx#xxx;x;xx;x;xxx
2;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;3#xxx;x;x;x;xxxx#xxx;x;xx;x;xxx#xxxx;xx;xxx;x;xxx

I need this in the new file :
xxxxxx#xxx;x;x;x;xxxx
xxxxxx#xxx;x;xx;x;xxx
xxxxxx#xxx;x;x;x;xxxx
xxxxxx#xxx;x;xx;x;xxx
xxxxxx#xxx;xx;x;x;xxx

I hope the above example was self explaining for you, I need to create from one row as many rows depending from number that can be found in the column n. 7, than each row must have 2 columns where the first is the content of the column n.6 of the original file and the second is the content of the block of 5 columns found in the original file separated by "#".

Any idea about how to solve this problem, thanks in advance.
Greetings

To separate the files:

awk -F";" '{print > "File"$1".txt"}' source.txt

To get the end result:

awk -F ";" '{
  n=split($0,a,"#")
  for(i=2;i<=n;i++){ 
    print $5 "#" a
  }
}' File2.txt

if you have Python

#!/usr/bin/env python
for line in open("file"):
    open("file_"+line[0]+".txt","a").write(line)  
    f=open(line[0]+".txt","a")    
    line=line.strip().split("#")
    oline = line[0].split(";")
    ordinal=oline[-2]
    for i in line[1:]:
        f.write(ordinal+"#"+i+"\n")
    f.close()

output

# more file_1.txt  file_2.txt
::::::::::::::
file_1.txt
::::::::::::::
1;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;4#xxx;x;x;x;xxxx#xxx;x;xx;x;xxx#xxxx;xx;xxx;x;xxx#xxxx;xx;xx;x;x
1;xxxxx;xxxxxxxxxx|xxxx;xxx;xxxxxx;3#xxx;x;x;x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'{if((f="file"$1".txt")!=of){close(of);of=f};p=$5;oFS=FS;FS="#";$0=$0;for(i=2;i<=NF;i++)print p"#"$i>>f;FS=oFS}' source.txt

cat file1.txt
aaaaaa#a1x;x;x;x;xxxx
aaaaaa#a2x;x;xx;x;xxx
aaaaaa#a3xx;xx;xxx;x;xxx
aaaaaa#a4xx;xx;xx;x;x
bbbbbb#b1x;x;x;x;xxxx
bbbbbb#b2x;x;xx;x;xxx
bbbbbb#b3xx;xx;xxx;x;xxx
dddddd#d1x;x;x;x;xxxx

cat file2.txt
cccccc#c1x;x;x;x;xxxx
cccccc#c2x;x;xx;x;xxx
eeeeee#e1x;x;x;x;xxxx
eeeeee#e2x;x;xx;x;xxx
eeeeee#e3xx;xx;xxx;x;xxx

Un-obscured awk code:

{
if( (f="file"$1".txt") != of )  {
  close(of) # close current file if we switched to another output file
  of = f
  }
p = $5
oFS = FS # save current FS
FS = "#" # set new FS
$0 = $0 # rebuild fields according to new FS
for (i = 2; i <= NF; i++) print p"#"$i >> f
FS = oFS # restore FS
}

To avoid hitting the max number of open files, the code does a close() just before (re)opening a file. To keep the close-open operations to a minimum it would be smarter to do:

sort source.txt | nawk -F\; ...

Thank you Franklin54 and Ghostdog74 .... both configurations are solving my problem ... Thanks a lot .....

Dear Colemar I'll try also you script .... I'll let you now thanks al lot to you .....

I have for you an additional question, how can I transform all sepatators ( "|" ";" "#") in one ";".
After the cration of the files I need to have just ";" as separator and not the originals.
I do not care if this is done before, at mean time or after the creation as described in my first post.

Greetings to everybody

Something like:

tr '|#' ';' < file > newfile