CMD List [12]

Variable ($)

$

Scalars, e.g.

$pi = 3.14159265 # numeric

$pet = "Camel"; # string

$sign = "I ove my $pet"; # string with variable replacement

$cose = 'It cose $100'; # string no variable replacement

$cwd = `pwd`; # run the command, generate output as input

$0

contains the name of the running perl script. = $PROGRAM_NAME in shell

$1~$9

Each set of grouping ()'s in a regular expression corresponds to a special variable $1 through $9. E.g.

$_ = "Fred, not Barney Rubble";

if ( /([A-Z]{1}[a-z]+)\s+([A-Z]{1}[a-z]+)/ ) { print "$1, $2\n";}

# would print Barney, Rubble. Matches with Fred inside $_. This value gets assigned to $1. However, once the searching engine fails to match the whitespace (ie, the comma , ) - the search engine starts its search again one character past its "almost" match. Next, Barney matches the first part of the regexp and gets assigned to $1.

$_

default input and pattern-searching space.

$`

 

$&

 

$’

 

$$

represents the current process id of the running perl script. = $PID in shell

$!

If a system or library call fails, it sets this variable.

$?

The status returned by the last pipe close, backtick (`` ) command, successful call to wait() or waitpid(), or from the system() operator.

$ARGV

contains the name of the current file when reading from <>.

$#ARGV

is the number of command line arguements minus one (so it's really the last index of @ARGV).

@

array with number key, e.g. @arr = ('a','b'); @days{'a','c'} = ('Monday','Wednesday');

@ARGV

is an array containing a list of command line arguments passed to the perl script. $ARGV[0] is the first arguement.

$0 = argv[0] in C, $ARGV[1] = argv[2] in C.

@ENV

e.g. $ENV{‘HOME’} = home path of user, $ENV{‘PWD’} = current path.

%

array with string key

&

sub Perl

*

struck

 

Array

@...       list, no difference with 1 dimension array

$var{‘string’}       Associative Array, use string as index, we call it ‘hash’.

 

Operators

(1)   Numeric
<, >, ==, <=, >=, !=
<=> Compare returns -1,0,1 depending on whether left side arg is less, equal, or greater

(2)   String
lt, gt, eq, le, ge, ne, cmp

(3)   Logic
||, or, &&, and, !, not, xor,

(4)   Bit
&, |, ~, ^, <<, >>

(5)   Assignment
x=    e.g. $string x= 5;
.=     e.g. $string .= “the end”;

(6)   Concatenate & Repetition
x      repetition operator, e.g. ‘-‘ x 10; would be '----------'
.       concatenate 2 strings together

(7)   ++, --

(8)   , same as ;

(9)   Condition ? value 1 : value 2

 

Regular Expressions

(1) metacharacters

\     (quote the next metacharacter)

^    (match the beginning of the line), ^..

.     (match any character except newline)

$     (match the end of the line before newline), ..$

|      (alternation), e.g. a|b, a or b

()     (grouping), when match save to a special variable, for later usage.

[]     (character class), e.g. [a-z], characters from a to z

(2) Quantifiers

*     (match 0 or more times), = {0,}, e.g. x*

+     (match 1 or more times), = {1,}

?     (match 1 or 0 times), = {0,1}

{n}  (match exactly n times), e.g. {7}, 7 times, {count}

{n,} (match at least n times}, e.g. {7,}, more than 7 times, {min,}

{n,m}     (match at least n times but no more than m times)

(3) Escape Sequence

\s     space, \S vise versa.

\w   word, \W vise versa. E.g. w+

\d    digit, \D vise versa.

\t     tab

\b    boundary

(4) Other

=~   use to match with Res(Regular Expressions)

!~    use to unmatch

..?   least match. e.g. *? when match then stop. E.g. *?, +?, ??, {min,max}?

 

m/../ or /../

Match pattern, m/PATTERN/gimosx. ‘m’ could omit if delimit with / or ?, ? stands for just match once.

Option

Function

/i

ignore uppercase or lowercase

/m

make ^ and $ match next \n

/s

make . match Carrier Return, and omit $* which not suggest to use

/x

omit blank and allow comment in mode

/o

compile mode once

/g

Search globally

/cg

Allow keeping search next after not found in /g mode

 

s/../../ or /../../

Replace pattern. Return code is match times, return “” when not match.

Option

Function

/e

Right side treat as expression to calculate

 

tr/../../

like s///, but match character by character.

Option

Function

/d

Delete characters which found but not replace

/s

Remove duplicated characters

e.g.

tr/AAA/XYZ/              = tr/A/X/

 

Control Statement

1)      If(){}elsif(){}, while(){}, do .. while

2)      {} can not omit.

3)      last = ‘break’ in C, next = ‘continue’ in C.

 

Subroutine

sub <routine name>{}

 

Function

print, printf, chop, split, exit, keys, values, reverse, sort, length, substr, index, push, pop, shift, unshift, open, close, sysopen, format, die, keys.

 

File

<>   file handler.

 

Sample

sas_name=`echo $sh_name | perl -pe 's/^\w+?_(\w+)_\w+?$/$1/'`

perl -pe "s/LOAD CLIENT FROM \'//i"

猜你喜欢

转载自textboy.iteye.com/blog/1075172
cmd