I want to make sure a file follows the following pattern:... ... ... foo ... foo_KO ... ... ... bar ... ... ... ... bar_KO ...The file is extremely long, so, in other words, I want to make sure of the following rule, in sed terms:Every time ([a-z]*)_KO is matched, there is another field on the same line whose value is \1.I hoope I'm clear enough. I'm on Solaris 10, using ksh.Example:intput... ... ... foo ... foo_KO ... ... ... bar ... ... ... ... bar_KO ...outputvalidinput... ... ... foo ... foo_KO ... ... ... bar ... ... ... ... bar_KO ...... fubar_KO ... ... ... ... ... outputinvalid line 3 orunmatched pattern fubar_KO
Detect pattern repetition in a file
shell;sed;regular expression
This will output the line numbers of invalid lines:sed -n '/\([a-z]\+\).*\1_KO/b;/\([a-z]\+\)_KO.*\1/b;=' input_fileBut not deals with multiple _KO's on one line.This should be more reliable and portable:awk '{for(i=1;i<=NF;i++)if((s=$i)~/^([a-z]+)_KO/){sub(/_KO$/,,s);o=0;for(j=1;j<=NF;j++)if($j==s)o=1;if(!o)printfline %d unmatched %s\n,NR,s}}' input_fileThis is trickier, more efficient and supports multiple patterns, but as depends on sorting, locale settings may affect its success:ruby -nae '$F.sort!.select!{|v|v=~/^[a-z]+(_KO)?$/};$F.each_with_index{|v,k|putsline #{$.} unmatched #{v}if v[/^([a-z]+)_KO$/]and$F[k-1]!=$1}' input_fileBasically the same as the above awk solution, but looks less verbose in perl:perl -nae 'for$k(grep/^[a-z]+_KO$/,@F){printline $. unmatched $k\nunless grep{$_ eq substr$k,0,-3}@F}' input_file