LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2008, 03:12 PM   #1
dontbugmeplz
LQ Newbie
 
Registered: Jul 2008
Posts: 4

Rep: Reputation: 0
bash script to find the difference between the 4th element of each line in 2 files


Hi All,
I have 2 data files. I want a shell script to get the 4th element on each line of each file, and take the difference and store it in a variable. (1st line are junk description, it would be good if u could write code to ignore the 1st 2 line and start reading from the third line)

To read from one file is simple:

while read a

do

echo "$a" | 'awk [print $4]'

done <"filename"

sth like above.

Now, how do I read the other file simultaneously and pull out the 4th element and take the difference.

A quick solution will be very much appreciated.
Thanks much!

DM.
 
Old 07-18-2008, 04:54 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quickie in Perl:

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $file1 = shift || die "please specify two files as arguments\n";
my $file2 = shift || die "please specify two files as arguments\n";

open(F1, "<$file1") || die "cannot open $file1 : $!\n";
open(F2, "<$file2") || die "cannot open $file2 : $!\n";

while(!eof(F1)&&!eof(F2)) {
        my $v1 = (split(/\s+/, <F1>))[3];
        my $v2 = (split(/\s+/, <F2>))[3];

        printf "%-4d - %-4d == %d\n",  $v2, $v1, ($v2 - $v1);
}
close(F1);
close(F2);
The program will terminate when the end of either file is detected. No checking is done to make sure the required values from the input files are actually numerical, so you'll get a warning or two if they are not.
 
Old 07-19-2008, 05:41 PM   #3
dontbugmeplz
LQ Newbie
 
Registered: Jul 2008
Posts: 4

Original Poster
Rep: Reputation: 0
bash script reqd!!!

Thanks a lot matthewg42. I need a bash shell script. Appreciate ur efforts, though.
 
Old 07-19-2008, 05:52 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
how about this:
Code:
#!/bin/bash

have_data=1
line_number=0
while [ $have_data -eq 1 ]; do
        let line_number+=1
        read -u 2 d1_1 d1_2 d1_3 d1_4 d1_5
        read -u 3 d2_1 d2_2 d2_3 d2_4 d2_5
        if [ "$d1_4" = "" ] || [ "$d2_4" = "" ]; then
                echo "End of data at line $line_number"
                have_data=0
        else
                echo "at line $line_number : $d1_4 + $d2_4 == $(($d1_4 + $d2_4))"
        fi
done 2<one 3<two
 
Old 07-19-2008, 06:48 PM   #5
dontbugmeplz
LQ Newbie
 
Registered: Jul 2008
Posts: 4

Original Poster
Rep: Reputation: 0
WIll try this, thanks!!!

Thanks Matthew for the quick response. Will try it and let u know.
 
Old 07-19-2008, 09:10 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '{
 getline line <"file2"
 m=split(line,l," ")
 print "The difference is: " l[4]-$4
}' file
 
Old 07-21-2008, 08:05 AM   #7
dontbugmeplz
LQ Newbie
 
Registered: Jul 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks!!!

Thanks ghostdog and matthew for ur help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 04:26 AM
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 07:42 PM
how to find files using bash script prernasin Linux - Newbie 6 09-26-2007 08:57 AM
A bash script to find duplicate image files fotoguy Programming 7 01-25-2007 06:47 PM
Trying to find the files that contain distro information for my bash script ethics Linux - General 5 07-04-2006 04:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration