Computing difference based on line contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Computing difference based on line contents
# 1  
Old 04-12-2013
Computing difference based on line contents

I have the following awk script set up to copy the contents of a line that contains 0008 in each line that contains values of 1895 through 2012.

Code:
awk -v OFS="   " '{val=0+substr($1,length($1)-3,4);if(val==0008){print;$1=x;y=$0}else{if(val>=1895&&val<=2012){print $1 y}else{print}}}'

Output data -
Code:
XXXXXXXXX0007   38.3   42.6   51.3   60.1   67.7   75.1   78.4   77.6   71.6   60.2   50.8   42.3   59.7
XXXXXXXXX0008   39.5   43.8   51.7   59.5   67.7   75.2   78.9   77.8   71.7   60.5   50.7   42.6   60.0
XXXXXXXXX1895   39.5   43.8   51.7   59.5   67.7   75.2   78.9   77.8   71.7   60.5   50.7   42.6   60.0
XXXXXXXXX1896   39.5   43.8   51.7   59.5   67.7   75.2   78.9   77.8   71.7   60.5   50.7   42.6   60.0

How would I modify this if I wanted to subtract the values in 0008 from the values in 1895-2012?

Input data -
Code:
XXXXXXXXX0007   38.3   42.6   51.3   60.1   67.7   75.1   78.4   77.6   71.6   60.2   50.8   42.3   59.7
XXXXXXXXX0008   39.5   43.8   51.7   59.5   67.7   75.2   78.9   77.8   71.7   60.5   50.7   42.6   60.0
XXXXXXXXX1895   38.5   33.0   50.0   60.7   67.2   77.2   78.5   79.2   76.8   56.0   50.4   43.4   59.2
XXXXXXXXX1896   41.0   43.9   47.9   66.8   74.2   75.6   79.9   81.5   72.5   60.0   53.7   43.4   61.7

# 2  
Old 04-12-2013
It's kind of unclear to me what you are trying to do. Maybe others can understand. If nobody else responds, could you restate the problem more clearly?
# 3  
Old 04-12-2013
Applying the usually needed portion of clairvoyance, I'd propose
Code:
awk     '               {val = substr($1,length($1)-3,4) + 0}
         val == 0008    {n = split ($0, DI)}
         val >  1894 &&
         val <  2013    {for (i=2; i<=n; i++) $i-=DI[i]; $0 = $0}
         1
        ' OFS="   " file
XXXXXXXXX0007   38.3   42.6   51.3   60.1   67.7   75.1   78.4   77.6   71.6   60.2   50.8   42.3   59.7
XXXXXXXXX0008   39.5   43.8   51.7   59.5   67.7   75.2   78.9   77.8   71.7   60.5   50.7   42.6   60.0
XXXXXXXXX1895   -1   -10.8   -1.7   1.2   -0.5   2   -0.4   1.4   5.1   -4.5   -0.3   0.8   -0.8
XXXXXXXXX1896   1.5   0.1   -3.8   7.3   6.5   0.4   1   3.7   0.8   -0.5   3   0.8   1.7

# 4  
Old 04-17-2013
Quote:
Originally Posted by hanson44
It's kind of unclear to me what you are trying to do. Maybe others can understand. If nobody else responds, could you restate the problem more clearly?

Apologies for the fuzziness nature of my question. Ill try to restate it below.

I need each tab dilemented column (2 through 14) to be subtracted from the same tab dilemented column in the line that contains 0008.

So from the below code
Code:
XXXXXXXXX0008   39.5   43.8   51.7   59.5   67.7   75.2   78.9   77.8   71.7   60.5   50.7   42.6   60.0
XXXXXXXXX1895   38.5   33.0   50.0   60.7   67.2   77.2   78.5   79.2   76.8   56.0   50.4   43.4   59.2

The output would be
Code:
XXXXXXXXX0008   39.5   43.8   51.7   59.5   67.7   75.2   78.9   77.8   71.7   60.5   50.7   42.6   60.0
XXXXXXXXX1895   -1.0   -10.8   -1.7     1.2   -0.5     2.0    -0.4     1.4     4.9......

---------- Post updated at 09:44 AM ---------- Previous update was at 08:32 AM ----------

Quote:
Originally Posted by RudiC
Applying the usually needed portion of clairvoyance, I'd propose
Code:
awk     '               {val = substr($1,length($1)-3,4) + 0}
         val == 0008    {n = split ($0, DI)}
         val >  1894 &&
         val <  2013    {for (i=2; i<=n; i++) $i-=DI[i]; $0 = $0}
         1
        ' OFS="   " file
XXXXXXXXX0007   38.3   42.6   51.3   60.1   67.7   75.1   78.4   77.6   71.6   60.2   50.8   42.3   59.7
XXXXXXXXX0008   39.5   43.8   51.7   59.5   67.7   75.2   78.9   77.8   71.7   60.5   50.7   42.6   60.0
XXXXXXXXX1895   -1   -10.8   -1.7   1.2   -0.5   2   -0.4   1.4   5.1   -4.5   -0.3   0.8   -0.8
XXXXXXXXX1896   1.5   0.1   -3.8   7.3   6.5   0.4   1   3.7   0.8   -0.5   3   0.8   1.7

RudiC

Thanks! That seems to work, but what is the syntax to preserve the decimal. I need to keep the precision to the tenths.

XXXXXXXXX1895 -1.0 -10.8 -1.7 1.2 -0.5 2.0 -0.4 1.4 5.1 -4.5 -0.3 0.8 -0.8
# 5  
Old 04-17-2013
You can control that with the CONVFMT variable.

Code:
awk     '               {val = substr($1,length($1)-3,4) + 0}
         val == 0008    {n = split ($0, DI)}
         val >  1894 &&
         val <  2013    {for (i=2; i<=n; i++) $i-=DI[i]; $0 = $0}
         1
        ' OFS="   " CONVFMT="%.2f" file

# 6  
Old 04-17-2013
Quote:
Originally Posted by Corona688
You can control that with the CONVFMT variable.

Code:
awk     '               {val = substr($1,length($1)-3,4) + 0}
         val == 0008    {n = split ($0, DI)}
         val >  1894 &&
         val <  2013    {for (i=2; i<=n; i++) $i-=DI[i]; $0 = $0}
         1
        ' OFS="   " CONVFMT="%.2f" file

Corona,

That did not seem to add in the .0 for those whole values.

Code:
awk '{val = substr($1,length($1)-3,4) + 0} val == 0008 {n = split ($0, DI)} val > 1894 && val < 2014 {for (i=2; i<=n; i++) $i-=DI[i]; $0 = $0} 1 ' OFS="\t" CONVFMT="%.1f" input > output

Code:
XXXXXXXXX1895   -1      -10.8   -1.7    1.2     -0.5    2       -0.4    1.4     5.1     -4.5    -0.3    0.8     -0.8

# 7  
Old 04-18-2013
Replace $i-=DI[i] by $i=sprintf("%.1f", $i-DI[i]);
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove a line based on contents of the first column?

Good day all. Using basic UNIX/Linux tools, how would you delete a line based on a character found in column 1? For example, if the CITY name contains an 'a' or 'A', delete the line: New York City; New York Los Angeles; California Chicago; Illinois Houston; Texas Philadelphia;... (3 Replies)
Discussion started by: BRH
3 Replies

2. Shell Programming and Scripting

Help with computing median line by line of several files.

I have several files which look like this. The files are much longer than this, I have just cut out the data and there are hundreds of these files file 1 0.00 5.905 0.05 5.740 0.10 5.570 0.15 5.440 0.20 5.310 0.25 5.185 0.30 5.075 0.35 5.897 0.40 ... (3 Replies)
Discussion started by: malandisa
3 Replies

3. Shell Programming and Scripting

File comparison based on contents

Hi I have 2 files 1.del ---- 1,2,3,4,5 1,2,3,4,4 1,1,1,1,2 2.del ---- 1,2,3,4,5 1, 1,2,3,4,4 1,1,1,1,2 I need to compare the above two files in unix, as in the output should only tell the difference in contents as I should get only the line 1 ( from 2.del) , rest all lines are... (4 Replies)
Discussion started by: Ethen561
4 Replies

4. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

5. Shell Programming and Scripting

move contents from one file to another based on line number or content

I want a script that will move everything beyond a certain line number or beyond a certain content word into another file. For example, if file A has this: first line second line third line forth line fifth line sixth line I want to run a script that will move everything beyond the third... (4 Replies)
Discussion started by: robp2175
4 Replies

6. Shell Programming and Scripting

Manipulating word based off of contents

Hello everyone, my first post here, please feel free to inform me if my question can be better formatted so my future posts may be more clear. I have a large text file which I need parsed in one specific way, I have done the rest of the processing, I am simply lacking the last aspect of such. ... (8 Replies)
Discussion started by: ryanfx
8 Replies

7. Shell Programming and Scripting

Merging files based on the contents

Hi, I have a file f1 having the contents as below select (<condn>) from dual I have another file f2 having the contents as below 1, 2, 3 I want to replace <condn> in f1 with the contents of f2 I tried using sed like this sed "s:<condn>:`cat f2`:g" f1 The above command resulted in sed:... (3 Replies)
Discussion started by: mr_manii
3 Replies

8. Programming

What is the difference between console-based and xwindow-based application?

Hi everyone, What is the difference between console-based and Xwindow-based application? Can I say the application working well in Xwindows can work in console? Can I say the application working well in console cann't work in Xwindow perhaps. Eg, ncurses is console-based and Imlib2 is... (4 Replies)
Discussion started by: liuyan03
4 Replies

9. Solaris

make sure HA server no difference contents

hello there. I would like to know how can I make sure HA server have exactly same contents. for example at timestamp 1 (before start install oracle product ) assume the both server have exactly same contents. at timestamp 2 I install Oracle product at both server, hope... (3 Replies)
Discussion started by: qyxiell
3 Replies
Login or Register to Ask a Question