tags:

views:

69

answers:

4

I need to write a script to show me all the alias I've set in certain config files like .tcshrc and some private script in case that I forget the meaning of alias like "a" , "b" , etc . the format of the aliases in the config file is either alias a "content of alias a" or alias b 'content of alias b' .

the code I wrote is as below :

#! /usr/bin/perl
open ( F , "<" , ".tcshrc" ) or die "can't open it; $! " ;

while ( <F> ) {
if ( /\b(alias\s+\w+\s+[\'\"][^\'\"]*[\'\"])/ ) {  
        print $1 ;
}

but the code doesn't work. So could any of you have a look at the code and tell me what's wrong with the reqex?


@Karel Bílek Yes, I missed the backslash at the second s, now it worked. but I'm still interested to know whether there is a better way to write the regex.

@Charles the lines I want to match is like

alias a 'ls -l'
alias b  "rm *"
+1  A: 

just quick answer without testing it (or looking too much at in) - aren't you missing backslash before the second s?

Karel Bílek
+2  A: 
#! /usr/bin/perl
open ( my $f , "<" , ".tcshrc" ) or die "can't open it; $! " ;

while ( <$f> ) {
if ( /\b(alias\s+\w+\s+['"][^'"]*['"])/ ) {
        print $1 ;
}
Ether
+5  A: 

You may not need a script for this. In tcsh, you can just execute alias without any arguments and it will list all alias definitions. Or to look up a particular alias just run alias [name] where NAME is the name of the alias.

For example:

$ alias
a   ls -l
b   rm *
$ alias a
ls -l
$ alias b
rm *

If you want to go the other way to, say, remember which aliases map to ls, you can use grep for that:

$ alias | grep ls
a   ls -l

This also works in other shells like bash and zsh.

Ryan Bright
+1  A: 

Don't you just want the grep some files? This seems to do what you want:

% grep -h alias ~/.*

Are you doing something more fancy with this script?

brian d foy