1

I have a text with random alphabetic characters and I want to change every character to his opposit one. ex. i have a character z, i change it in to a, b to y etc. I can't really find a better way to do this unless i do

sed -r -e 's/a/z/' -e 's/b/y/' ... 's/z/a/'

Is there a way to do this in a more simple way?

I just want to use the -r option in sed. Using the y command maybe?

Filip
  • 183
  • 9
  • Is this a homework assignment or something? A [nearly identical question](https://stackoverflow.com/questions/55068264/unix-sed-command-reversed-alphabet) was submitted just an hour ago. BTW, they're both dups of [How can I get sed to change all of the instances of each letter only once?](https://stackoverflow.com/questions/29886943/how-can-i-get-sed-to-change-all-of-the-instances-of-each-letter-only-once). – Gordon Davisson Mar 08 '19 at 19:13
  • Yes it is, looks like we have trouble finding it. I think the solution is sed 'y/abcdefghijklmnopqrstuvwxyz/zyxwvutsrqponmlkjihgfedcba/' – Filip Mar 08 '19 at 19:36

3 Answers3

2

tr is easier, e.g. for lowercase chars

$ z_a=$(echo {z..a} | tr -d ' '); echo adfa alfja | tr a-z $z_a
zwuz zouqz

the detour to create z-a is required since tr can't handle "reverse collating sequence order".

karakfa
  • 66,216
  • 7
  • 41
  • 56
0

I know this isn't a sed solution, but this seems a simple, straight forward use of perl:

cat input_file | perl -ple 's/(.)/chr(25-ord($1)+ord("a")*2)/eg'
Tanktalus
  • 21,664
  • 5
  • 41
  • 68
  • 1
    Just nitpicking, but this is a [UUoC](https://stackoverflow.com/q/11710552/3282436) (Useless Use of Cat). – 0x5453 Mar 08 '19 at 19:01
  • yes, but I didn't know where the input was really coming from. It could be echo, or whatever, so I just left it alone. – Tanktalus Mar 08 '19 at 19:37
0

sed comes, one way is like this:

sed -r 'y/'"$(echo {a..z})"'/'"$(echo {z..a})"'/' file
Til
  • 5,150
  • 13
  • 26
  • 34