Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3306958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:21:00+00:00 2026-05-17T21:21:00+00:00

I would like to filter out particular columns with a regex and discard others.

  • 0

I would like to filter out particular columns with a regex and discard others. For example, if I had the following column names:

date
mem_total
cpu.usagemhz.average_0
cpu.usagemhz.average_1
cpu.usagemhz.average_2

I would like to capture only columns that begin with “cpu.usage.mhz.average”

Is their a particular function of text::csv that will help me do a quick check of the column names?

Thanks!
JD

* Update **

I tried jimtut answer and it is extremely close to what I am looking for. Thanks Again Everyone!

Here is the code from jimtut with one small edit on the print statement at the bottom. I added the print $colCount just to see what was going on with the data;

use Text::CSV;

my $file = "foo.csv";
my $pattern = ".*In";
open(F, $file) or warn "Warning! Unable to open $file\n";

my $lineCount = 0;
my %desiredColumns;
while(<F>) {
  $lineCount++;
  my $csv = Text::CSV->new();
  my $status = $csv->parse($_); # should really check this!
  my @fields = $csv->fields();
  my $colCount = 0;

  if ($lineCount == 1) {
    # Let's look at the column headings.
    foreach my $field (@fields) {
      $colCount++;
      if ($field =~ m/$pattern/) {
        # This heading matches, save the column #.
        $desiredColumns{$colCount} = 1;
      }
    }
  }
  else {
    # Not the header row.  Parse the body of the file.
    foreach my $field (@fields) {
      $colCount++;
      if (exists $desiredColumns{$colCount}) {
        # This is one of the desired columns.
        # Do whatever you want to do with this column!
        print "$colCount\t$field\n";
      }
    }
  }
}
close(F);

Here is the results

colCount |  $field

12      565
13      73
14      36
15      32
16      127
17      40
18      32
19      42
20      171
12      464
13      62
14      32
15      24
16      109
17      21
18      19
19      39
20      150
12      515
13      76
14      28
15      30
16      119
17      15
18      25
19      46
20      169
12      500
13      71
14      30
15      28
16      111
17      20
18      18
19      40
20      167

I would like to add this data to individual arrays or hashes. what do you think? something like…

foreach column {
check to see if a hash already exists with that column number. If not then create hash.
}

Then go through each field and add the field data to the appropriate hash.

Do you think this is the right way to go about solving this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:21:01+00:00Added an answer on May 17, 2026 at 9:21 pm

    No, not a specific function in Text::CSV. I would do something like this:

    use Text::CSV;
    
    my $file = "foo.csv";
    my $pattern = "cpu.usage.mhz.average.*";
    open(F, $file) or die "Unable to open $file: $!\n";
    
    my $lineCount = 0;
    my %desiredColumns;
    my %columnContents;
    
    while(<F>) {
      $lineCount++;
      my $csv = Text::CSV->new();
      my $status = $csv->parse($_); # should really check this!
      my @fields = $csv->fields();
      my $colCount = 0;
    
      if ($lineCount == 1) {
        # Let's look at the column headings.
        foreach my $field (@fields) {
          $colCount++;
          if ($field =~ m/$pattern/) {
            # This heading matches, save the column #.
            $desiredColumns{$colCount} = 1;
          }
        }
      }
      else {
        # Not the header row.  Parse the body of the file.
        foreach my $field (@fields) {
          $colCount++;
          if (exists $desiredColumns{$colCount}) {
            # This is one of the desired columns.
            # Do whatever you want to do with this column!
            push(@{$columnContents{$colCount}}, $field);
          }
        }
      }
    }
    close(F);
    
    foreach my $key (sort keys %columnContents) {
      print "Column $key: " . join(",", @{$columnContents{$key}}) . "\n\n";
    }
    

    Hope that helps! I’m sure someone can write that in a Perl one-liner, but that’s easier (for me) to read…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to filter out old records and present only newer ones. The
I would like to filter out the request log from successful static requests, however,
I would like to filter NSMutableArray elements that contains some string. ListArchives is filled
Hello I have a datatable that I would like to filter with a single
I have a CSV file and I would like to filter all the lines
I have set of 6 elements and I would like to filter by every
Given a set of phrases, i would like to filter the set of all
I have a TreeMap resMap new TreeMap<String, Map<String, String>>(); I would like to filter
I would like to try some image filter features on iPhone like instagram does.
I would like to know how I can do a low-pass filter in opencv

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.