How to highlight unrivaled data on the right in the same file using fortran or shell?
After a lot of effort, I could not find a suitable team to get my request. I have two different data files with one column but unequal lengths. Part of my both files:
file1.dat file2.dat
23.99 23.99
45.950 45.951
5 6
23 23
43
34
I want to highlight unmatched data up to 2 decimal places in the same files as 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
Several tools do this for you (tkdiff, meld, vimdiff come to mind). However, SO is not a place for making tool lists, but for answering questions about how to solve problems.
vimdiff is not the same as diff. This is a script using vim to display the differences that may be available. There are several examples that a web search for displays a highlighted diff . Some tools are mentioned on these pages:
- Best UNIX / Linux File Delimitation Tools - Diff, Colordiff, Wdiff, Vimdiff
- Useful but unknown Unix tools: how wdiff and colordiff can help you choose the right swiss army knife
- Useful but unknown Unix tools: dwdiff is better than wdiff + colordiff
But there will be fewer sources of information on how to create a program that does this.
Some may use wdiff
internally, for example. It is not (and was not) the only tool of its kind, as I noted here , but it is well known. Some (like numdiff ) don't use wdiff
). Consider a later version spiff
(which is still available, although I remember having problems with it).
If I was developing a script to make requests, I would recommend that it outputs the output in diff -u
. This will allow you to reuse the program colordiff
, solving about half of the problem. Then I would do the following:
- filter each input file into a temporary file, discarding digits beyond the second decimal point,
- run
diff -u
in temporary files, - return the result from
diff -u
and use the line number information to concatenate the original lines.
This last step will take an experienced developer 2-3 hours to write, so presenting an example is out of scope.
source to share
I made a simple script, it can help you or someone else first we will combine file1.dat with file2.dat and output both files to output.dat file with two columns as if it looks like
|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 to compare between two columns with highlighted arrows using
awk -F : '{if(!($1==$2))printf("%s", "'$RED'=>'$NORMAL'not");print" matched ",$0}' output.dat
highlighted arrow colors
RED=`echo -e '\033[41m\033[37m'`
NORMAL=`echo -e '\033[0m'`
the result will be like
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
source to share