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 6695055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:10:32+00:00 2026-05-26T06:10:32+00:00

I want to extract rows from a file that match a particular pattern and

  • 0

I want to extract rows from a file that match a particular pattern and I want to do this for over 500 files. It should have the ability to retain the unique name of the file as well.

I used awk but then i have to do each file individually.

c:\>gawk "/S1901/" Census_Tract_*.csv > Census_Tract_*.csv

In the example shown in the link here (http://bit.ly/nMX8qh) I want to retain only those records that have S1901 in them. Apologies for the external link but i am not able to retain formatting of the table.

I found some perl code that I used to write it but it retains all the rows and does not select only those rows/records where the pattern matches. Any tips would be much appreciated. The perl code is below:

#perl -w
$pattern = "Subject_Census*.csv";  # process only those files that match pattern
while (defined ($in = glob($pattern))) {
  ($out = $in) =~ s/\.csv$/.outcsv/;            # read from "xyz.in" and write to "xyz.out"
  open (IN, "<", $in) or die "Can't open $in for reading: $!";
  open (OUT,">>", $out) or die "Can't open $out for writing: $!";
  while (<IN>) {
    $mystring  =~ /S1901/;
    print OUT $_ if $mystring == 0;
  }
  close (IN) or die "Can't close $in: $!";  # good idea to do some housekeeping
  close (OUT) or die "Can't close $out: $!";
}
  • 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-26T06:10:33+00:00Added an answer on May 26, 2026 at 6:10 am

    Untested:

    use strict;
    use warnings;
    use autodie;
    
    my $files_list_filename = 'files.txt';
    
    open my $fl, '<', $files_list_filename;
    my @list_of_files = <$fl>;
    chomp @list_of_files;
    close $fl;
    
    foreach my $file ( @list_of_files ) {
        open my $test_fh, '<', $file;
        while ( my $line = <$test_fh> ) {
            if( $line =~ m/S1901/ ) {
                print "$file at $.: $line";
            }
        }
        close $test_fh;
    }
    

    Is that sort of what you had in mind? It opens a file named filelist.txt and reads in a list of however many filenames you want to give it. Then it iterates over that list, opening each file one by one, scanning each file one by one, and if a line is found containing the trigger text, it prints the filename and line number, as well as the line itself where the trigger was met. Then it moves on to the next.

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

Sidebar

Related Questions

I want to extract rows 1 to n from my .csv file. Using this
I have a datatable that I want to extract certain information from (only certain
I want to extract from a webpage all URLs how can I do that
I want to extract the text information (Chinese) from the images that picked by
I have the following query that gives me an extract (I am putting this
I want to extract rows from a table whose interval intersects with an interval
I want to extract random rows from the app sqlite database in Android. I'm
Given a DataView that contains multiple rows, I want to extract a row based
Here is my mysql query. What I want to extract is; all rows that
I'm trying to use simple html dom to extract elements from a file that

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.