If you want a "pure BASH" solution, I don't have one off hand. Stuff like
what you mentioned, I generally use a data base for. In this particular,
I'd use SQLite. A shell script using sqlite3 & awk would look something
like:
#!/bin/sh
rm files.db3
{
cat <<EOF
.mode column
.headers on
create table file1 (name text, weight int, height int);
create table file2 (name text, weight int, height int);
EOF
awk 'NR > 1 {print "INSERT INTO file1 VALUES(\"" $1 "\"," $2 "," $3 ");"} '
file1.txt
awk 'NR > 1 {print "INSERT INTO file2 VALUES(\"" $1 "\"," $2 "," $3 ");"} '
file2.txt
cat <<EOF2
.print Lines in file1 which have names not in file2
select * from file1 where name not in (select name from file2);
.print
.print Lines in file2 which have names not in file1
select * from file2 where name not in (select name from file1);
.print
.print Statistics on common names.
select f1.name, f1.weight as weight1, f1.height as height1, f2.weight as
weight2, f2.height as height, f1.weight - f2.weight as w_diff, f1.height -
f2.height as h_diff
from file1 as f1
join file2 as f2
on f1.name=f2.name;
EOF2
} | sqlite3 files.db3
The files look like (columns separated by tabs, not spaces!)
[tsh009@it-johnmckown-linux Val]$ cat file1.txt
Name weight1 height1
Alex 220 67
Tom 320 75
Craig 180 71
John 186 65
[tsh009@it-johnmckown-linux Val]$ cat file2.txt
Name weight2 height2
Alex 226 69
Tom 320 75
Craig 170 70
Steve 420 80
It if were much more complicated, I'd use an R script. I'm way too lazy to
try this in plain BASH, using only BASH facilities. I might think about it
in ooRexx.
On Mon, Jan 18, 2016 at 10:15 PM, Val Krem <[email protected]> wrote:
>
>
> Hi John and all (re posted),
>
> I am trying to write a bash script to do the following small task
>
>
> I have two files and each file has two variables.
>
> File1
> Name weight1 height1
> Alex 220 67
> Tom 320 75
> Craig 180 71
> John 186 65
>
>
> File2
> Name weight2 height2
> Alex 226 69
> Tom 320 75
> Craig 170 70
> Steve 420 80
>
>
> Here are my objectives,
>
> 1. Combine the two files (file1 and file2)
> a) count and List the name(s) along with their weight and height that
> appear in file1 not in file2
>
> Name Weight height
> eg John 186 65
>
> b) count and list the names(S) along with their weight and height that
> appear in file 2 but not in file1
>
> Name Weight height
> Steve 420 80
>
> c) for those that appear in both file 1 and file 2, I want some stat
> Name weight1 height1 weight2 height2 w_diff h_diff
> Alex 220 67 226 69 6 2
> Tom 320 75 320 75 0 0
> Craig 180 71 170 70 -10 -1
>
> d) depending on the difference
> I want to list their names along with their weight and height
>
> i) increases in both variables
> Name weight1 height1 weight2 height2 w_diff h_diff
> Alex 220 67 226 69 6 2
>
>
> ii) decreases in both variables
> Name weight1 height1 weight2 height2 w_diff h_diff
> Craig 180 71 170 70 -10 -1
>
> iii) decreases in either of the variables (weight or height)
> not in this example.
>
>
>
> w_diff= weight1 - weight2
> h_diff= height1 - height2
>
> Thank you in advance
> val
>
--
Werner Heisenberg is driving down the autobahn. A police officer pulls
him over. The officer says, "Excuse me, sir, do you know how fast you
were going?"
"No," replies Dr. Heisenberg, "but I know where I am."
Computer Science is the only discipline in which we view adding a new wing
to a building as being maintenance -- Jim Horning
Schrodinger's backup: The condition of any backup is unknown until a
restore is attempted.
He's about as useful as a wax frying pan.
Maranatha! <><
John McKown