且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Perl中读取文件时,从一列打印的独特价值

更新时间:2023-02-16 15:38:10

 #!的/ 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 ....