Get each line out of a text to perform a certain action

Hi;
Sorry,I know this has been a topic here but my issue is a little different and since the solutions are too complicated for my KM knowledge I was not able to adapt them to my topic.
So this is what I want to achieve:
Read a text file and perform an action for every line (basically copy and paste it)
If the line falls below a certain amount of letters (for example 4) copy also the previous line/next line
until the end of the Text-File.
Thanks in advance,
Simon

Which (or both)? And what happens if the first/last line is shorter than 4 characters?

It's this requirement that makes things trickier -- you're no longer simply "For Each"ing one line at a time, you now have to also keep the previous line in case it's needed. And how are these lines to be joined -- with a space between them? Something else?

You'll also want to do something else -- "contents of each line" does not include the linefeed at the end, so at the least you'll probably want to include a linefeed.

Hi,
Thanks for coming back to me.
Exactly, the current line should be in relation to the previous and next line (if needed).
It's a txt-file where each line is a new line divided by a linefeed.

In relation how? If you have the file:

abc
some more text
123
another long line
abc

...what should the resulting output or the sequential paste operations be? Perhaps

abc some more text
123 another long line
abc

...or maybe

abc
some more text 123
another long line abc

...or something else?

Something like 'current line' less than 4 letters check the next line.(Actually by thinking it through the next line is more important than the previous one)
A little more detailed what I want to achieve:
My editing software contains a script sync option meaning audio is being transcribed and can be located by a text prompt.
I would like to mark and nummerize every script line.
(1st line = marker #01; 2nd line = marker #02...)
Now it could happen that lines are not unique (for example imagine the line in the script is 'Yes')
In those cases the next line should be added to the copy paste routine to get a more specific result.

So like in the first example of yours...

Then you don't need a "For Each" -- you can do everything in one go with a regular expression, searching for:

(?m)^(.{0,4})\R(.*)$

...and replacing with:

\1%Space%\2

Example:

Combining Lines.kmmacros (3.3 KB)

You could then "For Each" through the new collection of lines to add your numbered prefix. Or get clever with a shell script action:

image

1 Like

Thank you, 'Shell' is a mystery to me.
I understand that the first part is formatting the script to my needs.
What does the second part do?
Could you provide the shell code in text? I tried to identify it out of the image but could not get it to work.
Overall I still didn't find a way to seperate each line to do the action, meaning
line#1 do the action
then
line#2 do the action
until the end of the script.

Regex Explanation

Pattern: `(?m)^(.{0,4})\R(.*)

  • (?m): Enables multiline mode, so ^ and ` match the start and end of each line.
  • ^: Matches the beginning of a line.
  • (.{0,4}): Captures between 0 and 4 characters at the start of the line.
  • \R: Matches a line break (newline \n, carriage return \r, or both \r\n).
  • (.*): Captures the rest of the line after the line break.
  • $: Matches the end of the line.

Replacement: \1%Space%\2

  • \1: Inserts the first captured group (0-4 characters at the beginning).
  • %Space%: Adds a literal "%Space%" between the two parts.
  • \2: Inserts the second captured group (the remaining part of the line).

Could you write out exactly what you are trying to accomplish with a manual example of input and then the output with steps. Are you trying to just copy all the text and have things parsed from a clipboard to be able to paste or once it find the first four characters then combine with the next.

It sounds like you have a list of things that are being pasted weird like

1234
A bunch of text here and you would like this to now be.
567
And a new line and you want to keep the lines separate if they are longer than 4 characters.
890
But you also want a space after the line above with a space after it and combine to the next line even if it wraps around so you are looking for less than 4 characters befor a carriage return.

Becomes

1234 A bunch of text here and you would like this to now be.
567 And a new line and you want to keep the lines separate if they are longer than 4 characters.
890 But you also want a space after the line above with a space after it and combine to the next line even if it wraps around so you are looking for less than 4 characters befor a carriage return.

Hi Skillet,
Thanks for the brief dive into Shell. Very interesting yet complicated!
As mentioned above my editing software contains a script sync option meaning audio is being transcribed and can be located by a text prompt.
My goal is to mark my material into beats, script lines.
For that reason I format a scene of a script into the dialogue lines.
Every line is one beat.
The workflow should be
Read the txt-File
Copy the first line of the txt-File and paste it into the Find-Window of Media Composer.
Go through the list of found results, mark and name them ('#01')
When finished take the second line of the txt.-File paste it into the Find-Window and proceed as mentioned (this time with marker '#02')
Do this routine until the end of the txt-file.

In order to have only unique results short lines of dialog within the txt-File (under 4 letters) should be clustered with the next line. This is the shell-part that already works.
Hope you get it.
Cheers

Media Composer - Text File to Beat Macro (v11.0.3)

Media Composer - Text File to Beat.kmmacros (39 KB)

You can try this see the note in the macro. I would probably just assing a keyboard shortcut to the second part of this and run one by one instead of trying to automate the entire process. I have tried things like that many times without consistent success. It might work on your computer for a while as things are in RAM and loaded smoothly then you try later or on another computer and issues.

Change Pro Tools in this to Media Composer (I don't have Media Composer on this computer anymore), I also did the wait for apps and then a delay because there usually still seems to be a need for a further delay once it is in front.

Hopefully this gets you closer to what you are after.

1 Like

Note that there's a $ missing from the end, and in the last part of your description (possibly from not closing the code block in that first line).

1 Like

Thanks for catching that, I just updated it.

2 Likes

Wow, Thanks so much!
I'm on the road but will test it tomorrow.
Awesome!

1 Like

Hopefully, it, at a minimum, gives you ideas and gets you in the ballpark.

1 Like