Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Machines at work follow a constant naming pattern:

host[domain]_[id]

where [domain] is either "d" for dev machines and "p" for production machines, and [id] is a 2-digit string that is different for each machine. For example: 'hostp_a1' or 'hostd_g5'.

Now due to new company policies, we have separated the two domains and we have to append a suffix ".dev" for dev machines and ".prd" for production ones. My previous machines are now called 'hostp_a1.prd' and 'hostd_g5.dev'

I don't want to change my old habits, so I am wondering if there's a way for Ksh to do this for me dynamically. Here's a sed script that would do it for me:

sed -e 's/host\([dp]\)_\(..\)/host\1_\2.\1/' -e 's/p$/prd/' -e 's/d$/dev/'

Here are some solutions I thought of:

  • Regex alias:

    If I could do something like alias host[dp]_..='.....

  • Triggers:

    The trigger would execute the sed each time a command is entered. Might hurt performance a bit, but I don't care since it's only interactive.

Is there any way Ksh could do that? It probably doesn't help that I'm using an old version Version M-11/16/88i. Can maybe other shells do this?

(as long as we're at it, I'm currently learning sed so if anyone has a better sed command to suggest, please do. Thanks).

share|improve this question
1  
I'm not sure I understand what you're trying to do. Do you want hostd_g5 to be systematically rewritten to hostd_g5.dev wherever you type it? Even if you typed ls notes/hostd_g5.txt? Or are you interested in specific network-related commands? Or do you want that whenever the string is looked up in DNS? –  Gilles Sep 30 '11 at 23:29
    
To be honest, my prime interest is to explore the shell's features. Obviously, the second and third solutions you suggest are far more interesting, but are they doable? As you can see from the tone of my question, I was merely brainstorming because I know very little of what shells are really capable of. So far, I have replaced "common" commands (rlogin, ssh, rsh) with functions that translate machine names if given. But is there a way for a shell to do more? I also exposed the situation hoping that Unix 'gurus' would expose their ways of solving such a problem. :) –  rahmu Sep 30 '11 at 23:51
    
there is no substitution at this level. Alias is for 1st word of the command (so also after a ;). This is not a variable or function name (unless your define 1 function per server name) and ther is no instruction to process any modification before execution –  NeronLeVelu Nov 27 '13 at 16:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.