Reformat a List of Words

I am having trouble understanding why a simple macro I wrote just seems to not work. I am feeding it a list of words that I want reformatted into a comma-separated line. The problem is sometimes the words are separated by more than one comma and sometimes whitespace.

Here's the simple macro:

In the second Execute Shell Script, I have tried all of the following (and more) yet the return of both the before and after are always the same. It's like the macro simply skips this command. I have also tried to place five-second pauses everywhere. Nothing helps.

sed 's/,\s*,/,/g'
sed 's/,\s*,/,/g; s/,\+/,/g'
sed 's/,\s\+/,/g'
tr -s ','
tr -s ',' | sed 's/,\s*/,/g'
awk '{gsub(/,\s*/, ","); gsub(/,+/ , ",")} 1'
sed 's/,\s\+/,/g; s/,\+/,/g'

Here's the macro:
Change Vertical Word List to Comma Separated Line.kmmacros (6.1 KB)

I have troubles with regex, so I created a simpler solution for you that reformats the first two parameters into comma separated parameters. If this isn't what you want, tell me what's wrong with it and I can probably fix it.

image

Thanx, Airy... That's great. The problem is sometimes the duplicated commas are between different words and sometimes there are a different number of words, although always less than ten.

I'm not sure I completely understand the request, but here's how I interpreted it:

Take the provided text, and remove anything that's not a character, leaving just words separated by commas on one line.

If that's correct, then this seems to work:

Vertical Words to Horizontal Line.kmmacros (4.2 KB)

The first regex replaces any set of one or more non-characters with a comma. The second regex trims any commas that were added at the start or end of the line due to the first regex.

Is that what you're trying to get to?

-rob.

I'm sorry I missed that point. Your example text didn't include any commas, which is probably why I missed that. But I think that's easy to fix.

Results in:

2, Snow Man Frosty
3, Ice
4, Rain

That sounds more like what you want.

My apologies for not being very clear. I should have sent a better example. Also, I sent the wrong macro. I guess I was in too much of a hurry.

Regardless, I think I can use Rob's response... I didn't even think about using Regular Expression.

So that you know, this below is what I was having trouble with:

Input would be something like this:

Snow,    ,    Rain,    Ice,    thunder,
Snow,    ,    Rain,    Ice,,   thunder,
Snow,  , ,    Rain,    Ice,    thunder,,

and I wanted to get it like this:

Snow, Rain, Ice, thunder
Snow, Rain, Ice, thunder
Snow, Rain, Ice, thunder

I just had to change Rob's ^,?[.?]$ to ^,?$ and I got there.

Thanx to both of you.

If you're happy, I'm happy, but for future consideration, the command that would work using shell commands is:

tr ',' ' ' | tr -s ' ' | tr ' ' ',' | sed 's/,$//;s/,/, /g'

It replaces all commas with spaces. Then squishes all spaces to a single space. Then replaces all spaces with commas. Then removes all trailing commas and also adds a space after each comma.

Like regex, shell commands have been around a long time and are very powerful.

1 Like

Thanx Airy. I already did something similar, but yours is much cleaner and better. I appreciate the lesson.

And, of course, splits and filters etc can also ease the burden of regular expressions, if you have a scripting language to hand:

Here, for example, we may only need \W+ for word delimiters.

Normalised rows of comma-separated words.kmmacros (2.3 KB)

1 Like