Reverse Word Order of Lines in the Clipboard

I think you're saying my solution doesn't work because it doesn't have a comma in the result. It's hard to tell what you're saying because you don't say why you object to my solution. All you say is that "it doesn't perform the job". I'm going to have to guess why you are objecting to my solution, and I'm guessing that you are objecting due to the lack of a comma. Or maybe you are objecting because I didn't use the clipboard.

Therefore, you win. I didn't use the clipboard and I didn't insert a comma. To be honest, I just didn't think those elements of the OP's questions were important or part of what the OP was really trying to solve. And it sounded to me like the OP could handle those things on his own. Maybe I was wrong.

You also put the word code in italics, which tells me you object to me using that word. Okay I retract that word since it's making everyone object today. Just judge my "code" on its simplicity and beauty.

Since I can't be sure why you are objecting to my solution, I'll show you what my solution actually does. This looks to me like it's reversing the names. If you look closely you will see a leading space on lines 2 & 3. In my mind I didn't think that would be a problem for the OP. Just like you didn't use the clipboard in your three solutions because you didn't think that was the problem, even though the OP stated the clipboard clearly.

> cat file1 | rev | sed 's/$/ ~/' | tr ' ' '\n' | rev | tr '\n' ' ' | tr '~' '\n'
> Washington George 
>  Lincoln Abraham 
>  Rosevelt Teddy 

These are the results. They stand for themselves. The names are reversed. But you are right my solution fails on many counts:

  1. My solution is "code" even though I claimed it wasn't.
  2. My solution failed because it didn't generate a comma.
  3. My solution failed because it generated a leading space.
  4. My solution failed because it didn't post the result in the clipboard.

If any website moderator wants to delete my posts because my solution "doesn't perform the job", that's okay with me and I won't complain ever.

Hey @Sleepy,

My mistake, this code does work, although the names are not delimited by comma-space.

echo "$dataStr" | rev | sed 's/$/ ~/' | tr ' ' '\n' | rev | tr '\n' ' ' | tr '~' '\n'

I tested in a BBEdit Shell Worksheet and somehow the first pass failed. (I assume a user-error of some kind.)

-Chris

Oh, it looks like our posts crossed in the mail. That's unfortunate. That would have cut down my response quite a bit if I had seen your last post first.

Try and get into the habit of doing this as standard practice. Whenever you post a solution such as the ones you did, it is really helpful (especially to those who don't script) to have a brief run down of what is happening, and then an example of it in action, with the input, the implementation if required, and then the result.

When you explained to me step-by-step what was happening, it was a lot clearer for me.

Yup. I gave it a try and it worked. It's a creative solution. I'm pretty impressed at the idea behind it and it certainly isn't something I would have thought up. It does, as you said, preclude one from having a tilde appear in the file, but I presume the tilde in the command expression can be replaced with any character, provided it doesn't appear in the file ?

Did you know you can do this to reverse the order of lines in a file:

tail -r /path/to/file.ext

I can sense that you felt perhaps personally slighted, and possibly dismissed by the (incorrect) conclusion that was made about your first shell snippet. I can empathise, as I'm sensitive as well and often interpret things negatively at first. But you're going to get feedback when you submit anything, and it won't always be good. Just don't let it discourage you from submitting more, as I for one quite enjoy your unique way of problem-solving so hope to see more. Try not to get defensive, though. We all sound a lot more abrupt and matter-of-fact when we type, and it's easy to misconstrue brevity or a formal tone as an insult or rudeness. I don't know where you're from, but I find I'm always pissing off Americans for some reason, and think it's my Britishness that favours being indirect in how I say things (which we are raised to view as politeness), whereas I sometimes feel jolted by the directness that Americans seem to favour, and then remember they're not being aggressive, they're just being American.

Anyway, I'm looking forward to your next lightbulb and unique solution.

1 Like

That's a very kind and generous post. You could be a psychologist with that kind of thinking. You were probably pretty accurate in your analysis. Although it has always been one of my techniques in life to criticize myself online, that way when people try to disagree with me they are agreeing with me. :slight_smile: And sometimes it works.

You are right about the tail -r, and I came across that during my research to do word reversals. But the real reason I mentioned it, I think, was because it was a stepping stone to achieving the new method I developed for reversing words in lines. I thought it would benefit people to understanding how my word reversal filters worked. I think that's why I mentioned it.

But a lot of solutions to these problems ended up using commands and parameters that simply aren't available in macOS. My solution uses only the most basic functionality in rev, tr, and sed. In fact you have no idea how hard it was for me to work around the bug in macOS that this doesn't work:

sed 's/\n/~/'

From what I can see macOS is the only version of UNIX that doesn't recognize \n as a search code in sed. That probably took me an hour to work around.

P.S. I'm Canadian. I don't reveal more than that about myself however.

Thanks. The next one that I planned to post was a dozen or so Macros that implement all the functions that one does with "sets", like set intersections and unions, and cross-products, using the lines of a text variable as the members of the set. But I got a little distracted with all these other posts today.

1 Like