Creating a secret code converter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a secret code converter
# 15  
Old 10-31-2012
Code:
$ cat key.txt
a=AE
e=AA
i=AU
o=AI
u=AO

$ echo "Testing" | awk -F= 'NR==FNR{R[$1]=$2;next}
{for(i=1;i<=length;i++) {
   if(substr($0,i,1) in R) printf R[substr($0,i,1)]
   else printf substr($0,i,1)
  }
  printf "\n"}' key.txt - 
TAAstAUng

This User Gave Thanks to Chubler_XL For This Post:
# 16  
Old 10-31-2012
Quote:
Originally Posted by rgrmatt
I had problems with 'sed' when I started the original script. The issue I ran into was that sed was making changes to characters that were the results of a previously executed sed.
This is indeed a problem. It is possible, to use a two-step approach by "protecting" the characters you have already changed:

Suppose, you want to change "x" to "yz" and "y" to "ab" - you will have to distinguish between the "y"s from the original text and the "y"s from your first translation. Simply append or prepend a character you don't use normally to signify already translated characters. Basically this is called "escaping" and traditionally the backslash "\" is used for that.:

"x" -> "\y\z", "y" -> "\a\b"

You can now refine your search string to search and translate only for characters not prepended by a "\":

"\x" -> "\x", "x" -> "\y\z", "y" -> "\a\b", "\y" -> "\y"

Of course you have to finally - as the very last step - to remove all the backslashes. Now that in sed code, which uses "\" for escaping too - even for escaping "\" themselves:

Code:
sed 's/^x/\\y\\z/
     s/[^\]x/\\y\\z/g
     s/^y/\\y\\z/
     s/[^\]y/\\a\\b/g
     ... <more translations here>...
     s/\\//g' /path/to/inputfile

As you see you need two rules for every character you want to translate: one for the character at the beginning of a line and one for everywhere else.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
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