playlist - AWK merging of two files intercrossing lines -


and thank taking moment reading this, @ point trying finish , polish little radio station using icecast, working.

however there program called ezstream, stream music icecast when nobody live, common autodj, wich uses playlists.

said playlist can play once , program closes, , thats want exploit here, managed automatically create 2 different playlists, 1 music, , other 1 commercials, jingles , soundbytes, , list of files stored on computer. following, have names , stuff.

this playlist1

/home/mp3/albums/album1/title1.mp3  /home/mp3/albums/album1/title2.mp3  /home/mp3/albums/album1/title3.mp3  /home/mp3/albums/album1/title4.mp3  /home/mp3/albums/album1/title5.mp3  /home/mp3/albums/album2/title1.mp3  /home/mp3/albums/album2/title2.mp3  /home/mp3/albums/album2/title3.mp3  /home/mp3/albums/album2/title4.mp3  /home/mp3/albums/album2/title5.mp3 

the playlist2 similar contains commercials looks this

/home/mp3/commercials/commercial1.mp3  /home/mp3/commercials/commercial2.mp3  /home/mp3/commercials/commercial3.mp3  /home/mp3/commercials/commercial4.mp3  /home/mp3/commercials/commercial5.mp3 

the part im critically stuck in mergin 1 in 2:1 or 3:1 ratio (if give me hand both codes awesome too.

the final output supposed this

/home/mp3/albums/album1/title1.mp3  /home/mp3/albums/album1/title2.mp3  /home/mp3/commercials/commercial1.mp3  /home/mp3/albums/album1/title3.mp3  /home/mp3/albums/album1/title4.mp3  /home/mp3/commercials/commercial2.mp3  /home/mp3/albums/album1/title5.mp3  /home/mp3/albums/album2/title1.mp3  /home/mp3/commercials/commercial2.mp3 

and on until both files merged completely, far i've managed find code, reason not working, gives errors related syntax , missing < symbol.

awk code:

awk ‘fnr==nr{  song[fnr]=$0;  next  }  {  print song[fnr+line];line++;  print song[fnr+line]  print $0  }’ playlist1.m3u playlist2.m3u 

all should outputed third file mergedplaylists.m3u

i didnt created code, altho i've tried fiddling while, 1 thing not clear me why "song" in there, can changed else? "album" ?

the code not work @ all, neither writes @ output file (nor know if correct).

i hope some1 can give me hand particular case, awk seems helpful cryptic , im finding lot of problems understand it...

thank again

this put 2 songs before each commercial:

awk ' fnr==nr{ song[++numsongs]=$0; next } {    (i=1;i<=2;i++)       print song[++songnr]    print } songnr == numsongs { exit } ' playlist1.m3u playlist2.m3u 

change "2" "3" or whatever see fit.

alternative implementation based on comments below:

$ cat tst.awk begin{ interval = (interval ? interval : 3) }  nr==fnr { songs[++numsongs] = $0; next }  { commercials[++numcommercials] = $0 }  end {     (songnr=1; songnr<=numsongs; songnr++) {          print songs[songnr]          if ( !( songnr % interval) && (++commercialnr in commercials) )             print commercials[commercialnr]      }  } $ $ cat songs.txt /home/mp3/albums/album1/title1.mp3 /home/mp3/albums/album1/title2.mp3 /home/mp3/albums/album1/title3.mp3 /home/mp3/albums/album1/title4.mp3 /home/mp3/albums/album1/title5.mp3 /home/mp3/albums/album2/title1.mp3 /home/mp3/albums/album2/title2.mp3 /home/mp3/albums/album2/title3.mp3 /home/mp3/albums/album2/title4.mp3 /home/mp3/albums/album2/title5.mp3 $ $ cat commercials.txt /home/mp3/commercials/commercial1.mp3 /home/mp3/commercials/commercial2.mp3 /home/mp3/commercials/commercial3.mp3 /home/mp3/commercials/commercial4.mp3 /home/mp3/commercials/commercial5.mp3 $ $ awk -f tst.awk songs.txt commercials.txt /home/mp3/albums/album1/title1.mp3 /home/mp3/albums/album1/title2.mp3 /home/mp3/albums/album1/title3.mp3 /home/mp3/commercials/commercial1.mp3 /home/mp3/albums/album1/title4.mp3 /home/mp3/albums/album1/title5.mp3 /home/mp3/albums/album2/title1.mp3 /home/mp3/commercials/commercial2.mp3 /home/mp3/albums/album2/title2.mp3 /home/mp3/albums/album2/title3.mp3 /home/mp3/albums/album2/title4.mp3 /home/mp3/commercials/commercial3.mp3 /home/mp3/albums/album2/title5.mp3 $ $ awk -v interval=1 -f tst.awk songs.txt commercials.txt /home/mp3/albums/album1/title1.mp3 /home/mp3/commercials/commercial1.mp3 /home/mp3/albums/album1/title2.mp3 /home/mp3/commercials/commercial2.mp3 /home/mp3/albums/album1/title3.mp3 /home/mp3/commercials/commercial3.mp3 /home/mp3/albums/album1/title4.mp3 /home/mp3/commercials/commercial4.mp3 /home/mp3/albums/album1/title5.mp3 /home/mp3/commercials/commercial5.mp3 /home/mp3/albums/album2/title1.mp3 /home/mp3/albums/album2/title2.mp3 /home/mp3/albums/album2/title3.mp3 /home/mp3/albums/album2/title4.mp3 /home/mp3/albums/album2/title5.mp3 $ $ awk -v interval=2 -f tst.awk songs.txt commercials.txt /home/mp3/albums/album1/title1.mp3 /home/mp3/albums/album1/title2.mp3 /home/mp3/commercials/commercial1.mp3 /home/mp3/albums/album1/title3.mp3 /home/mp3/albums/album1/title4.mp3 /home/mp3/commercials/commercial2.mp3 /home/mp3/albums/album1/title5.mp3 /home/mp3/albums/album2/title1.mp3 /home/mp3/commercials/commercial3.mp3 /home/mp3/albums/album2/title2.mp3 /home/mp3/albums/album2/title3.mp3 /home/mp3/commercials/commercial4.mp3 /home/mp3/albums/album2/title4.mp3 /home/mp3/albums/album2/title5.mp3 /home/mp3/commercials/commercial5.mp3 $ $ awk -v interval=4 -f tst.awk songs.txt commercials.txt /home/mp3/albums/album1/title1.mp3 /home/mp3/albums/album1/title2.mp3 /home/mp3/albums/album1/title3.mp3 /home/mp3/albums/album1/title4.mp3 /home/mp3/commercials/commercial1.mp3 /home/mp3/albums/album1/title5.mp3 /home/mp3/albums/album2/title1.mp3 /home/mp3/albums/album2/title2.mp3 /home/mp3/albums/album2/title3.mp3 /home/mp3/commercials/commercial2.mp3 /home/mp3/albums/album2/title4.mp3 /home/mp3/albums/album2/title5.mp3 

here's awk:

begin{ interval = (interval ? interval : 3) }  nr==fnr { songs[++numsongs] = $0; next }  { commercials[++numcommercials] = $0 }  end {     (songnr=1; songnr<=numsongs; songnr++) {          print songs[songnr]          if ( !(songnr % interval) && (++commercialnr <= numcommercials) )             print commercials[commercialnr]      }  } 

here's c-like pseudo-code:

void main() {      file *filep;     char *line;      char *songs[1000];     char *commercials[1000];      int fnr = 0;     int nr = 0;     int interval = 0;     int numsongs = 0;     int numcommercials = 0;     int songnr = 0;     int commercialnr = 0;     int argnr = 0;      /* begin */     if (argv[++argnr] == "interval") {          interval = argv[++argnr];     }     interval = (interval ? interval : 3);      (++argnr;argnr<=argc;argnr++) {        filep = argv[argnr];        fnr = 0;        while ( fgets(line,filep) > 0 ) {           nr++;           fnr++;            if (nr == fnr) { songs[++numsongs] = line; continue; }            commercials[++numcommercials] = line;         }     }      /* end */     (songnr=1; songnr<=numsongs; songnr++) {          printf("%s\n",songs[songnr]);          if ( !(songnr % interval) && (++commercialnr <= numcommercials) )                 printf("%s\n",commercials[commercialnr]);         }      }      return; } 

the change made awk script ease of comparison remove "in" operator there's no clear c equivalent.

hope helps clarify awk script doing.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -