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)
In reply to Extract information from several files in directory by Sofie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |