Shuffling text - add lower lines to the end of upper lines

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Shuffling text - add lower lines to the end of upper lines

#1 Post by foxidrive » 29 May 2015 11:12

Hi Guys, I have a puzzle that I'm not clever enough to solve with findrepl or jrepl - without growing a second brain cell to help my main brain cell with this task.

Here are the source, and result files I would like, plus the rules for the task that I think are complete.

If anyone finds it an interesting puzzle than I'd appreciate help to solve this.


In essence I'd like to add titles after the 17th one, to the end of the upper set of titles from 01 to 17,
and discard any above 17+17 titles ... but there are some tricky bits.

Source file:

Code: Select all

             Album: Auf Wiederseh'n
01 Tanze mit mir inden Morgen: Gerhard Wendland
02 Mondhelle Nacht: Gert & Hermien
03 Lass mein Herz: Enca marina
04 Mama: Heintje
05 Nimm deine weisse gitarre: Gert Timmerman
06 Heitschi: Heintje
07 Monika: Ulli Martin
08 Blau Bluht der Enzian: Heino
09 Emmer Sontags: Cindy & Bert
10 Aufwiedersehn: Denis Roussos
11 Michaela: Bata Ellic
12 Schon ist es auf: Roy Black & Anita
13 Adios Amar: Andy Borg
14 Ich sing ein Lied: Heintje
15 Ich bau dir ein Schloss: Heintje
16 Unknown: track 16: Artist at Australia
17 Unknown: track 17: Artist at Sweden
18 Unknown: track 18: Artist at Home
19 Unknown: track 19: Artist at America
20 Unknown: track 20: Artist at Sweden

Result file.

Code: Select all

             Album: Auf Wiederseh'n
01 Tanze mit mir inden Morgen: Gerhard 18 Unknown: track 18: Artist at
02 Mondhelle Nacht: Gert & Hermien     19 Unknown: track 19: Artist at
03 Lass mein Herz: Enca marina         20 Unknown: track 20: Artist at
04 Mama: Heintje                       
05 Nimm deine weisse gitarre: Gert Timmerman
06 Heitschi: Heintje
07 Monika: Ulli Martin
08 Blau Bluht der Enzian: Heino
09 Emmer Sontags: Cindy & Bert
10 Aufwiedersehn: Denis Roussos
11 Michaela: Bata Ellic
12 Schon ist es auf: Roy Black & Anita
13 Adios Amar: Andy Borg
14 Ich sing ein Lied: Heintje
15 Ich bau dir ein Schloss: Heintje
16 Unknown: track 16: Artist at Australia
17 Unknown: track 17: Artist at Sweden


Rules for this task:

Code: Select all

If any lines exist after title 17 (the album line is not counted or changed)

A)  then - truncate the titles from 01 to 17 at 38 characters and pad with spaces to 38 characters if needed.

B) add a space to the beginning of all titles numbered 18 and onward.

C) add the 18 and over titles at the end of 01,02,03 titles, and onward until it reaches title numbered 17.
    Also truncate all of the added titles at character 32, including the leading space
   (title line is 38 characters, a space, and 31 characters from the added title - making 70 char in total.

   Any extra title over (17 + 17) can be discarded
   
D) Any titles past 01,02,03 etc that do not have an added line - do not need to be truncated or padded

E) An extra complication is that some files use three digit numbers for titles,
   and some have no numbers, but still need to be processed in the same way.


The aim of the exercise is to make the result file into an image (with ImageMajick)
and an android player displays this image, while it plays the mp3 of the record.

The display screen is large, and it needs a large font for the lady to read
the titles - so this would allow her to follow more of the titles on the record.


I'd love some help or pointers to techniques that will help me solve this. Ta!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Shuffling text - add lower lines to the end of upper lin

#2 Post by aGerman » 29 May 2015 12:37

You could combine the SET /P technique with FOR /F and FINDSTR /N.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set "src=input.txt"
set "dest=output.txt"

set "sp=                                      "
set "lns="
<"%src%" >"%dest%" (
  setlocal EnableDelayedExpansion
  set /p "lns="
  echo(!lns!
  endlocal
  for /l %%i in (1 1 17) do set /p "="
  for /f "skip=1 tokens=1* delims=:" %%i in ('findstr /n . "%src%"') do (
    set "lnf=%%j"
    set "lnf_sp=%%j%sp%"
    set "lns=" &set /p "lns="
    setlocal EnableDelayedExpansion
    if defined lns (
      echo(!lnf_sp:~,38! !lns:~,31!
    ) else (
      echo(!lnf:~,70!
    )
    endlocal
    if %%i gtr 17 exit /b
  )
)


Regards
aGerman

PS: Do you realy like German old-school "Schlager" Music :?: :shock:

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Shuffling text - add lower lines to the end of upper lin

#3 Post by Aacini » 29 May 2015 15:05

I like this "puzzle"! :)

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "spaces= "
for /L %%i in (1,1,6) do set "spaces=!spaces!!spaces!"

set i=99
for /F "delims=" %%a in (input.txt) do (
   set /A i+=1
   if !i! leq 117 (
      set "line[!i:~1!]=%%a"
      if !i! equ 117 set i=200
   ) else if !i! leq 217 (
      set "line= %%a"
      for %%i in (!i:~1!) do (
         set "line[%%i]=!line[%%i]!%spaces%"
         set "line[%%i]=!line[%%i]:~0,38!!line:~0,32!"
      )
   )
)

(for /F "tokens=2 delims==" %%a in ('set line[') do echo %%a) > output.txt


Antonio

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Shuffling text - add lower lines to the end of upper lin

#4 Post by Jer » 29 May 2015 17:03

Don't know if this is good coding, but it get's it done.

Code: Select all

%Echo Off
setlocal enabledelayedexpansion

Set /P albumName=<Source.txt
Set /A itemCount=0
Set spaces=                                  X
Set spaces=%spaces:~0,34%

::convert 3-digit to 2-digit if needed. count music track lines.
Type NUL>tmpalbum.txt
For /f "skip=1 tokens=1* delims= " %%a In (Source.txt) Do (
   Set /A itemCount+=1
   Set lineNo=%%a
   Set lineNo=!lineNo:~-2!
   Set trackText=%%b
   Echo !lineNo! !trackText!>>tmpalbum.txt
)

Set /A truncLimit=!itemCount!-17
Set /A bCount=0
Set /A cCount=0

::create array #1 with up to 17 items; put anything beyond that is array #2
For /f "tokens=*" %%a In (tmpalbum.txt) Do (
   Set var=%%a
   If !bCount! geq 17 (
       Set var=!var!%spaces%
       Set var=!var:~0,31!
       Set array2[!cCount!]=!var!
       Set /A cCount+=1
   ) Else (
       Set array1[!bCount!]=!var!
       Set /A bCount+=1
   )
)

Set /A bCount=0

::prepare disjoint list
For /L %%d In (0 1 16) Do (
    Set tVar1=!array1[%%d]!
    Set tVar2=!array2[%%d]!
    If Not "!tVar2!"=="" (
        Set tVar1=!tVar1!%spaces%
        Set tVar1=!tVar1:~0,38!
        Set tVar1=!tVar1! !tVar2!
    )
    Set array1[!bCount!]=!tVar1!
    Set /A bCount+=1
)

Echo.& Echo              %albumName%& Echo.
For /L %%d In (0 1 16) Do (
    Echo !array1[%%d]!
)

If EXIST tmpalbum.txt DEL tmpalbum.txt

endlocal

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Shuffling text - add lower lines to the end of upper lin

#5 Post by foxidrive » 30 May 2015 00:54

Many thanks to all you guys! I've got a working script and it is Wunderbar!

Like many music titles, they often have a ! character
and aGerman's code already handled the ! characters with ease.

Thanks for all your ingenious methods!

PS: Do you realy like German old-school "Schlager" Music :?: :shock:


The lady here loves all kinds of old time music, from German to Irish to Scottish - I get to hear them from the next room, and they are better than a lot of trash metal stuff I've heard from my youth. :)

Andre Rieu is a particular favourite of hers and after watching many concerts - he's a nice bloke and a great showman, and the orchestra and singers are marvelous.

Anyway, the images go well with the new script! Thank all.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Shuffling text - add lower lines to the end of upper lin

#6 Post by Aacini » 30 May 2015 03:06

I modified my solution in order to manage exclamation marks. Here it is:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "spaces= "
for /L %%i in (1,1,6) do set "spaces=!spaces!!spaces!"

set i=99
setlocal DisableDelayedExpansion
for /F "delims=" %%a in (input.txt) do (
   set "line=%%a"
   setlocal EnableDelayedExpansion
   set /A i+=1
   if !i! leq 117 (
      for /F "tokens=1-3 delims=/" %%b in ("!i!/!i:~1!/!line!") do (
         endlocal
         set "i=%%b"
         set "line[%%c]=%%d"
         if %%b equ 117 set i=200
      )
   ) else if !i! leq 217 (
      for %%i in (!i:~1!) do set "left=!line[%%i]!%spaces%"
      for /F "tokens=1-4 delims=/" %%b in ("!i!/!i:~1!/!left:~0,38!/!line:~0,31!") do (
         endlocal
         set "i=%%b"
         set "line[%%c]=%%d %%e"
      )
   ) else (
      endlocal
   )
)

(for /F "tokens=2 delims==" %%a in ('set line[') do echo %%a) > output.txt

Antonio

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Shuffling text - add lower lines to the end of upper lin

#7 Post by aGerman » 30 May 2015 04:15

they often have a ! character

Yeah handling special characters correctly is a must while processing text files.

they are better than a lot of trash metal stuff I've heard from my youth

True but I have heard the "Schlager" music all the time when I grew up in the 70s and 80s because my parents like it. Now I want to run away screaming whenever I hear it :lol:

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Shuffling text - add lower lines to the end of upper lin

#8 Post by Ed Dyreen » 30 May 2015 04:48

Andre Rieu is pretty cool, wasting more money on his concerts than he earns.

As we say in dutch; Andre Rieu is ne coole gast 8)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Shuffling text - add lower lines to the end of upper lin

#9 Post by foxidrive » 30 May 2015 08:21

Aacini wrote:I modified my solution in order to manage exclamation marks. Here it is:


Thank you Antonio, it tests fine. :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Shuffling text - add lower lines to the end of upper lin

#10 Post by foxidrive » 30 May 2015 08:29

aGerman wrote:I have heard the "Schlager" music all the time when I grew up in the 70s and 80s because my parents like it. Now I want to run away screaming whenever I hear it :lol:


I learned to appreciate music from the instruments and tonal qualities of voices, as listening to German records while growing up, when I didn't speak more than a few words in German, sort of makes it a little difficult to appreciate the lyrics! ;)

Ed Dyreen wrote:Andre Rieu is pretty cool, wasting more money on his concerts than he earns.
As we say in dutch; Andre Rieu is ne coole gast 8)


Cool is true! ...and I also make up Dutch words. ;)

His sets are incredibly elaborate, and the costumes are top notch - fireworks, and balloons, cameras galore - his Son is involved there too - and the way Andre focuses on children, and disabled people, and songs from respected performers - he has my respect. :thumbsup:

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Shuffling text - add lower lines to the end of upper lin

#11 Post by Ed Dyreen » 30 May 2015 12:28

foxidrive wrote:Cool is true! ...and I also make up Dutch words. ;)
Lolz, dutch isn't only spoken in the Netherlands, if we hadn't sold New York, you'd be speaking Dutch too :p

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Shuffling text - add lower lines to the end of upper lin

#12 Post by foxidrive » 31 May 2015 00:21

Ed Dyreen wrote:if we hadn't sold New York, you'd be speaking Dutch too :p


Yeah, but we all know how that ended up! :)

Image

Post Reply