How to subtract the adjacent lines from a single column?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to subtract the adjacent lines from a single column?
# 1  
Old 02-23-2010
How to subtract the adjacent lines from a single column?

Hi All,

I have a file with only one column and i need to subtract the adjacent lines of the same column and print it in the same column.

For Example:
(Input)
HTML Code:
Col1

5
10
12
6
9
12
5
.
.
.
.
output should be like this:
HTML Code:
Col1

5
2
-6
3
3
-7
.
.
.
.
Can anyone help me in this regard?

Expecting your reply and Thanks in advance.

Warm regards
Fredrick.
# 2  
Old 02-23-2010
Try this:

Code:
awk 'p{print $0-p}{p=$0}' file


As always, we also appreciate any reaction from the Original Poster after an answer, thank you.

Last edited by Franklin52; 02-23-2010 at 09:29 AM..
# 3  
Old 02-23-2010
Frank, Can you please brief how this awk code works?
# 4  
Old 02-23-2010
One in perl..

Code:
perl -e '@A=<>; while (++$i <=$#A ) { print $A[$i]-$A[$i-1],"\n"; }' file

For frankin's snippet,

Code:
awk 'p{print $0-p}{p=$0}' file

For the first time, as p will be valuated as FALSE and will not execute {print $0-p}. However, p will turn to a non-zero/TRUE value when it execute {p=$0}.From second record onwards, p will be TRUE and will evaluate {print $0-p} and print it.Also, it will set the new value(current line) to p

Last edited by dennis.jacob; 02-23-2010 at 12:51 PM..
# 5  
Old 02-23-2010
I got it. Thanks Dennis.
# 6  
Old 02-23-2010
Franklin52's solution is probably the best, simplest, and shortest there is. However, that shouldn't stop unix userland lovers from having some fun Smilie

Code:
sed 'x;G;1d;y/\n/+/;s/.*/eval(-&)/' data | m4

Only works with integers, as opposed to the AWK which will handle floating point as well, and assumes that the data file consists of nothing but one integer per line.

Code:
$ cat data
5
10
12
6
9
12
5

$ sed 'x;G;1d;y/\n/+/;s/.*/eval(-&)/' data | m4
5
2
-6
3
3
-7

Cheers,
Alister
# 7  
Old 02-23-2010
Quote:
Originally Posted by Franklin52
Try this:

Code:
awk 'p{print $0-p}{p=$0}' file


As always, we also appreciate any reaction from the Original Poster after an answer, thank you.
If we have zero in input file then we will not get the difference.

Code:
awk 'NR-1{print $0-p}{p=$0}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

2. Shell Programming and Scripting

Search Pattern and Print lines in Single Column

Hi Experts I have small query where I request the into a single file Suppose: File1: {Unique entries} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 Replies

3. Shell Programming and Scripting

Compare & subtract lines in files by column using awk.

I have two files with similar column pattern as given below : 2 sample lines from file1 are given below. 18 12630 . G T 49.97 . AC=2;AF=1.00;AN=2;DP=3;Dels=0.00;FS=0.000;HRun=0;HaplotypeScore=0.0000;MQ=60.00;MQ0=0;NDA=1;QD=16.66;SB=-0.01 GT:AD:DP:GQ:PL ... (2 Replies)
Discussion started by: vaibhavvsk
2 Replies

4. Shell Programming and Scripting

Adjacent row and column check in Perl

HI, i am new to perl world. And i am trying to compress a file, as given below procedure. INPUT FILE: 1 1 2 1 ==> R1 2 1 3 1 ==> R2 3 1 4 1 ==> R3 OUTPUT FILE: 1 1 4 1 (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

5. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

6. Shell Programming and Scripting

Merging Adjacent Lines Using Gawk

Hi all, I have a text file consisting of 4 columns. What I am trying to do is see whether column 2 repeats multiple times, and collapse those repeats into one row. For example, here is a snippet of the file I am trying to analyze: 1 Gamble_Win 14.282 0.502 1 Sure_Thing 14.858 0.174 1... (4 Replies)
Discussion started by: Jahn
4 Replies

7. Shell Programming and Scripting

Copy the data column to adjacent column

Hi, i have file which contains only one record like below a|b|c|.... The no of columns is not known. The expected output is as below: a|a|b|b|c|c|... Please provide me your suggestions. OS: unix Thanks, Selva (7 Replies)
Discussion started by: bharathappriyan
7 Replies

8. Shell Programming and Scripting

paste each 10 lines of single column to several column

Hi, I need to paste each 10 lines of single column to several columns. Please, can anyone tell me how to write in awk? Input File: 22 34 36 12 17 19 15 11 89 99 56 38 29 (4 Replies)
Discussion started by: nica
4 Replies

9. Shell Programming and Scripting

Awk multiple lines with 3rd column onto a single line?

I have a H U G E file with over 1million entries in it. Looks something like this: USER0001|DEVICE001|VAR1 USER0001|DEVICE001|VAR2 USER0001|DEVICE001|VAR3 USER0001|DEVICE001|VAR4 USER0001|DEVICE001|VAR5 USER0001|DEVICE001|VAR6 USER0001|DEVICE002|VAR1 USER0001|DEVICE002|VAR2... (4 Replies)
Discussion started by: SoMoney
4 Replies

10. UNIX for Dummies Questions & Answers

print adjacent lines

how do you print the lines before and after the line you are interested in? Example: Line to be printed: line 344 Output: line 343 line 344 line 345 Thanks (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question