user4933588
user4933588

Reputation:

How to highlight the unmatched data to its right in the same file using fortran or shell?

After a lot of effort I could not find any suitable command to get my requirement. I have two different data files with one column but unequal lengths. Part of my both files are:

file1.dat   file2.dat
 23.99       23.99
 45.950      45.951
 5           6
 23          23
             43
             34

I am looking to highlight the unmatched data up to 2 decimal point in same files as given below:

file1.dat                   
 23.99               
 45.950              
 5        not present
 23                  

file2.dat
 23.99
 45.951
 6        not present
 23       
 43       not present
 34       not present

Upvotes: 3

Views: 174

Answers (2)

Thomas Dickey
Thomas Dickey

Reputation: 54563

Several tools do this for you (tkdiff, meld, vimdiff come to mind). However, SO is not a place to make lists of tools, but to answer questions on how to solve problems.

vimdiff is not the same as diff. It is a script using vim to display differences, which you may have available. There are several examples which a web search on display highlighted diff would find easily. These pages mention some tools:

But there would be fewer sources of information on how to construct a program which does this.

Some might use wdiff internally, for instance. It is not (and was not) the only tool of this kind, as I noted here, but is well-known. Some (such as numdiff) do not use wdiff). Consider it a later version of spiff (which is still accessible, though I recall problems with it).

If I were developing a script to do what was requested, I would recommend that it produce output in diff -u format. That would let you reuse the colordiff program, solving about half the problem. Then, I would do this:

  • filter each of the input files to a temporary file, dropping the digits past the second decimal point,
  • run diff -u on the temporary files,
  • read back the result from diff -u and use the line number information to merge in the original lines.

That last step would take an experienced developer 2-3 hours to write, so presenting an example is out of scope.

Upvotes: 3

auth private
auth private

Reputation: 1314

i did simple script it can help you or other first we will combine file1.dat with file2.dat and output both to file called output.dat with two columns seems to be like this

|23.99 : 23.99|
|45.950 : 45.951|
|5 : 6|
|23 : 23|
| : 43|
| : 34|

with paste with awk

:|paste -d ' :  ' file1.dat - file2.dat > output.dat | awk -F '|' '{print "|" $1 "|" $8}' output.dat

then going to compare between two columns with highlighted arrows with

awk -F : '{if(!($1==$2))printf("%s", "'$RED'=>'$NORMAL'not");print" matched ",$0}'  output.dat

colors of highlighted arrows

RED=`echo -e '\033[41m\033[37m'`
NORMAL=`echo -e '\033[0m'`

the result will be like this

 present  23.99 :23.99
=>not present  45.950 :45.951
=>not present  5 :6
 present  23 :23
=>not present   :43
=>not present   :34

full script :

#!/bin/bash
RED=`echo -e '\033[41m\033[37m'`
NORMAL=`echo -e '\033[0m'`


:|paste -d ' :  ' file1.dat - file2.dat > output.dat | awk -F '|' '{print "|" $1 "|" $6}' | awk -F : '{if(!($1==$2))printf("%s", "'$RED'=>'$NORMAL'not");print" present ",$0}'  output.dat

Upvotes: 3

Related Questions