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!

*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.

Jobs

Hi, I need help to update values

Hi, I need help to update values

(OP)
Hi,

I need help to update values in my file1.txt. I need a script to check and match the ID in file1.txt and match against ID in file2.txt. If matched, the value for START should be updated by the values in $3 (file2.txt) and remains those unmatched. I tried a code something like this:

CODE -->

awk  'NR==FNR{if ($0~/exNum 1/) {rp[++cnt]=$3};next};
      /START/    {$2 = rp[++xtr]}
      1
    ' file2 file1 

but, my output screwed as there are unmatched IDs that i need them to be remained unchanged in the file1.txt

My files are-

file1.txt

CODE -->

ID    P_200 
START    12412 
END    12444 
// 
ID    P_6  
START   235411   
END    18763  
//  
ID    P_10  
START    631012  
END    32814  
//  
ID    P_60 
START    3112 
END    3281 
// 
ID    P_9 
START    5812 
END    6112 
// 
ID    P_133 
START    389417  
END    314124  
// 

file2.txt

CODE -->

ex    37193    37735    P_10    
S     37193    37735    P_10     exNum 5
ex    37862    38019    P_10    
S     37862    38019    P_10     exNum 4
ex    38076    38835    P_10    
S     38076    38835    P_10     exNum 3
ex    38880    39050    P_10    
S     38880    39050    P_10     exNum 2
ex    39093    39644    P_10    
S     39093    39644    P_10     exNum 1
ex    21204    22151    P_6    
S     21204    22151    P_6     exNum 2
ex    22217    22765    P_6    
S     22217    22765    P_6     exNum 1
ex    42657    42674    P_133    
S     42657    42674    P_133     exNum 1 

The output should remain those unmatched ID and supposed to be like this:

CODE -->

ID    P_200
START    12412
END    12444
@@
ID    P_6 
START   22765 
 END    18763 
@@ 
ID    P_10 
START    39644 
END    32814 
@@
 ID    P_60
START    3112
END    3281
@@
ID    P_9
START    5812
END    6112
@@
ID    P_133
START    42674 
END    314124 
@@ 

The START values for P_6, P_10 and P_133 should be updated with new values from file2.txt, while the values of START for ID P_200, P_60 and P_9 should remain unchanged as there are no match in file2 for all of them. i have thousands of data to work on and your help on this is highly appreciated. Thanks

RE: Hi, I need help to update values

(OP)
Hi PHV,

It worked great!!
But, can you pls explain to me about the script?
Thanks so much for your kind help.

RE: Hi, I need help to update values

CODE

awk '
# process the first file
NR==FNR{
  # for lines containing "exNum 1"
  if($0~/exNum 1/) {
    # save the replacement value, indexed by P_number 
    rp[$4[/color]=$[color #FF0000]3
  }
  # skip to next line and start over
  next
}

# process the second file
# save the P_number
/^ID/{p=$2}
/^START/{
  # if there is a replacement value for this P_number
  if(p in rp) {
    # replace the second field with the replacement value
    $2=rp[p]
  }
}
# print all lines from second file
1
' file2.txt file1.txt 

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: Hi, I need help to update values

(OP)
Hi Annihilannic,

Thanks so much! Now i understand how it works... ;)

RE: Hi, I need help to update values

(Correction of broken TGML in my previous post.)

CODE --> AWK

awk '
# process the first file
NR==FNR{
 # for lines containing "exNum 1"
 if($0~/exNum 1/) {
   # save the replacement value, indexed by P_number 
   rp[$4]=$3
 }
 # skip to next line and start over
 next
}

# process the second file
# save the P_number
/^ID/{p=$2}
/^START/{
 # if there is a replacement value for this P_number
 if(p in rp) {
   # replace the second field with the replacement value
   $2=rp[p]
 }
}
# print all lines from second file
1
' file2.txt file1.txt 

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

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!

Resources

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