×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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 



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

Hi

Because you skip the processing of main file only if the current line comes from a file* and its 2nd field contains at least a digit. As the if condition checks the 2nd field again, is safe to remove that part from the pattern, allowing all file* lines to reach the next and skip the main specific processing :

CODE --> Awk

# remove this ---------------vvvvvvvvvvvvv
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; } 

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close