Sofie has asked for the wisdom of the Perl Monks concerning the following question:
Hi I am a beginner at Perl, trying to do some really simple things, but nothing seems simple.. I have a folder with a bunch of tab sep txt files. All the files have the same type of information, and I need to extract the information from one of the columns in each file and put in a new file. I have previously managed to do this with one file by readin gthe file into an array and iterating each row of the array, spliting it on tab and the taking the second column (where the data I want is) and printing this to a new file. But now I have several files. I started by using a while loop to open the directory and counting the number of files in the directory. But struggling to get to reading each file and extracting my data.. After lots of googling, there seems to be lots of different answers, but can't really understand them. I need something very simple so I can understand how it works.
#!/usr/bin/perl -w use strict; #create file to write to open (LABNR, ">>Labnr_all.txt") or die "Could not open file"; #open the directory where files are located $dirname = 'Filer'; opendir (DIR, $dirname) or die "Could not open $dirname\n"; #count files $nrfiles = 0; while ($filename = readdir(DIR)){ $nrfiles ++ if $filename =~ /\.txt/; print "$filename\n" if -f $filename; #this is where I want to look into each file and extract the info... } print "The number of files in the folder: $nrfiles\n"; closedir(DIR)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Extract information from several files in directory
by hippo (Chancellor) on Nov 27, 2020 at 15:38 UTC | |
Re: Extract information from several files in directory
by tybalt89 (Prior) on Nov 27, 2020 at 16:06 UTC | |
Re: Extract information from several files in directory
by AnomalousMonk (Bishop) on Nov 27, 2020 at 21:23 UTC | |
by Tux (Abbot) on Nov 28, 2020 at 09:38 UTC | |
Re: Extract information from several files in directory
by LanX (Cardinal) on Nov 27, 2020 at 14:49 UTC | |
Re: Extract information from several files in directory
by wazat (Monk) on Nov 29, 2020 at 18:32 UTC |