to extract certain column of data from a number to files. | Newbie | | Join Date: Oct 2008
Posts: 3
| | |
I am new to perl scripting. I am having some problem to write a program.
I have a number of files containing same type of data with same header.
File is like this.
SN. Cities temperature Humidity rainfall
1 abc 33 66 23
2 ghi 36 83 12
3 xyz 23 78 11
......
I want to extract temperature and humidity for all cities from each file to a new file.
File names are like 1.txt, 2.txt, 3.txt, 4.txt.........
Please help me to solve this problem.
Thanks in advance.
~NaraN
|  | Expert | | Join Date: Dec 2007
Posts: 400
| | | re: to extract certain column of data from a number to files. Quote:
Originally Posted by NaraN I am new to perl scripting. I am having some problem to write a program.
I have a number of files containing same type of data with same header.
File is like this.
SN. Cities temperature Humidity rainfall
1 abc 33 66 23
2 ghi 36 83 12
3 xyz 23 78 11
......
I want to extract temperature and humidity for all cities from each file to a new file.
File names are like 1.txt, 2.txt, 3.txt, 4.txt.........
Please help me to solve this problem.
Thanks in advance.
~NaraN What have you tried so far?
If the source data is in excel file, you can make use of Spreadsheet::ParseExcel and Spreadsheet::WriteExcel. If it is text file, then you should be able to do it using split() function.
However, we will not be able to help you unless you post your code.
| | Member | | Join Date: Sep 2008
Posts: 65
| | | re: to extract certain column of data from a number to files.
Hi there,
did u see this post: http://bytes.com/forum/thread845672.html
i guess, your problem will get resolve if you try it out..
But as said by nitinpes, you should try scripting yourself.if get struck then post the code here..people will help you out.
-Vijayarl
|  | Newbie | | Join Date: Aug 2007
Posts: 20
| | | re: to extract certain column of data from a number to files.
Hi !!!
don't know whether you got ur solution or not. just got some free time so tried to solve your problem. As i m also a beginner, probably this is not the best way, there must be much better ways to do the same thing which someone more experienced can suggest.
Here is my peice of code:- - use strict;
-
use warnings;
-
use FileHandle;
-
use Fcntl qw(:flock);
-
-
my $file = shift;
-
-
my $openmode = '+<';
-
my ($fh, $eof,$line, $linenr, $header, $fields);
-
my (@fields, @header);
-
-
if (!open($fh, $openmode, $file)) {
-
die("Unable to open file '$file': $!");
-
}
-
-
if (!flock($fh, LOCK_EX | LOCK_NB)) { # Get an exclusive non-blocking lock
-
# We didn't get the lock
-
die("File $file locked by another process. Skipping it");
-
close($fh);
-
}
-
-
print("Processing $file ");
-
-
$eof = 0;
-
-
while(!$eof) {
-
-
# Read another line of input
-
$line = $fh->getline();
-
if (!defined($line)) {
-
# Reached EOF
-
$eof = 1;
-
-
last;
-
}
-
-
$linenr = $fh->input_line_number();
-
-
# Stript EOL chars from line
-
$line =~ s/\r?\n$//s;
-
# $line =~ s/ //g;
-
if ($line =~ /^\s*$/) {
-
# Blank line
-
print "Skipping blank line $linenr\n";
-
next;
-
} elsif ($line =~ /^#/) {
-
# Comment line
-
print "Skipping comment line $linenr\n";
-
next;
-
} elsif ($linenr == 1) {
-
# Header line
-
print "Processing header line #$linenr\n";
-
@header = split / /,$line;
-
print("\n @header \n");
-
next;
-
}
-
-
@fields = split / /,$line;
-
print("\n @fields \n");
-
-
}
-
-
| | Newbie | | Join Date: Oct 2008
Posts: 3
| | | re: to extract certain column of data from a number to files.
hey friends,
Thanks a lot for ur replies. I will try according to you people..
I will post the result as soon as i get some result.
presently, i just can filter the certain column of data using split function but they have to be concatenated similar datas( just like adding columns in excel) from datas from multiple files.
Result shuld be like:
SN Places Temp humidy date temp humidity date temp humidiy date.........
1 abc 33 97 Oct1 34 98 Oct2 32 97 Oct3
2 xyz 23 88 Oct1 22 89 Oct2 21 86 Oct3
.......
Thanks again,
Regards
~NaraN
|  | Expert | | Join Date: Dec 2007
Posts: 400
| | | re: to extract certain column of data from a number to files. Quote:
Originally Posted by NaraN presently, i just can filter the certain column of data using split function but they have to be concatenated similar datas( just like adding columns in excel) from datas from multiple files.
Result shuld be like:
SN Places Temp humidy date temp humidity date temp humidiy date.........
1 abc 33 97 Oct1 34 98 Oct2 32 97 Oct3
2 xyz 23 88 Oct1 22 89 Oct2 21 86 Oct3
.......
Thanks again,
Regards
~NaraN Post your code here, so that someone can help you out.
|  | Site Moderator | | Join Date: May 2007 Location: New Hampshire
Posts: 2,572
| | | re: to extract certain column of data from a number to files.
crazy4perl,
While I understand that you are probably anxious to help, please be sure and read the posts in any thread you visit. One of our experts (Nithinpes) had asked the OP what they have tried and to post their code. This is a learning forum and just providing someone answers as you have does not help them learn. Also, in doing so you may be giving someone the answers to their homework, which is against site policy.
I am not saying don't help, but next time please wait until they show their code and see if you can help with that first.
Regards,
Moderator
| | Newbie | | Join Date: Oct 2008
Posts: 3
| | | re: to extract certain column of data from a number to files.
Thanks nithinpes and crazy4perl.
I have written a simple code that can extract required columns from a simple file. I am just trying to get some output and this works for a file.
$myfile=$ARGV[0];
$myfile1=$ARGV[1];
open (DATA, "<$myfile") || die "Can't open $myfile $!";
while (<DATA> )
{
@x= split('\t');
if ($x[0] >= 1) {
print STDOUT "$x[0]\t$x[1]\t$x[2]\n" ;
}}
close DATA;
exit(0);
And i execute this ...perl perlscript.pl 1.txt >out.txt
Now my problem is: Can i open another file and append the output columns (x[1] and [2] from another file 2.txt) in out.txt in each line ...? Or other good ways to do it efficiently? Should i open all the files simultaneously and do my work. Can you suggest me please.
Thanks in advance
regards,
~NaraN
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: to extract certain column of data from a number to files.
Tie::File allows you to easily edit a file. Perls builtin editor (-i command line or $^I in a script) can also be used to edit files inplace. You can also open two files for reading/input and a third file to output the new results to.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|