Batch modifying text - content and context based

Joined
Jan 19, 2023
Messages
2
Reaction score
0
Hello everyone,

Really not sure I'm in the right category for this since I'm really new at anything coding – and programming – related but I'm looking for a way to automate a task that is taking me forever to do by hand.

I'm a graphic designer and I'm working on a book collecting a lot of text entries (Twitter trending terms, for every hour over the course of the year 2022, so around 8,7k words). For design and legibility purposes, I would like to add a Tab symbol before a term anytime it is consecutively recurrent, and reset it anytime another term shows up. It would look something like this (with em-dashes instead of tabs here):

...
Term 1
—Term 1
——Term 1
———Term 1
Term 2
—Term 2
Term 1
—Term 1
——Term 1
Term 3
Term 2
—Term 2
...

Could anyone (try to) explain to me how would I go about writing such a script ?

For now, the text is stored in Excel cells because it is convenient to paste into InDesign tables.

Also, for the patient pedagogues amongst you, there are two caveats that I would assume could cause issues:
-Some of the terms are multi-word phrases or contain hashtags
-Each page of the book contains a day, so the script would need to hard-reset every 24 terms (24 hours) no matter what

Thank you for reading and I hope you have a nice day,

Nino from Brussels
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
You can write a batch script to accomplish this by using the following steps:

  1. Load the text into a file: You can copy the text from Excel cells and paste it into a text file using a text editor like Notepad.
  2. Write a script to process the text: You can write a batch script using the following code:

Code:
@echo off

rem Initialize the variables
set "prevTerm="
set "tabs="

rem Read the file line by line
for /f "delims=" %%a in (textfile.txt) do (
rem If the current term is equal to the previous term, add a tab
if "%%a" == "%prevTerm%" (
set "tabs=%tabs% "
) else (
rem If the current term is not equal to the previous term, reset the tabs
set "tabs="
)
rem Print the tabs and the current term
echo %tabs%%%a
rem Store the current term for comparison in the next iteration
set "prevTerm=%%a"
)

  1. Run the script: Save the script as a .bat file and run it in the command prompt to process the text file and produce the output.
Note: To handle multi-word phrases or hashtags, you can use the "tokens=*" option in the "for /f" loop to preserve the spaces in the terms. To hard-reset every 24 terms, you can us
 
Joined
Jan 19, 2023
Messages
2
Reaction score
0
You can write a batch script to accomplish this by using the following steps:

  1. Load the text into a file: You can copy the text from Excel cells and paste it into a text file using a text editor like Notepad.
  2. Write a script to process the text: You can write a batch script using the following code:

Code:
@echo off

rem Initialize the variables
set "prevTerm="
set "tabs="

rem Read the file line by line
for /f "delims=" %%a in (textfile.txt) do (
rem If the current term is equal to the previous term, add a tab
if "%%a" == "%prevTerm%" (
set "tabs=%tabs% "
) else (
rem If the current term is not equal to the previous term, reset the tabs
set "tabs="
)
rem Print the tabs and the current term
echo %tabs%%%a
rem Store the current term for comparison in the next iteration
set "prevTerm=%%a"
)

  1. Run the script: Save the script as a .bat file and run it in the command prompt to process the text file and produce the output.
Note: To handle multi-word phrases or hashtags, you can use the "tokens=*" option in the "for /f" loop to preserve the spaces in the terms. To hard-reset every 24 terms, you can us
Thanks so much for the answer, I’ll dig into it asap and let you know if I find any success !

Could legitimately be life saving (or at least sanity saving)

Cheers !

Ps: it looks like the end of your message got cut off but thanks a ton still !
 
Joined
Jul 4, 2023
Messages
475
Reaction score
58
Bash:
@echo off

rem Initialize the variables
set "prevTerm="
set "tabs="

rem Read the file line by line
for /f "delims=" %%a in (textfile.txt) do (
rem If the current term is equal to the previous term, add a tab
if "%%a" == "%prevTerm%" (
set "tabs=%tabs% "
) else (
rem If the current term is not equal to the previous term, reset the tabs
set "tabs="
)
rem Print the tabs and the current term
echo %tabs%%%a
rem Store the current term for comparison in the next iteration
set "prevTerm=%%a"
)

the code above started work for me properly with these few corrections ;)

Bash:
@echo off
setlocal enabledelayedexpansion

:: Initialize the variables
set "prevTerm="
set "tabs="

:: Read the file line by line
for /f "delims=" %%a in (textfile.txt) do (
  :: If the current term is equal to the previous term, add a tab
  if "%%a" == "!prevTerm!" (
    set "tabs=!tabs! "
  ) else (
  :: If the current term is not equal to the previous term, reset the tabs
    set "tabs="
  )

  :: Print the tabs and the current term
  echo !tabs!%%a
  :: Store the current term for comparison in the next iteration
  set "prevTerm=%%a"
)

textfile.txt (with example text inside as below)
Code:
Term 1
Term 1
Term 1
Term 1
Term 2
Term 2
Term 1
Term 1
Term 1
Term 3
Term 2
Term 2

tabs-in-content.png
 
Joined
Nov 23, 2023
Messages
59
Reaction score
3
Hello everyone,

Really not sure I'm in the right category for this since I'm really new at anything coding – and programming – related but I'm looking for a way to automate a task that is taking me forever to do by hand.

I'm a graphic designer and I'm working on a book collecting a lot of text entries (Twitter trending terms, for every hour over the course of the year 2022, so around 8,7k words). For design and legibility purposes, I would like to add a Tab symbol before a term anytime it is consecutively recurrent, and reset it anytime another term shows up. It would look something like this (with em-dashes instead of tabs here):

...
Term 1
—Term 1
——Term 1
———Term 1
Term 2
—Term 2
Term 1
—Term 1
——Term 1
Term 3
Term 2
—Term 2
...

Could anyone (try to) explain to me how would I go about writing such a script ?

For now, the text is stored in Excel cells because it is convenient to paste into InDesign tables.

Also, for the patient pedagogues amongst you, there are two caveats that I would assume could cause issues:
-Some of the terms are multi-word phrases or contain hashtags
-Each page of the book contains a day, so the script would need to hard-reset every 24 terms (24 hours) no matter what

Thank you for reading and I hope you have a nice day,

Nino from Brussels
Hello everyone,

Really not sure I'm in the right category for this since I'm really new at anything coding – and programming – related but I'm looking for a way to automate a task that is taking me forever to do by hand.

I'm a graphic designer and I'm working on a book collecting a lot of text entries (Twitter trending terms, for every hour over the course of the year 2022, so around 8,7k words). For design and legibility purposes, I would like to add a Tab symbol before a term anytime it is consecutively recurrent, and reset it anytime another term shows up. It would look something like this (with em-dashes instead of tabs here):

...
Term 1
—Term 1
——Term 1
———Term 1
Term 2
—Term 2
Term 1
—Term 1
——Term 1
Term 3
Term 2
—Term 2
...

Could anyone (try to) explain to me how would I go about writing such a script ?

For now, the text is stored in Excel cells because it is convenient to paste into InDesign tables.

Also, for the patient pedagogues amongst you, there are two caveats that I would assume could cause issues:
-Some of the terms are multi-word phrases or contain hashtags
-Each page of the book contains a day, so the script would need to hard-reset every 24 terms (24 hours) no matter what

Thank you for reading and I hope you have a nice day,

Nino from Brussels
 
Joined
Nov 23, 2023
Messages
59
Reaction score
3
Hello everyone,

Really not sure I'm in the right category for this since I'm really new at anything coding – and programming – related but I'm looking for a way to automate a task that is taking me forever to do by hand.

I'm a graphic designer and I'm working on a book collecting a lot of text entries (Twitter trending terms, for every hour over the course of the year 2022, so around 8,7k words). For design and legibility purposes, I would like to add a Tab symbol before a term anytime it is consecutively recurrent, and reset it anytime another term shows up. It would look something like this (with em-dashes instead of tabs here):

...
Term 1
—Term 1
——Term 1
———Term 1
Term 2
—Term 2
Term 1
—Term 1
——Term 1
Term 3
Term 2
—Term 2
...

Could anyone (try to) explain to me how would I go about writing such a script ?

For now, the text is stored in Excel cells because it is convenient to paste into InDesign tables.

Also, for the patient pedagogues amongst you, there are two caveats that I would assume could cause issues:
-Some of the terms are multi-word phrases or contain hashtags
-Each page of the book contains a day, so the script would need to hard-reset every 24 terms (24 hours) no matter what

Thank you for reading and I hope you have a nice day,

Nino from Brussels
In an adjacent column (let's say Column B), enter the formula =IF(A2=A1, "-", A2) in cell B2 (assuming your terms are in Column A). This formula checks if the current term (A2) is the same as the previous one (A1). If yes, it inserts a hyphen ("-"). Otherwise, it copies the term itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top