第一道:
- 取出/etc/passwd中shell出现的次数
顺便贴一下我的/etc/passwd文件
- root:x:0:0:root:/root:/bin/bash
- bin:x:1:1:bin:/bin:/sbin/nologin
- daemon:x:2:2:daemon:/sbin:/sbin/nologin
- adm:x:3:4:adm:/var/adm:/sbin/nologin
- lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
- sync:x:5:0:sync:/sbin:/bin/sync
- shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
- halt:x:7:0:halt:/sbin:/sbin/halt
- mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
- news:x:9:13:news:/etc/news:
- uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
- operator:x:11:0:operator:/root:/sbin/nologin
- games:x:12:100:games:/usr/games:/sbin/nologin
- gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
- ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
- nobody:x:99:99:Nobody:/:/sbin/nologin
- nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
- vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
- rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
- mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
- smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
- oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin
- pcap:x:77:77::/var/arpwatch:/sbin/nologin
- dbus:x:81:81:System message bus:/:/sbin/nologin
- avahi:x:70:70:Avahi daemon:/:/sbin/nologin
- sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
- rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
- nfsnobody:x:4294967294:4294967294:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
- haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
- avahi-autoipd:x:100:156:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin
- xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
- ntp:x:38:38::/etc/ntp:/sbin/nologin
这道题真的很简单,要知道处理文本那可是Perl的强项。熟悉perl的童鞋,一个hash就能搞定。
- #!/usr/bin/perl -w
- use strict;
- my %hash;
- open my $file,'<','/etc/passwd' or die "$!\n";
- while ( <$file> ) {
- chomp;
- my @shell = split /:/;
- if ( $shell[6] ) {
- if ( exists $hash{$shell[6]} ) {
- $hash{$shell[6]}++;
- } else {
- $hash{$shell[6]} = 1;
- }
- }
- }
- close $file;
- while ( my ( $key,$val ) = each %hash ) {
- print "$key\t$val\n";
- }
output:
- [root@Xen ~]# perl c.pl
- /sbin/halt 1
- /bin/sync 1
- /sbin/nologin 27
- /sbin/shutdown 1
- /bin/bash 1
第二道:
employee文件中记录了工号和姓名
- 100 Jason Smith
- 200 John Doe
- 300 Sanjay Gupta
- 400 Ashok Sharma
bonus文件中记录工号和工资
- 100 $5,000
- 200 $500
- 300 $3,000
- 400 $1,250
要求把两个文件合并并输出如下
处理结果:
- 400 ashok sharma $1,250
- 100 jason smith $5,000
- 200 john doe $500
- 300 sanjay gupta $3,000
这道题也不是特别的难,两个hash就可以搞定的
- #!/usr/bin/perl -w
- use strict;
- my (%A,%B);
- open my $file,'<','employee.txt' or die "$!\n";
- while ( <$file> ) {
- chomp;
- /^(\d+)\s+\w+\s+\w+/;
- $A{$1} = $_ if $1 ;
- }
- close $file;
- open $file,'<','bonus.txt' or die "$!\n";
- while ( <$file> ) {
- chomp;
- /^(\d+)\s+(\$\S+)/;
- $B{$1} = $2 if ( $1 || $2 );
- }
- close $file;
- for my $num ( keys %A ) {
- if ( exists $B{$num} ) {
- $A{$num} .= "\t$B{$num}";
- }
- }
- print join "\n",sort values %A;
- print qq/\n/;
output:
- [root@Xen ~]# perl hebin.pl
- 100 Jason Smith $5,000
- 200 John Doe $500
- 300 Sanjay Gupta $3,000
- 400 Ashok Sharma $1,250
怎么样,看完上面的代码,您有学习perl的冲动了没?
本文转自dongfang_09859 51CTO博客,原文链接:http://blog.51cto.com/hellosa/596099,如需转载请自行联系原作者