1

How can I reverse

"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

so that it would be something like

"ZYXW..."

I know it's a fairly basic question, but the help would be appreciated :)

12
  • 2
    It is not clear what you are asking, from what you have said, I would say just finish typing it. However I as sure this is not what you mean. May be see command tr. But I can't give any more advice until I know what you are trying to do. May 7 '20 at 21:54
  • 5
    What have you tried so far? Why wouldn't echo "ZYXW..." be acceptable? Please edit your question to add more detail.
    – roaima
    May 7 '20 at 21:57
  • 2
    1/2 @kangalioo there are different ways to achieve the question that's been asked. The most trivial one is echo "ZYX...BCA". Another might be to pipe into tr '[A-Z]' 'ZYX...CBA'. (And actually that's a really bad solution because could easily break, depending on the locale.) And so on. With more context it may be possible to offer a better solution.
    – roaima
    May 8 '20 at 10:59
  • 2
    2/2 @kangalioo Secondly, on this Stack people are encouraged to post what they have already tried, even though it doesn't work.Often, explaining why an attempt does not work - and therefore what does work instead - is really helpful to the person asking the question. Not only do they get an answer, they have hopefully learned something too.
    – roaima
    May 8 '20 at 11:01
  • 2
    Perhaps it would help to explain the question's intent by asking: What output is desired if the input is (say) "HELLO"? (Both "OLLEH" and "SVOOL" seem good matches for the question as it currently stands, though there are several other possibilities.)
    – gidds
    May 8 '20 at 11:21
10

The rev command will reverse strings:

echo ABCDEFGHIJKLMNOPQRSTUVWXYZ | rev

produces output

ZYXWVUTSRQPONMLKJIHGFEDCBA
3

Assuming you want to print a reversed string.

echo -n "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | tac -rs '\w'

will produce:

ZYXWVUTSRQPONMLKJIHGFEDCBA
6
  • echo 'ABC DEF' | tac -rs '\w' produces FE DCBA with a newline before and none after, rather than FED CBA here. Too difficult for me to guess this shifted space, but I'm not sure that's what OP would want.
    – A.B
    May 7 '20 at 23:06
  • This will place the newline at the start of the string.
    – IsaaC
    May 7 '20 at 23:20
  • My comment was not an answer to your comment, it just happens that my comment came after yours. You comment is spot on, no issue from me. @A.B
    – IsaaC
    May 7 '20 at 23:24
  • echo -n "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | tac -rs '\w' will get rid of the newline. Thanks, I'm correcting the solution above.
    – Uggla
    May 8 '20 at 0:00
  • 2
    If you replaced | tac -rs '\w' with | tac -rs '.' this would fix the space issue.
    – A.B
    May 8 '20 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.