Match and replace value in 2 different places using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match and replace value in 2 different places using awk
# 1  
Old 03-17-2011
Match and replace value in 2 different places using awk

Hi,

I need help on replacing values in certain field in my file1.txt based on matched patterns in file2.txt using awk.

The blue color need to match with one of the data in field $2 in file2.txt. If match, BEGIN and FINISHED value in red will have a new value from field $3 and $4 accordingly.


file1.txt

Code:
ID   N_4736
NAME   N_4736
BEGIN  544
FINISHED  550
COM    Safe  

ID    N_32
NAME   N_32
BEGIN    872
FINISHED   1027
COM   Crash

Input file2.txt
Code:
test1     4754    120      160
test1     4736    500      600
test1     5487    1478     200      
test1     32      800      1000

The output should look like this:-

Code:
D   N_4736
NAME   N_4736
BEGIN  500
FINISHED   600
COM    Safe  

ID    N_32
NAME   N_32
BEGIN    800
FINISHED  1000
COM   Crash

i tried couple of codes that i found and do necessary modification but it's either gave me nothing or just display the same data again. I really don't know how to do this. Smilie appreciate your kind help on this. thanks
# 2  
Old 03-17-2011
nawk -f red.awk file2.txt file1.txt

red.awk:
Code:
FNR==NR{b[$2]=$3;f[$2]=$4;next}
$1=="ID" {id=substr($2,index($2,"_")+1)}
id in b {$2=($1=="BEGIN")?b[id]:(($1=="FINISHED")?f[id]:$2)}
1

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 03-17-2011
Code:
sed '1{x;p;x;}' f1| awk 'NR==FNR{a["N_"$2]=$3;b["N_"$2]=$4;} NR>FNR{RS=""; if ($2 in a) {$6=a[$2];$8=b[$2];for(i=1;i<=NF;i++){if(i%2==1)$i="\n"$i;}print $0}}' f2 - >output.txt

This User Gave Thanks to sk1418 For This Post:
# 4  
Old 03-18-2011
Hi Both,

i tried both methods and it seems that vgersh99 script works perfect to my issue.. Thanks so much guys!! Smilie)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum the fields with 6 decimal places - getting only 2 decimal places as output

I used the below script to Sum up a field in a file based on some unique values. But the problem is when it is summing up the units, it is truncating to 2 decimals and not 6 decimals as in the input file (Input file has the units with up to 6 Decimals – Sample data below, when the units in the 2... (4 Replies)
Discussion started by: brlsubbu
4 Replies

2. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

awk Match First Field and Replace Second Column

Hi Friends, I have looked around the forums and over online but couldn't figure out how to deal with this problem input.txt gene1,axis1/0/1,axis2/0/1 gene1,axis1/1/2,axis2/1/2 gene1,axis1/2/3,axis2/2/3 gene2,axis1/3/4,axis2/3/4 Match on first column and if first column is... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

6. Shell Programming and Scripting

Replace second match+awk/sed

I have a text file that looks like this: ----------------------------------------- sta WP00 time 10/23/2013 20:10:17 sensor trillium_240_2 0 583 add close sensor trillium_240_2 10/23/2013 20:10:17 sensor trillium_120 0 279 add close sensor trillium_120 10/23/2013 20:10:35... (11 Replies)
Discussion started by: klane
11 Replies

7. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

8. Shell Programming and Scripting

Match a Pattern & Replace The value Using AWK

I have a csv file in which i have to search a particular string and replace the data in any column with something else. How do i do it using awk. file ------ 2001,John,USA,MN,20101001,29091.50,M,Active,Y 2002,Mike,USA,NY,20090130,342.00,M,Pending,N... (3 Replies)
Discussion started by: Sheel
3 Replies

9. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

10. Shell Programming and Scripting

Four decimal places with awk

i have a script in which awk prints "($2-1700)/10000" and the answer is -0.07,but i want the answer in 4 decimal places. that is -0.0700. How can i sue awk to get my results in four decimal places (4 Replies)
Discussion started by: tomjones
4 Replies
Login or Register to Ask a Question