Perl指令碼 雜記
Perl指令碼 雜記
[@more@]Perl指令碼:
Perl chdir函式2010年10月29日 星期五 下午 16:24用法:
chdir EXPR
chdir介紹:
如果可能,chdir函式改變當前程式的工作目錄到EXPR。如果省略EXPR,即返回撥用者的根目錄。成功時返回真,否則返回假。
例子:
假設根目錄/home/zhenglc:
#!/usr/bin/perl
chdir "/usr/home";
# Now you are in /usr/home dir.
chdir;
# Now you are in home directory /home/zhenglc又或者是用perl的單行命令:
perl -e 'chdir; print `pwd`'
#輸出:/home/zhenglc
perl -e 'chdir "/home/zhenglc/temp/"; print `pwd`'
#輸出:/home/zhenglc/temp
##pwd是shell命令。返回當前的路徑。chdir函式相當於Linux的命令cd。
#!/usr/bin/perl
use strict;
use warnings;
my $dir_out = "/data4/nba_show_traffic/";
system "mkdir","-p",$dir_out;
my $dir = "/data4/nba_sw_int_traffic/";
chdir $dir || die "Can't change dir $dir $!n";
my @files = glob ("*"); #陣列變數將儲存該目錄下所有檔名,除了以點號開頭的隱藏檔案.
foreach my $file (@files)
{
$file =~ m/(d{1,3}.d{1,3}.d{1,3}.d{1,3})/;#匹配任意ip地址,m是匹配,match,這個地方沒有做容錯,程式設計者知道肯定是能匹配上的。
print "$1n";#列印出匹配上的那個ip地址
my $f_out = $dir_out.$file;
open FH,"
open FH_OUT,">",$f_out || die "Can't open $f_out $!n";
while(
{
if(/GigabitEthernet0/1/)
{
print FH_OUT $_;
}
}
close FH_OUT;
close FH;
}
Perl中index的用法,
Introduction
The index() function is used to determine the position of a letter or a substring in a string. For example, in the word "frog" the letter "f" is in position 0, the "r" in position 1, the "o" in 2 and the "g" in 3. The substring "ro" is in position 1. Depending on what you are trying to achieve, the index() function may be faster or more easy to understand than using a regular expression or the split() function.
Example 1a. Where in a string is a certain letter?
Let's say you are trying to determine if a word has a particular letter in it. Let's look for the letter "l" in the string "perlmeme.org".
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'l';
my $result = index($string, $char);
print "Result: $resultn";
This program gives you:
Result: 3
Example 1b. The string doesn't contain the letter?
If the string doesn't contain the letter, index() will return a -1. For example, we can look for the letter "L" in the string "perlmeme.org":
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'L';
my $result = index($string, $char);
print "Result: $resultn";
The program outputs:
Result: -1
Example 1c. The string contains more than one of the letter?
If the letter we're searching for appears more than once in the string, index() return the index of the first occurrence of the letter.
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'e';
my $result = index($string, $char);
print "Result: $resultn";
This program gives you:
Result: 1
Example 2. Looking for a substring
Looking for a substring (rather than a single character) works in exactly the same way as looking for a single character:
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $substr = 'me';
my $result = index($string, $substr);
print "Result: $resultn";
This program gives you:
Result: 4
Example 3a. What if I don't want the first occurrence?
The index() function let's you specify an offset. This tells index() where to start looking in the string. In our example, we found the first occurrence of the letter 'e' at position 1. Let's try to find the second:
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'e';
# The first 'e' was at position 1, so let's start
# looking again at position 2
my $offset = 2;
my $result = index($string, $char, $offset);
print "Result: $resultn";
The program outputs:
Result: 5
Example 3b. How to find every occurrence
To find (and do something with) every occurrence of a character in a string, you could use index() in a loop, incrementing the offset each time:
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'e';
my $offset = 0;
my $result = index($string, $char, $offset);
while ($result != -1) {
print "Found $char at $resultn";
$offset = $result + 1;
$result = index($string, $char, $offset);
}
When we run this program, we get the following output:
Found e at 1
Found e at 5
Found e at 7
Example 4. How to find the last occurrence
Instead of looping through every occurrence of a letter in a string to find the last one, you can use the rindex() function. This works exactly the same as index() but it starts at the end of the string. The index value returned is string from the start of the string though.
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'e';
my $result = rindex($string, $char);
print "Result: $resultn";
This would produce:
Result: 7
Perl指令碼一小例子:
echo "aaTbbT"|perl -ne "for $i (split /T/){print $i;}"#這裡將for改成foreach也可以,split/T/可以改成split(“:”)或split(“:”,$_)
結果:aabb
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23168012/viewspace-1045839/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- perl指令碼指令碼
- 6,雜湊(perl語言筆記)筆記
- Perl指令碼執行方法小結指令碼
- 透過hostname獲取IP的perl指令碼指令碼
- ruby指令碼,隨機生成複雜密碼指令碼隨機密碼
- C語言和SH指令碼的雜交程式碼C語言指令碼
- [轉]Shell向Perl指令碼中傳遞變數的方法指令碼變數
- 用perl指令碼檢測即將到期的date分割槽指令碼
- crontab無法執行perl指令碼 手工卻成功執行指令碼
- Linux指令碼語言PERL 的模板應用分析(轉)Linux指令碼
- 在Linux系統下安裝Perl指令碼語言(轉)Linux指令碼
- 工具推薦. 線上unix, 線上python/perl指令碼測試環境Python指令碼
- Perl連線Oracle資料庫的一些操作指令碼【轉】Oracle資料庫指令碼
- QT6 原始碼雜記QT原始碼
- 一些CTF雜項MISC解題指令碼指令碼
- perl學習筆記1筆記
- 14,程式管理(perl筆記)筆記
- perl學習筆記(7)筆記
- perl小指令碼——ftp上傳檔案、讀取資料庫表中資料指令碼FTP資料庫
- JavaScript工作指令碼筆記整理JavaScript指令碼筆記
- 記憶體檢查指令碼記憶體指令碼
- Shell指令碼學習筆記指令碼筆記
- Android make指令碼簡記Android指令碼
- perl學習筆記---標量筆記
- 複雜多變場景下的Groovy指令碼引擎實戰指令碼
- 用Jmeter編寫一個較複雜的測試指令碼JMeter指令碼
- shell指令碼程式設計筆記指令碼程式設計筆記
- python指令碼練習筆記Python指令碼筆記
- linux shell 指令碼攻略筆記Linux指令碼筆記
- shell指令碼學習筆記-1指令碼筆記
- 一鍵部署lamp指令碼記錄LAMP指令碼
- [BI專案記]-DB指令碼同步指令碼
- [筆記]OTN上的sql指令碼筆記SQL指令碼
- [筆記]一些SQL指令碼筆記SQL指令碼
- html雜記HTML
- webserver 雜記WebServer
- oracle雜記Oracle
- cpp雜記