awk - how to get difference of the same column when other column matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - how to get difference of the same column when other column matches
# 1  
Old 04-17-2013
awk - how to get difference of the same column when other column matches

I have a file like this :
Code:
# cat list
cucm, location,76,2
cucm1,location1,76,4
cucm,location,80,8
cucm1,location1,90,8
cucm1,location1,87,11
cucm,location,67,9

and I want output like this :
Code:
cucm,location,76,2
cucm1,location1,76,4
cucm,location,80, 6 ===> (8-2 =6)
cucm1,location1,90,4 ====>(8-4=4)
cucm1,location1,87,3
cucm,location,67,1

Only when the first and second column matches, I should take a difference of fourth column in the consecutive matching rows.

How will I achieve it. ?

I tried this :
Code:
awk -F"," '/cucm1,location1/ {print $1, $2, $3, $4, $4-prev;prev=$4;}' list

This works. But i may not know the contents in the runtime. Hence it has to check if column1 and column2 matches and then do a diff.

Please help

Last edited by Franklin52; 04-17-2013 at 05:09 AM.. Reason: Please use code tags
# 2  
Old 04-17-2013
Please use
Code:
CODE tags first

# 3  
Old 04-17-2013
Code:
awk -F"," '/cucm1,location1/ {print $1, $2, $3, $4, $4-prev;prev=$4;}' list


Last edited by Franklin52; 04-17-2013 at 05:27 AM.. Reason: Please use Code Tags when you post any code or data samples
# 4  
Old 04-17-2013
Quote:
Only when the first and second column matches,
I should take a difference of fourth column in
the consecutive matching rows.
Does not seem to match what happens in the output. Too hard to understand what you are trying to do.
# 5  
Old 04-17-2013
Assuming the space before the 2nd column in the input file(line 1) is a typo:

Code:
awk -F, '{x=$4;$4-=a[$1$2];a[$1$2]=x;}1'  OFS=, file

If not a typo, use this:
Code:
awk -F, '{gsub(/^ */,"",$2);x=$4;$4-=a[$1$2];a[$1$2]=x;}1'  OFS=, file

Guru.
# 6  
Old 04-17-2013
Thanks a lot, Guru.
It works.
Thanks again.

Can you please explain your code snippet ?

---------- Post updated at 04:20 AM ---------- Previous update was at 03:54 AM ----------

How will I do if i have to find out the delta of multiple columns.
Thanks in Advance
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

3. Shell Programming and Scripting

awk add all columns if column 1 name matches

Hi - I want to add all columns if column1 name matches. TOPIC1 5 1 4 TOPIC2 3 2 1 TOPIC3 7 2 5 TOPIC1 6 3 3 TOPIC2 4 1 3 TOPIC3 9 5 4 . . . . . . . . . . . . Result should look like TOPIC1 11 4 7 TOPIC2 7 3 4 (1 Reply)
Discussion started by: oraclermanpt
1 Replies

4. 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

5. 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

6. Shell Programming and Scripting

How to get difference of the same column between two files when other column matches?

File 1: 20130416,235800,10.78.25.104,BR2-loc,60.0,1624,50.0,0,50.0,0 20130416,235800,10.78.25.104,BR1-LOC,70.0,10,50.0,0,70.0,0 20130416,235800,10.78.25.104,Hub_None,60.0,15,60.0,0,50.0,0 File 2: 20130417,000200,10.78.25.104,BR2-loc,60.0,1626,50.0,0,50.0,0... (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

7. Shell Programming and Scripting

[Solved] awk Column difference

Hi, I've got what is probably quite an easy little (presumably) awk problem that I just can't seem to work out (mental block...I've already spent ages getting the data into this format!). I want to work out the difference between rows for certain columns. for example: 1359142876 RED 14... (3 Replies)
Discussion started by: chrissycc
3 Replies

8. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
3 Replies

9. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

10. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies
Login or Register to Ask a Question