Column minus column operation? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Column minus column operation?

UNIX for Dummies Questions & Answers


Tags
column, operation

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-17-2012
exsonic exsonic is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 5 April 2013, 3:14 PM EDT
Posts: 6
Thanks: 15
Thanked 0 Times in 0 Posts
Column minus column operation?

I have a two files, file A and B, which have 5 columns, and each 5 columns are made up of random numbers, that means, numbers are all different.

They have same amount of lines (Both 1000 lines)

I hope to do a operation

1) 2nd column of file A - 2nd column of file B
2) 5th column of file A - 3rd column of file B

and print them out with file name 'results' with

First column: 1st column of file A
Second column: result of 1)
Third column: result of 2)

I've been tried to use cat and awk, but not works. Need any help here~
Sponsored Links
    #2  
Old 11-17-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Gotham Knight
 
Join Date: Mar 2012
Last Activity: 25 August 2014, 2:00 AM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 495 Times in 472 Posts
Please post a representative sample of the input and the corresponding expected output.

Without any samples, try this:

Code:
awk '{a1=$1;a2=$2;a5=$5;getline<"fileB";print a1,(a2-$2),(a5-$3)}' fileA


Last edited by elixir_sinari; 11-17-2012 at 11:44 PM..
The Following User Says Thank You to elixir_sinari For This Useful Post:
exsonic (11-18-2012)
Sponsored Links
    #3  
Old 11-18-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 29 August 2014, 5:55 PM EDT
Location: Galactic Empire
Posts: 3,362
Thanks: 230
Thanked 1,197 Times in 1,125 Posts
Using paste & awk:-

Code:
paste file_A file_B | awk ' { printf "%d %d %d\n", $1, ($2-$7), ($5-$8); } '

The Following User Says Thank You to Yoda For This Useful Post:
exsonic (11-18-2012)
    #4  
Old 11-18-2012
drl's Avatar
drl drl is online now Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 1 September 2014, 2:38 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,670
Thanks: 39
Thanked 192 Times in 175 Posts
Hi.

Similar to awk, Gary Perlman's |stat dm:

Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate column content arithmetic, |stat dm.
# See: http://hcibib.org/perlman/stat/index.html

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C awk dm

pl " Input data files data[12]:"
head data[12]

pl " Combined input files:"
paste data[12] > data3
head data3

pl " Results, bipinajith awk:"
awk ' { printf "%d %d %d\n", $1, ($2-$7), ($5-$8); } ' data3

pl " Results, dm"
dm x1 x2-x7 x5-x8 < data3

exit 0

producing:

Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
awk GNU Awk 3.1.5
dm - ( local: ~/executable/dm, 2009-11-09 )

-----
 Input data files data[12]:
==> data1 <==
 7 	14 	10 	19 	15 
10 	 1 	14 	13 	20 
 5 	13 	11 	19 	 9 

==> data2 <==
10 	 5 	15 	16 	20 
 8 	 4 	14 	 4 	20 
 1 	12 	 7 	17 	 6 

-----
 Combined input files:
 7 	14 	10 	19 	15 	10 	 5 	15 	16 	20 
10 	 1 	14 	13 	20 	 8 	 4 	14 	 4 	20 
 5 	13 	11 	19 	 9 	 1 	12 	 7 	17 	 6 

-----
 Results, bipinajith awk:
7 9 0
10 -3 6
5 1 2

-----
 Results, dm:
7	9	0
10	-3	6
5	1	2

See web page noted in comments for details on |stat.

Best wishes ... cheers, drl

( Edit 1: corrected errors introduced by too-smart spelling corrector )

Last edited by drl; 11-19-2012 at 07:13 AM..
The Following User Says Thank You to drl For This Useful Post:
exsonic (11-18-2012)
Sponsored Links
    #5  
Old 11-18-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 September 2014, 2:17 PM EDT
Location: Amsterdam
Posts: 9,384
Thanks: 273
Thanked 2,347 Times in 2,107 Posts
Slight variation:

Code:
awk '{split($0,A); getline<f; print A[1], A[2]-$2, A[5]-$3}' f=fileB fileA

The Following User Says Thank You to Scrutinizer For This Useful Post:
exsonic (11-18-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Enter third column & Perform Operation Ernst Shell Programming and Scripting 5 06-13-2011 11:47 AM
Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 rydz00 Shell Programming and Scripting 7 11-09-2010 10:28 AM
column operation using awk shashi792 Shell Programming and Scripting 4 09-08-2010 08:09 PM
Column operation : cosne and sine operation shashi792 Shell Programming and Scripting 4 09-08-2010 07:59 PM
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05:27 AM



All times are GMT -4. The time now is 02:41 PM.