Script to subtract rows HELP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to subtract rows HELP
# 1  
Old 11-18-2010
Script to subtract rows HELP

Dear All,

Please help with a script which can accomplish the following:

Input table:

$1 $2 $3

Student1 1 50
Student2 56 75
Student3 77 100

Desired Output:

$1 $2 $3 $4

Student1 1 50
Student2 56 75 6
Student3 77 100 2

i want to subtract 56 ($2) - 50($3) = 6 ($4); 77($2) - 75($3) = 2 ($4)and print the difference as $4. i have a list running 1000s of rows.

Please help me to solve this.
# 2  
Old 11-18-2010
try this,
Code:
awk '{if(NR==1){a=$3;print}else {print $0,$2-a;a=$3}}' inputfile

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 11-18-2010
Quote:
Originally Posted by pravin27
try this,
Code:
awk '{if(NR==1){a=$3;print}else {print $0,$2-a;a=$3}}' inputfile

Thanks, it works!
# 4  
Old 11-19-2010
Code:
awk 'x{$0=$0FS ($2-x)}{x=$3}1' file

This User Gave Thanks to danmero For This Post:
# 5  
Old 05-08-2011
This helped me today. Thank you pravin27 :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to subtract time stored in a variable

Hello, I am writing a script to find time difference between two timestamp stored in a variable. i have two variable t1=11:48:30 t2=13:13:48 how i can find the difference i.e t2-t1 in seconds. Please help (4 Replies)
Discussion started by: anand2308
4 Replies

2. Shell Programming and Scripting

Subtract two rows (in Time format)

Hello all, I have written sth like this: #!/bin/bash grep -e XXX -e YYYY myfile.log | grep -v ZZZ | awk '{print $1 " " $2 ";" $3 ";" $9 ";" $11}' > myfile.csv sed -i '1iDate;Time;From;To' myfile.csv => it is clear that it converts log to csv and add a header. Now I want to subtract row... (4 Replies)
Discussion started by: frhling
4 Replies

3. Shell Programming and Scripting

Find the script running time and subtract from sleeptime

HI Guys, I want to find out the script running time and subtract from sleeptime. My Script Below Give me error :- #!/usr/bin/ksh timeout=100 start=$SECONDS sleep 20 end=$SECONDS echo "Time: $((end - start)) " ScTime = $((end - start)) (1 Reply)
Discussion started by: asavaliya
1 Replies

4. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

5. Linux

help with columns and rows - script

Hi everyone, how can I convert a file with 3375 rows and 6 columns to a file with 1350 rows and 15 columns by using a script? Is it possible with awk or something like that? Any help is much appreciated. Thanks. D. (5 Replies)
Discussion started by: DKalfileritos
5 Replies

6. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

7. UNIX for Dummies Questions & Answers

Subtract -1 from a date in Unix script

Hi I have a date in a variable in Unix script and have to subtract -1 from the date. fromdate='07/13/2009' Now we want to subract one day from it i.e the end result should be '07/12/2009' Any suggestions? Thanks (2 Replies)
Discussion started by: akashtcs
2 Replies

8. Shell Programming and Scripting

script for add and subtract two hours

i find a script to calculate hours of job i(nclude extraordinary) i make a script to calculate add and subtract two hours (format hh:mm:ss) (7 Replies)
Discussion started by: ZINGARO
7 Replies

9. UNIX for Dummies Questions & Answers

Subtract date & time in diferent rows

Hi Friends :) I have a long file having fields in the form : Field1 yy/mm/dd hh:mm:ss Duration(Sec) line 1) 123123 05/11/30 12:12:56 145 line 2) 145235 05/11/30 12:15:15 30 line 3) 145264 05/11/30 13:14:56 178 . . I want to subtract yy/dd/dd hh:mm:ss in line (2) from yy/mm/dd hh:mm:ss in... (1 Reply)
Discussion started by: vanand420
1 Replies

10. UNIX for Dummies Questions & Answers

How to subtract 2 hours from 'date' in shell ( /bin/sh ) script ?

I write a sh script that zip and copy to tape all files that older then 2 hours. 1. The way I choose is - touch a file with "now - 2 hours", then use fine with '! -newer' 2. Do you have any other idea to do it ? tnx. (1 Reply)
Discussion started by: yairon
1 Replies
Login or Register to Ask a Question