Perl中读取文件时,从一列打印的独特价值 [英] Perl reading file, printing unique value from a column

查看:165
本文介绍了Perl中读取文件时,从一列打印的独特价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的Perl和我想实现用Perl以下。

我有一个包含以下数据的文件:


 的/ dev / hda1上的/ boot EXT3 RW 0 0
为/ dev / hda1上的/ boot EXT3 RW 0 0


我想从文件中提取第二个字段,只打印唯一值。我希望这个例子输出,程序应该打印:

EXT3

也如果我有几个不同的文件系统,它应打印在同一行上。

我已经尝试了许多件code,但我离开卡住。

感谢您


解决方案

 #!的/ usr / bin中/ perl的我散列%;
而(小于&GT){
如果(/\\s*[^\\s]+\\s+[^\\s]+\\s+([^\\s]+)\\s+.*/){
    $哈希{$ 1} = 1;
}
}
打印连接(\\ n键(%哈希))\\ N。

用法:

  ./<prog-name>.pl文件1 FIL2 ....

I am new to perl, and i'd like to achieve the following with perl.

I have a file which contain the following data:

/dev/hda1 /boot ext3 rw 0 0
/dev/hda1 /boot ext3 rw 0 0

I'd like to extract the second field from the file and print unique values only. My desired output for this example is, the program should print :

ext3

also if i have several different filesystem, it should print in on the same line.

I have tried many piece of code but am left stuck.

Thank you

解决方案

#!/usr/bin/perl

my %hash ;
while (<>) {
if (/\s*[^\s]+\s+[^\s]+\s+([^\s]+)\s+.*/) {
    $hash{$1}=1;
}
}
print join("\n",keys(%hash))."\n";

Usage:

 ./<prog-name>.pl file1 fil2 ....

这篇关于Perl中读取文件时,从一列打印的独特价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆