Difference operator


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference operator
# 1  
Old 12-04-2009
Difference operator

Dear All,

Good day, Just i would like to know that is there anything called difference operator in awk?

For example, if a file contains 5 columns (as shown below) with both negative and positive values:
Col1 Col2 Col3 Col4 Col5

I need to calculate the difference between Col1 and Col2, Col2 and Col3, Col3 and Col4, Col4 and Col5, and i need to print out the same in an output file as: New_Col1 New_Col2 New_Col3 New_Col4

Can any one help me in this regard?

Expecting your reply and thanks in advance.

Regards
Fredrick.
# 2  
Old 12-04-2009
Try this, it substracts field 1 with field 2, field 2 with field 3 etc:

Code:
awk '{for(i=1;i<NF;i++){s=s FS $i-$(i+1)}print s;s=""}' file

# 3  
Old 12-04-2009
replace some string by null??

What does s=s there signify? Is that like replasing some sting?
# 4  
Old 12-04-2009
Quote:
Originally Posted by ash.g
What does s=s there signify? Is that like replasing some sting?
Code:
s=s FS $i-$(i+1)

This replaces the variable s with itself and adds a field separator FS and the substraction of the 2 fields after s.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Like operator in IF statement

how can i use like operator in IF statement. Below is correct format, please guide if ; then CT_ACT_FILE_NAME=`echo FINACLE' else CT_ACT_FILE_NAME=`echo not listed' fi ---------- Post updated at 04:58 PM ---------- Previous update was at 04:56 PM ---------- Please use CODE... (6 Replies)
Discussion started by: rizwan.shaukat
6 Replies

3. Homework & Coursework Questions

Description of OR AND Operator

From the below i would need the difference using between && and || error_func() { # Standard error function ] && print -u2 "ERROR: $*" exit 1 } ] && error_func "Source Directory is missing as input argument" ] || error_func "Source Directory does not... (1 Reply)
Discussion started by: arun888
1 Replies

4. Programming

what is the main difference between difference between using nonatomic lseek and O_APPEND

I think both write at the end of the file ...... but is there a sharp difference between those 2 instruction ..... thank you this is my 3rd question today forgive me :D (1 Reply)
Discussion started by: fwrlfo
1 Replies

5. UNIX for Dummies Questions & Answers

+= operator

im new to bash scripting and im just using online tutorials and trial and error. i wanted to write a script to read numbers from a file and find their sum: #!/bin/bash theSum=0 for line in $(cat numbers.txt) do let "theSum = theSum + $line" echo "$line" done echo "The sum is... (3 Replies)
Discussion started by: astrolux444
3 Replies

6. UNIX for Dummies Questions & Answers

su with << operator

All, THe below is my script , when i use this i am getting nothing . could any one help me to know what is the use of the << operator below su - $8 << supo echo "exportsph $2 $1 $3 $4" exportsph $2 $1 $3 $4 supo i also tried as individual command su - userid << supo , when i do... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

7. Programming

new operator

Hi, Please clear the 2 questions, 2 Questions, 1) Why the new as a operator? Is there any special reason why it can't be a function like malloc? 2) How are we considering sizeof(),new are as a opearartors? I know + - * / -> , . etc.. are operators, which criteria satisfied by sizeof()... (4 Replies)
Discussion started by: Nagapandi
4 Replies

8. HP-UX

Or operator with if

hi, i was trying to club to test condition with if. if -o ; then it is giving me error message, i wanted to ask how can we check two condtions with one if. (1 Reply)
Discussion started by: babom
1 Replies

9. Shell Programming and Scripting

And operator

I am trying to check two variables and if both are blank I want to set a flag: the_f3_pid=`rsh $target ps -ef | grep "f3.eab" | awk '{print $2}'` the_f7_pid=`rsh $target ps -ef | grep "f7.eab" | awk '{print $2}'` if ; then y=1 fi I get an error: ./script_name: test: 0403-021 ]... (4 Replies)
Discussion started by: rcarnesiii
4 Replies

10. Programming

Problem about c++ new operator

#include <iostream.h> class test { private: int i; public: inline test(int m) { i = m; } inline int get_i() { return i; } }; int main() { test * a = new test(2); (3 Replies)
Discussion started by: xbjxbj
3 Replies
Login or Register to Ask a Question