Ty Young
Ty Young

Reputation: 7

Perl grep for a value in column 1

A system call I'm making in Perl as follows:

@filesystems = `/nas/bin/nas_fs -query:TypeNumeric==1:IsRoot==False -fields:RWServers,ROServers,Name,RWMountpoint, -format:%L:%L:%s:%s\\\\n`;

works well. It gets me the desired info:

server_5::fs_pipeline_950155:/root_vdm_30/fs_pipeline_95015    
:server_7:fs_nfs_esx_wks_vms:    
server_7::fs_ovid3:/fs_ovid3

If, however, I want to really only populate @filesystems with entries for which there's a value in column 1 (i.e. the first value ... lines 1 and 3 in the example above), I'm unsure how to achieve this. awk -F through a pipe doesn't seem to work.

Upvotes: 0

Views: 146

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74685

You could do this in Perl itself:

my @temp = `/nas/bin/nas_fs -query:TypeNumeric==1:IsRoot==False -fields:RWServers,ROServers,Name,RWMountpoint, -format:%L:%L:%s:%s\\\\n`;
my @filesystems = grep { !/^:/ } @temp;

This filters out any entries which begin with a colon from the list.

Alternatively, you could invoke another process:

my @filesystems = `/nas/bin/nas_fs <args> | grep -v '^:'`;

grep -v returns only lines that don't match the pattern, so lines beginning with a colon are excluded.

Upvotes: 1

TobyLL
TobyLL

Reputation: 2306

You can do this in your script after @filesystems is populated:

# Removes blank lines and lines starting with :
@filesystems = grep { !/(^:|^\s*$/ } @filesystems;

Upvotes: 1

Related Questions