注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

lazydba

hello

 
 
 

日志

 
 

Less is more | csh, perl and others  

2008-03-26 17:03:09|  分类: script |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Conventions
code and command
?
1. Variable in csh
set a = "this is a variable"
echo $a
unset $a
echo $a
?
2. Environment Variables in csh
setenv b "this is a environment variable"
echo $b
unsetenv $b
echo $b
?
3. Loop in csh
foeach file (*.log)
???? echo $file
end
?
4. Alias in csh
alias rm rm -i
rm *.log
\rm *.log
unalias rm
?
5. Using perl to change lower case to upper case:
echo 'this is a test' | perl -wpl -e 'tr/a-z/A-Z/'
THIS IS A TEST
?
6. Change the first letter of a word to upper case:
echo 'this is a test' | perl -wpl -e 's/(\w+)/\u$1/g'
This Is A Test
?
7. Using s str to get a part of a string:
echo 'bad.log.20080325' | perl -wnl -e 'print(s str($_, 0, index($_, ".")));'
bad
echo 'bad.log.20080325' | perl -wnl -e 'print(s str($_, 0, rindex($_, ".")));'
bad.log
7.1 Get the path of a file
echo '/path/to/file/bad.log.20080325' | perl -wnl -e 'print(s str($_, 0, rindex($_, "/")));'
/path/to/file
7.2 Get the filename without path
echo '/path/to/file/bad.log.20080325' | perl -wnl -e 'print(s str($_, 1 + rindex($_, "/")));'
bad.log.20080325

8. Access shell environment variable in perl:
export bash_env='environment variable in bash'
echo | perl -wnl -e 'print $ENV{bash_env};'
environment variable in bash

9. Pass variable from perl to sh variable:
a=`echo 'assign to sh variable' | perl -wpl -e 'tr/a-z/A-Z/'`
echo $a
ASSIGN TO SH VARIABLE

The following sample using?emp_salary.dat as a test file.
cat emp_salary.dat
Name, Salary
tom,? 100
jack, 200

10. perl as cat
perl -wpl -e '' emp_salary.dat

11. perl as grep
perl -wnl -e '/tom/ and print;' emp_salary.dat
11.1 perl as grep -v
perl -wnl -e '/tom/ or print;' emp_salary.dat

12. perl as sed:
perl -wpl -e 's/^(\w+)/\u$1/' emp_salary.dat
Name, Salary
Tom,? 100
Jack, 200
12.1 write back to emp_salary.dat
perl -wpli.bak -e 's/^(\w+)/\u$1/' emp_salary.dat

13. perl as awk
13.1 print salary without emp name.
perl -wnlaF',' -e '$. > 1 and print $F[1];' emp_salary.dat

13.2?Calculate the?average salary of all emps and print it:
perl -wnlaF',' -e '
?BEGIN {$sum=0;}
?$. > 1 and $sum += $F[1];
?END{print $sum/($.-1);}' emp_salary.dat

Many of the ideas are?learned from the book <Minimal Perl For UNIX and Linux People>(http://minimalperl.com/)
It's a interesting reading.

14. eval (bash and csh)
eval "ls -l"

Reference:
$ perl -h

Usage: perl [switches] [--] [programfile] [arguments]
? -0[octal]?????? specify record separator (\0, if no argument)
? -a????????????? autosplit mode with -n or -p (splits $_ into @F)
? -C????????????? enable native wide character system interfaces
? -c????????????? check syntax only (runs BEGIN and CHECK blocks)
? -d[:debugger]?? run program under debugger
? -D[number/list] set debugging flags (argument is a bit mask or alphabets)
? -e 'command'??? one line of program (several -e's allowed, omit programfile)
? -F/pattern/???? split() pattern for -a switch (//'s are optional)
? -i[extension]?? edit <> files in place (makes backup if extension supplied)
? -Idirectory???? specify @INC/#incl? directory (several -I's allowed)
? -l[octal]?????? enable line ending processing, specifies line terminator
? -[mM][-]module? execute `use/no module...' before executing program
? -n????????????? assume 'while (<>) { ... }' loop around program
? -p????????????? assume loop like -n but print line also, like sed

? -P????????????? run program through C preprocessor before compilation
? -s????????????? enable r imentary parsing for switches after programfile
? -S????????????? look for programfile using PATH environment variable
? -T????????????? enable tainting checks
? -t????????????? enable tainting warnings
? -u????????????? dump core after parsing program
? -U????????????? allow unsafe operations
? -v????????????? print version, s version (incl?s VERY IMPORTANT perl info)
? -V[:variable]?? print configuration summary (or a single Config.pm variable)
? -w????????????? enable many useful warnings (RECOMMENDED)
? -W????????????? enable all warnings
? -X????????????? disable all warnings
? -x[directory]?? strip off text before #!perl line and perhaps cd to directory

Perl function specification:
perldoc -f function_name


  评论这张
 
阅读(95)| 评论(2)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017