Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

updating a main file lines with lines from the other files with some conditions 1

Status
Not open for further replies.

bugenwilla

Systems Engineer
Apr 16, 2018
1
PL
hello,
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



 
Hi

Because you skip the processing of main file only if the current line comes from a file* and its 2[sup]nd[/sup] field contains at least a digit. As the [tt]if[/tt] condition checks the 2[sup]nd[/sup] field again, is safe to remove that part from the pattern, allowing all file* lines to reach the [tt]next[/tt] and skip the main specific processing :
Code:
[gray]# remove this ---------------vvvvvvvvvvvvv[/gray]
FILENAME==f{m=$0};FILENAME!=f[COLOR=red yellow][s]&&$2~/[0-9]+/[/s][/color]{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; }


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top