[awk] split file1 and save it as var from file2

I have 2 files:

file_1:

file_2:

expected result:
name file:
"artV1"

"artV2"

etc.

I have:

but why don;t work save to file 'out'??

when use this, it's look great but file name is wrong; is track1 not artV1 :wall:

Try this:

awk 'FNR==NR{if(FNR>1) fil[$2]=$1;next} 
{
 for(i=2;i<NF;i++)
 {
  if(FNR==1){filepos=fil[$i]}
  print $1 FS $i FS > filepos
 }
}' file2 FS=";" file1

I've assumed that the maximum number of files required to be generated is within the max. open files limit for your awk implementation (should not be a big concern!).

I've also assumed that file2 has all the possible values occurring in record 1 of file1.

Ok it's work, but how it us in awk file:
this don't work:

It's a bit tedious job to do it the way you want it done. :slight_smile: Can't use FNR/NR in this case because getline will not set these variables.

cat split.awk

BEGIN {
while(getline < "fil2")
 if(NF==2)
  fil[$2]=$1
close("fil2")
FS=";"
while(getline < "fil1")
{
 if($0 ~ /^Head/)
  for(i=2;i<NF;i++)
   filepos=fil[$i]
 if($0 ~ /^date/)
  for(i=2;i<NF;i++)
   print $1 FS $i FS > filepos
}
}
1 Like