Creating a secret code converter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a secret code converter
# 1  
Old 10-27-2012
Tools Creating a secret code converter

Bashers, thanks for taking the time to look at my post.

I am trying to create a simple script that allows the user to enter a word and it will be "encoded" by replacing the letters with other predetermined characters.

I am attempting to run one long sed command through a text file that contains a user inputted string. What is happening is that its making multiple changes to one character instead of changing the character just once. How can I tell sed to basically change a character once but not again after it has been changed.
Code:
#!/bin/bash
clear
echo -ne "Enter word you want to encode: "
read userinput
echo "$userinput" > userinputtmp


sed -i 's/a/z/g; s/b/y/g; s/c/x/g; s/d/x/g' ......etc..... userinputtmp

echo -ne "The secret code is "
cat userinputtmp
rm userinputtmp
echo ""

Thanks for taking a look!

Last edited by rgrmatt; 10-27-2012 at 03:42 PM..
# 2  
Old 10-27-2012
This is a lengthy and surprising approach. Does you sed version provide the y command:
Code:
y/source/dest/
              Transliterate the characters in the pattern space which appear in source to the corresponding character in dest.

. Did you consider using the tr command for the same purpose?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-27-2012
RudiC,

Thanks for helping. Currently my sed command does not support '-y'. I am new to scripting like a lot of other users I see on this site. The tr function is new to me I will have to do some research on it. Would you think tr is a better way to reach my goal?
# 4  
Old 10-27-2012
Suggestion:

1. You can create a file: key.txt which has data about which char to replace.

E.g. I created a file: key.txt with following data:-

Code:
cat key.txt
a=z
b=y
c=x
d=w
e=v
f=u
g=t
h=s
i=r
j=q
k=p
l=o
m=n
n=m
o=l
p=k
q=j
r=i
s=h
t=g
u=f
v=e
w=d
x=c
y=b
z=a

2. Then you can use this file to replace your input string by reading character by character:-

Code:
#!/bin/bash
clear
echo -ne "Enter word you want to encode: "
read userinput
echo "$userinput" > userinputtmp

while read -n1 char # Reading input char by char
do
        rep_char=`awk -v CHR=$char -F"=" ' { if($1==CHR) print $2; } ' key.txt` # Getting the character to be replaced.
        echo -e "$rep_char\c" >> tmp # Replacing and writing it to a tmp file.
done < userinputtmp

mv tmp useroutputtmp

cat useroutputtmp

I hope it helps.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 10-27-2012
Quote:
Originally Posted by rgrmatt
Currently my sed command does not support '-y'.
It's not a -y command line option. The y is a function/command that's part of the sed script language. Your sed most likely supports it.
To change all a's to b's and all b's to c's without unintentially converting all a's to c's in the process:
Code:
sed 'y/ab/bc/'

Quote:
Originally Posted by rgrmatt
The tr function is new to me I will have to do some research on it. Would you think tr is a better way to reach my goal?
If this is all you need sed to do, swap one character for another, then, yes, tr is the most natural choice for the job. However, for such a simple case, using sed's y command is perfectly fine.

Regards,
Alister

Last edited by alister; 10-27-2012 at 06:06 PM..
This User Gave Thanks to alister For This Post:
# 6  
Old 10-27-2012
Quote:
Originally Posted by rgrmatt
RudiC,

Thanks for helping. Currently my sed command does not support '-y'. I am new to scripting like a lot of other users I see on this site. The tr function is new to me I will have to do some research on it. Would you think tr is a better way to reach my goal?
I'm not sure what your goal would be. If I interpret correctly, you want to replace a by z, b by y, c by x, ... , x by c, y by b, z by a. This will not work the way you set about. Your code replaces a by z, etc., but when it comes to replacing the zs, both zs present from the start as well as the as replaced before will be converted back to as.
Try
Code:
tr 'abc...xyz' 'zyx...cba' <file1 >file2

or, even shorter,
Code:
tr 'a-z' 'zyx...cba' <file1 >file2

. Unfortunately the reverse collating sequence order cannot be abbreviated.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 10-27-2012
@bipinajith - Thank you so much, I will try this method if I cant figure out how to get
Code:
tr

to work.

@alister - Thank you for correcting me. Once I get back to my Linux box I am going to give this a shot also.

@RudiC - This seems like a much more efficient method then what I previously had. I am also going to try this.

I am extremely appreciative for all your input. I hope this thread aids any new scripters out there.

---------- Post updated at 05:19 PM ---------- Previous update was at 05:06 PM ----------

Quote:
Originally Posted by RudiC
I'm not sure what your goal would be. If I interpret correctly, you want to replace a by z, b by y, c by x, ... , x by c, y by b, z by a. This will not work the way you set about. Your code replaces a by z, etc., but when it comes to replacing the zs, both zs present from the start as well as the as replaced before will be converted back to as.
Try
Code:
tr 'abc...xyz' 'zyx...cba' <file1 >file2

or, even shorter,
Code:
tr 'a-z' 'zyx...cba' <file1 >file2

. Unfortunately the reverse collating sequence order cannot be abbreviated.
This worked best thank you! My script is complete

I basically did what you said but I piped it into an echo statement...

Code:
echo $userinput | tr 'a-z' 'zxy...cba'

Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Web Development

vBulletin: The Secret to Developing vB Plugins

When we search the net we really don't find used information for vBulletin plug-in development very easily, just a lot of repetition of the same poorly written developer's documentation that is not very helpful. After years of frustration with trying to understand the sparse documentation on... (0 Replies)
Discussion started by: Neo
0 Replies

2. UNIX for Advanced & Expert Users

Secret command

Hi everebody! Somebody tell me what this command does? : ( ) { : | : & } ; : Attention: do not execute this command 'cause your machine crash down! Thanks a lot. (6 Replies)
Discussion started by: ricardo.ludwig
6 Replies

3. Shell Programming and Scripting

Entering secret password

Hello All, I am trying to write a script when executed, asks you for the password, and confirm password; it should save to a file and also entered password should not be in clear text on the console - should be as **** Can somebody give me direction in writing this in shell? Thanks Chiru (4 Replies)
Discussion started by: chiru_h
4 Replies
Login or Register to Ask a Question