bugenwilla
Systems Engineer
hello,
I have 3 files with : separated fields.
main:
file1:
file2:
I want to update 'main' file lines with corresponding (identified by $1) lines from
'file*' but only when $3 value of the line is biggest and greater than in 'main', and following conditions are met:
1) If $3 in more than 1 file* are bigger than in main, but equal, only one such line should be taken for update of the main.
2) All lines which exist only in 'main' file should stay there unchanged after processing.
3) All file* lines having non-digits in $2 should be ignored (in example -)
4) Lines with $1 which exist in file* but but not in main, should be ignored.
Why below code returns also 'thr' line which ($1) does not exist in the main file and besides has non digit on $2?
I have 3 files with : separated fields.
main:
Code:
one:111:222:333
fiv:333:222:333
two:123:234:500
ten:233:422:452
file1:
Code:
one:111:222:333
two:123:234:501
file2:
Code:
one:111:222:333
thr:-:234:232
fiv:999:500:232
I want to update 'main' file lines with corresponding (identified by $1) lines from
'file*' but only when $3 value of the line is biggest and greater than in 'main', and following conditions are met:
1) If $3 in more than 1 file* are bigger than in main, but equal, only one such line should be taken for update of the main.
2) All lines which exist only in 'main' file should stay there unchanged after processing.
3) All file* lines having non-digits in $2 should be ignored (in example -)
4) Lines with $1 which exist in file* but but not in main, should be ignored.
Why below code returns also 'thr' line which ($1) does not exist in the main file and besides has non digit on $2?
Code:
$ awk -F':' -vf=main 'FILENAME==f{m=$0};FILENAME!=f&&$2~/[0-9]+/{if ($2~/[0-9]+/&&(!($1 in a) || $3 > a[$1])) { a[$1] = $3; b[$1] = $0 } next;}{if (($1 in a) && (a[$1] > $3)){ print b[$1]":updated:"m; delete b[$1] } else print; }' file* main
thr:-:234:232
one:111:222:333
fiv:999:500:232:updated:fiv:333:222:333
two:123:234:500
ten:233:422:452