Chinaunix首页 | 论坛 | 博客
  • 博客访问: 123568
  • 博文数量: 20
  • 博客积分: 747
  • 博客等级: 军士长
  • 技术积分: 195
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-25 18:02
文章分类

全部博文(20)

文章存档

2012年(1)

2011年(1)

2010年(2)

2009年(16)

我的朋友

分类: LINUX

2009-03-23 10:32:19

#!/usr/bin/perl

##Write a program to open the /etc/passwd file by name and print out the username, user ID (number), and real name in formatted columns. Use format and write.

use strict;
use warnings;

my $username;
my $ID;
my $realname;

format OUTFILE =
====================================
| @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
"username:".$username
| @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
"ID:".$ID
| @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
"realname:".$realname
====================================
.

open PASS,"/etc/passwd" or die "Can't.";
open OUTFILE,">format.log" or die "Can't.";
while(){
        chomp;
        my @items = split(/:/);
        $username = $items[0];
        $ID = $items[1].":".$items[2].":".$items[3];
        $realname = $items[4];
        write (OUTFILE);
}


result:

# head format.log

====================================
| username:root                    |
| ID:x:0:0                         |
| realname:root                    |
====================================
====================================
| username:bin                     |
| ID:x:1:1                         |
| realname:bin                     |
====================================

阅读(904) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~