|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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)}' fileALast 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
|
||||
|
||||
|
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
|
||||
|
||||
|
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 0producing: 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
|
||||
|
||||
|
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 | ||
|
![]() |
| Tags |
| column, operation |
| Thread Tools | Search this Thread |
| 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 |
|
|