Hi everyone!

I have just signed in at daniweb, I hope you can help me. I am new to perl and have the following problem to solve:
I have loads of files with 16 columns full of numbers. I need to
1. take column 6 and substract column 10, store the difference column in a new column
2. then delete all columns and just leave the first and this difference column in the file

I know this is very easy to do in any mathematical program or openofficeCalc but since I have to do it like for hundreds of file I really need a program that does this for me.

I would be really grateful if someone could help me.
many thanks,
uan

Dead easy in awk (Perl's baby sibling)
Assuming these are comma separated value files, awk -F, '{ print $1 "," $6-$10 }' file.txt

Hi there,

so, this is where i am:

(...)#opening the file with the 12 columns
unless (open(FH,$file)){ 
	print STDERR "Datei \"$prot\" konnte nicht geƶffnet werden.";
	exit;
	}
#reading in evething separated by a space
$/=" ";
@filedata=<FH>;	
close FH;

#$input_record_separator set back to default to be able to chomp
$/="\n";
#the 1st column which i will need later
my @q=();
for(my $i=0; $i<= scalar @filedata; $i+=12){
	push @q, $filedata[$i];
	}
chomp (@q); 
# this does not work! why not??????????????????? i still have the newline at the end if each #number

#taking difference of 6th and 10th columns
my @diff=();
my $d=0;
for(my $i=0; $i<= (scalar @filedata)-12; $i+=12){
	  $d= $filedata[$i+5]-$filedata[$i+9];
	  push @diff, $d;
	  }
#print to file
$out="out.dat";
open(DAT, ">$out") or die "Error occured while writing .dat";

for(my $i=0; $i<= scalar @diff; $i++){
	print DAT $q[$i], " ", $diff[$i];
	}
close(DAT);

so, my question is: is there any easier method? And why is that chomping not working??

hi Salem,
thanks for you message. what is awk, how do i use it? so where do i have to type that command you wrote?
thanks,
uan

http://en.wikipedia.org/wiki/Awk

> how do i use it?
About the same as you would perl, except you type 'awk' instead of 'perl'

Along with 'sed' and 'grep', they form a family of text processing utilities which are pretty ubiquitous on any Unix / Linux distribution.

but how do i write than a loop over all the hundreds of files to do this in awk? in perl that part is very easy...

Replace
file.txt
with
*.txt

awk reads the file even more implictly than perl does. So implicitly in fact that you don't have to say anything at all.

post some sample input data and desired output data.

If you want to continue with awk you should take this discussion to an awk forum.

Perl has very similar command line syntax to awk and could be done as a command line/one-liner if you really wanted to do that. one-liners are good for very targeted tasks, but not so good when you have more complex programming problems.

Your problem is right about in the middle of being a simple task and a more complicated one better suited to a script.

Hi everyone,

well, I definitely need to use a perl script, since this is just one of many many tasks I have to do with my datafiles. I want to have this part as a subroutine in a module among many others.

So, could anyone please help me with the $/ and the chomp problem? And explain me what I am really doing in lines 7 and 12 above in the script?

many thanks,
uan

I don't see why you need to read the whole file in, or store the whole file before writing it out.

while ( <FH> ) {
  chomp;
  my @f = split; # splits $_ at white-space
  print DAT $f[0] . "," . $f[5]-$f[9] . "\n";
}

to quote myself:

post some sample input data and desired output data.

Thanks a lot Salem, you are absolutely right!! I have learned a lot by this and my problem is solved :))!

cheers,
uan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.