Column minus column operation?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Column minus column operation?
# 1  
Old 11-17-2012
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~
# 2  
Old 11-18-2012
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-18-2012 at 12:44 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 11-18-2012
Using paste & awk:-
Code:
paste file_A file_B | awk ' { printf "%d %d %d\n", $1, ($2-$7), ($5-$8); } '

This User Gave Thanks to Yoda For This Post:
# 4  
Old 11-18-2012
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 08:13 AM..
This User Gave Thanks to drl For This Post:
# 5  
Old 11-18-2012
Slight variation:
Code:
awk '{split($0,A); getline<f; print A[1], A[2]-$2, A[5]-$3}' f=fileB fileA

This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (12 Replies)
Discussion started by: as7951
12 Replies

2. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

4. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

5. Shell Programming and Scripting

Enter third column & Perform Operation

I am trying to enter a third column in this file, but the third column should that I call "Math" perform a some math calculations based on the value found in column #2. Here is the input file: Here is the desired output: Output GERk0203078$ Levir Math Cotete_1... (5 Replies)
Discussion started by: Ernst
5 Replies

6. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

7. Shell Programming and Scripting

column operation using awk

I have atxt file,i want to perform some operation on 3rd coulmn 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 67.780083 -3.0000000 6.9356270 0 4 ... (4 Replies)
Discussion started by: shashi792
4 Replies

8. Shell Programming and Scripting

Column operation : cosne and sine operation

I have a txt file with several columns and i want to peform an operation on two columns and output it to a new txt file . file.txt 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 ... (4 Replies)
Discussion started by: shashi792
4 Replies

9. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

10. Shell Programming and Scripting

compare minus symbol "-" with a column

Need to compare minus symbol "-" with a column that contains minus on few rows. the sample script awk I have written is if ( '$COLUMN2' ~ "\-" ) {} COLUMN2 is a shell variable. I just need the if condition checking the shell variable within the awk script. Below is the entire awk part... (14 Replies)
Discussion started by: kalee
14 Replies
Login or Register to Ask a Question