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?
# 8  
Old 02-23-2010
Code:
awk 'NR==1{key=$0} NR>1{print $0-key;key=$0;}'

# 9  
Old 02-24-2010
Another way...

Code:
awk '{a[FNR]=$0;b[FNR]=$0}END{for(i=1;i<=FNR-1;i++) print b[i+1]-a[i]}'

# 10  
Old 02-24-2010
Quote:
Originally Posted by protocomm
Another way...

Code:
awk '{a[FNR]=$0;b[FNR]=$0}END{for(i=1;i<=FNR-1;i++) print b[i+1]-a[i]}'


Hello, protocomm:

In this case, since they are not being modified, there is no point in having two identical copies of an array. You can simplify your approach to the following:

Code:
awk '{a[FNR]=$0}END{for(i=1;i<FNR;i++) print a[i+1]-a[i]}'

Regards,
Alister
# 11  
Old 02-24-2010
it's true ... thanx
# 12  
Old 03-02-2010
Hello All,

Thank you very much for your replies. Its working fine.

In addition to the above query, I would like to extend the same from 'n' columns. That is, I have a file with 100 lines with 1000 columns, I would like to subtract each line with each other.

HTML Code:
For example, (with 5 lines and 5 columns) 

Input
          Col1          Col2          Col3             Col4          Col5
line1      A1            B1            C1                D1            E1
line1      A2            B2            C2                D2            E2
line1      A3            B3            C3                D3            E3
line1      A4            B4            C4                D4            E4
line1      A5            B5            C5                D5            E5
I would like to subtract line1 with line1, line2, line3, line4 and line5, line2 with line1, line2, line3, line4 and line5, line3 with....so on ....
(all against each other)

Can any one help me in the regard?

Expecting your reply and thanks in advance.

Warm regards
Fredrick.
# 13  
Old 03-02-2010
Fredrick, isn't your most recent question also being asked here? Is it homework? It really sounds like a homework question...
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