Sponsored Content
Top Forums Shell Programming and Scripting compare values in different lines of file Post 302360828 by Scrutinizer on Saturday 10th of October 2009 03:53:41 PM
Old 10-10-2009
Wrench

Hi Christine,

You could give this script a try. It runs with ksh:
Code:
#!/bin/ksh
prevline=""
while read line; do
  pval=${prevline##* }
  val=${line##* }
  if (( val*pval > 0 )); then
    avg=$(( (val+pval)/2 ))
    printf "%s %1.1f\n" "${prevline% *}" $avg
    line=""
  elif [[ $prevline != "" ]]; then
    echo $prevline
  fi
  prevline=$line
done < infile
if [[ $prevline != "" ]]; then
  echo $prevline
fi

Alternatively here is an awk equivalent
Code:
awk '{ if ( pval*$NF > 0 )
       { avg=(pval+$NF)/2
         sub(/ [^ ]*$/,"",prev)
         printf("%s %1.1f\n", prev, avg)
         $0=""
       }
       else if (prev != "")
         { print prev }
       sub(/ *$/,"")
       pval=prev=$0;
       sub(/.* /,"",pval)
     }
     END { if ( prev != "" )
           print prev
     }' infile


Last edited by Scrutinizer; 10-10-2009 at 05:18 PM..
This User Gave Thanks to Scrutinizer For This Post:
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I need to extract last column of a file and compare the values

Hi, I am new to unix and I need help in solving below mentioned issue, really appreciate ur help. I have a file sam, john, 2324, 07142007 tom, thomson, 2343, 07142007 john, scott, 2478, 07142007 its a comma delimited file, I need to extract the last column from each line and this... (4 Replies)
Discussion started by: vukkusila
4 Replies

2. Shell Programming and Scripting

How to compare the values of a column in awk in a same file and consecutive lines..

I would like to compare the values of 2nd column of consecutive lines of same file in such a way so that if the difference between first value and second value is more than 100 it should print complete line else ignore line. Input File ========== PDB 2500 RTDB 123 RTDB-EAGLE 122 VSCCP 2565... (4 Replies)
Discussion started by: manuswami
4 Replies

3. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

4. Homework & Coursework Questions

Compare to values in a file in unix

Here is sample file ===============Index 0=================== isActive=0, Input=1, Output=1, Status=1 State = Future , PRIMARY UnderCount=2 inCount=2 outCount=0 SCount=673 -- ===============Index 1=================== isActive=0, Input=1, Output=1, Status=1 ... (1 Reply)
Discussion started by: sooda
1 Replies

5. Shell Programming and Scripting

How to compare the values of a column in a same file using awk?

Dear Unix experts, I have got a file where I would like to compare the values of second column if first column is same in such a way that the difference between the values is >50. If not, I would like to discard both values. For example, my input file looks like - comp275_c0_seq2 73... (7 Replies)
Discussion started by: utritala
7 Replies

6. UNIX for Beginners Questions & Answers

Compare Values between column in the same file

Input File:- COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 COLUMN6 SMS Email AO Mail Post N Cell VEGE Potato E W 396 12 0 384 0 0 0 0 0 VEGE Onion S W 17 0 17 0 0 0 0 0 0 FRUIT APPLE N W 549 61 0 0 0 0 0 488 0 FRUIT APPLE SE W 291 14 239 38 0 10 0 0 0 FRUIT APPLE EAMS W 397 32 309 56 309 309 0... (27 Replies)
Discussion started by: Nina2910
27 Replies

7. Shell Programming and Scripting

Compare Values in a Delimited Text file

Hi, How do I compare two columns within a text file If 2nd column values are same then I want to know 3rd column number matches or not Example: Prod Stag1 1234.79 Prod Stag2 1234.79 20 Prod Stag3 1234.79 30 Prod Stag4 1234.79 UAT Stag1 1243.56 UAT Stag2 1243.56 20 UAT ... (3 Replies)
Discussion started by: krux_rap
3 Replies

8. UNIX for Beginners Questions & Answers

Awk: compare values in two columns of the same file

I'm trying to learn awk, but I've hit a roadblock with this problem. I have a hierarchy stored in a file with 3 columns: id name parentID 4 D 2 2 B 1 3 C 1 1 A 5 I need to check if there are any values in column 3 that are not represented anywhere in column 1. I've tried this: awk '{arr;}... (7 Replies)
Discussion started by: kaktus
7 Replies
PREV(1) 							     [nmh-1.5]								   PREV(1)

NAME
prev - show the previous message SYNOPSIS
prev [+folder] [-showproc program] [-showmimeproc program] [-header | -noheader] [-checkmime | -nocheckmime] [switches for showproc or showmimeproc] [-version] [-help] DESCRIPTION
Prev performs a show on the previous message in the specified (or current) folder. Like show, it passes any switches on to the program named by showproc or showmimeproc, which is called to list the message. This command is almost exactly equivalent to "show prev". Consult the manual entry for show(1) for all the details. FILES
$HOME/.mh_profile The user profile PROFILE COMPONENTS
Path: To determine the user's nmh directory Current-Folder: To find the default current folder showproc: Program to show non-MIME messages showmimeproc: Program to show MIME messages SEE ALSO
show(1), next(1) DEFAULTS
`+folder' defaults to the current folder `-checkmime' `-header' CONTEXT
If a folder is specified, it will become the current folder. The message that is shown (i.e., the previous message in sequence) will become the current message. BUGS
prev is really a link to the show program. As a result, if you make a link to prev and that link is not called prev, your link will act like show instead. To circumvent this, add a profile-entry for the link to your nmh profile and add the argument prev to the entry. MH.6.8 11 June 2012 PREV(1)
All times are GMT -4. The time now is 02:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy