簡體   English   中英

使用grep命令查找單詞,然后在perl腳本中打印整列

[英]Use grep command to find a word and then print the entire column in perl script

我有一個.csv文件,我想在其中查找特定單詞'ELECTRICIANS',這是該列中的第一項。 我想打印整個列,名稱為ELECTRICIANS,如果我重復其他任何單詞,則應該打印錯誤。

我已經執行了grep操作,如下所示:

my $exit = system("grep -q $column_name $file_name");
if ($exit == 0)
{
        print "Entered column name $column_name found in file $file_name\n";
}
else {
        print "Column name not found, try again!\n";
        exit;
}

現在,如何在“ ELECTRICIANS”下的文件內打印該列?

.csv文件的內容如下:

WEEK,SITE ENGINEERS,SITE ENGINEERS 2,ELECTRICIANS,ELECTRICIANS 2
ONE,13,28,17,29   
TWO,13,30,18,27 
THREE,13,30,14,23  
FOUR,15,30,12,29 
FIVE,15,22,16,24  
SIX,16,30,20,30 
SEVEN,12,27,13,29  
EIGHT,19,22,16,29 
NINE,19,21,19,30  
TEN,12,22,14,30,13

您的示例數據的最后一行似乎還有一個額外的列。

但這就是您想要的。

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

# read header
my @cols = split /,/, <DATA>;

# Process the rest of the data
while (<DATA>) {
  my %data;
  @data{@cols} = split /,/;
  say $data{ELECTRICIANS};
}

__DATA__
WEEK,SITE ENGINEERS,SITE ENGINEERS 2,ELECTRICIANS,ELECTRICIANS 2
ONE,13,28,17,29
TWO,13,30,18,27
THREE,13,30,14,23
FOUR,15,30,12,29
FIVE,15,22,16,24
SIX,16,30,20,30
SEVEN,12,27,13,29
EIGHT,19,22,16,29
NINE,19,21,19,30
TEN,12,22,14,30,13

您正在編寫一個Perl腳本,不需要將一個子進程派生到shell中,然后再調用其他shell函數。 Perl內置了grep。下面以示例說明如何在Perl中完全實現相同的結果。 我還添加了注釋來解釋腳本。

use strict;
use warnings;

#set the column name, this could be changed to accept input from user, 
#also read from the data file handle, this could also be changed to read from any other file
my $column = "ELECTRICIANS";
my @headers = split(',',<DATA>);

#Check for the column name rom the headers just read from the file handle, if not found then exit
unless (grep {$_ eq "$column"} @headers){
    print "Column name $column not found, try again!\n";
    exit 1;
}

print $column, "\n";

#if we got to hear then the CSV file must have the column we are insterested in so start looping though the lines
#split each line into fields then for each lin make a hash using the headers and the fields. then we can print the column
#we are interested in
while(<DATA>){
    chomp();
    my @fields = split(',');
    my %data;
    @data{@headers}=@fields;
    print $data{$column}, "\n";
}

__DATA__
WEEK,SITE ENGINEERS,SITE ENGINEERS 2,ELECTRICIANS,ELECTRICIANS 2 
ONE,13,28,17,29 
TWO,13,30,18,27 
THREE,13,30,14,23 
FOUR,15,30,12,29 
FIVE,15,22,16,24 
SIX,16,30,20,30 
SEVEN,12,27,13,29 
EIGHT,19,22,16,29 
NINE,19,21,19,30 
TEN,12,22,14,30,13

這產生輸出

ELECTRICIANS
17
18
14
12
16
20
13
16
19
14

在讀取每一行並將其放入哈希后,您還可以打印其他列,因為您知道每個字段的列名稱。

我經常因為我的一線而受到批評,但是這里有一個:

perl -F, -slane '
    if ($. == 1) {
        for ($i=0; $i<@F; $i++) {
            if ($F[$i] eq $colname) {
                $colno = $i;
                last;
            }
        }
        die "cannot find $colname column" unless defined $colno;
    }
    print $F[$colno]
' -- -colname=ELECTRICIANS file.csv

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM