Replacing some part of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing some part of file
# 1  
Old 01-17-2010
Replacing some part of file

Hello,
I have two files, consider that as file1 and file2. Here file1 is the master file.
file1 will contain data like
Code:
GS*RA*071000013*102562451P*091130*0520*334052023*X*003050
ST*820*334052023
BPR*C*509.77*C*ACH*CTX*01*071000013*DA*5529085*9000002008**01*071000013*DA*5529085*091130
NTE*ZZZ*QWEST80217E
REF*PY*A97983
REF*TN*081000602100305*TRACE NUMBER FROM THE ACH PAYMENT
DTM*020*091127
N1*PR*CIS INFO. VEND.*ZZ*PAYER FROM ACH
ENT*1*AG*94*CASS
N1*BT*AT&T MOBILITY
RMR*IV*206Z130004706B**214.93
NTE*OTH*ACCT#J-520-294-1749 877M
DTM*003*091101
RMR*IV*503Z130027168B**294.84
NTE*OTH*ACCT#J-644-294-1659 877M
DTM*003*091018
RMR*IV*456Z130027123C**294.84
SE*000014*334052023
GE*1*334052023

file2 will contain data like
Code:
RMR*IV*206Z130004706B**200.93
NTE*OTH*ACCT#J-520-294-1749 877M
DTM*003*091101

Here in file2 a account number will be found in RMR line as highlighted in red. The same account number will be found in file1 too. And there is a change in the amt as that is also highlighted in red above.I have to write a script or awk so that the file2 content is replace in file1, so the file1 after replacing should look like
Code:
GS*RA*071000013*102562451P*091130*0520*334052023*X*003050
ST*820*334052023
BPR*C*509.77*C*ACH*CTX*01*071000013*DA*5529085*9000002008**01*071000013*DA*5529085*091130
NTE*ZZZ*QWEST80217E
REF*PY*A97983
REF*TN*081000602100305*TRACE NUMBER FROM THE ACH PAYMENT
DTM*020*091127
N1*PR*CIS INFO. VEND.*ZZ*PAYER FROM ACH
ENT*1*AG*94*CASS
N1*BT*AT&T MOBILITY
RMR*IV*206Z130004706B**200.93
NTE*OTH*ACCT#J-520-294-1749 877M
DTM*003*091101
RMR*IV*503Z130027168B**294.84
NTE*OTH*ACCT#J-644-294-1659 877M
DTM*003*091018
RMR*IV*456Z130027123C**294.84
SE*000014*334052023
GE*1*334052023

the difference between the original file1 and the required output is the also highlighed in red.

So can you help me in replacing the lines in file2 to file1 by finding the correct account number from RMR record.

Thanks.

Last edited by Scott; 01-17-2010 at 07:15 AM.. Reason: Please use code tags
# 2  
Old 01-17-2010
Use this:-

Code:
nawk 'NR==FNR&&/RMR/{getline b ; getline e ; a[$3]=$0 ; next }
/RMR/&&($3 in a){$0=a[$3] ; print ; c=3 ; next } c-->0{if (c==2) {$0=b} ; if(c==1) {$0=e} }
1'  FS="*" File2.txt  FS="*"  File1.txt

SmilieSmilieSmilie

---------- Post updated at 15:03 ---------- Previous update was at 14:51 ----------

or if File2 have many "RMR" not just as you post you can use the below:-


Code:
nawk 'NR==FNR&&/RMR/{getline b ; getline e ; a[$3]=$0"\n"b"\n"e ; next }
/RMR/&&($3 in a){$0=a[$3] ; print $0 ; c=3 ; next } c-->0{if (c==2) {$0=""} ; if(c==1) {$0=""} ; next }
1'  FS="*" File2.txt  FS="*" File1.txt

SmilieSmilieSmilie
# 3  
Old 01-17-2010
Another approach:
Code:
awk -F"*" 'NR==FNR {if($1=="RMR") {a[$3]=$5}; next}
a[$3] && $1=="RMR" {$5=a[$3]}
1' OFS="*" file2 file1

# 4  
Old 01-18-2010
Hi Ahmed,

nawk is not working here. I am using HP UNIX. Can you please suggest any other ways to do.

Thanks.

---------- Post updated at 04:12 AM ---------- Previous update was at 04:08 AM ----------

Hi Franklin,

This approach works !

But this replaces only the RMR line. But I want to replace the whole content of file2 to file1, I mean not only the RMR line from file2 but the whole content of file2 to file1 which matches the account number highlighted in red in previous post (206Z130004706B).

Thanks.
# 5  
Old 01-18-2010
Quote:
Originally Posted by atlantis
Hi Franklin,

This approach works !

But this replaces only the RMR line. But I want to replace the whole content of file2 to file1, I mean not only the RMR line from file2 but the whole content of file2 to file1 which matches the account number highlighted in red in previous post (206Z130004706B).

Thanks.
Redirect the output to a temporary file and overwrite file2 with it, something like:

Code:
awk -F"*" 'NR==FNR {if($1=="RMR") {a[$3]=$5}; next}
a[$3] && $1=="RMR" {$5=a[$3]}
1' OFS="*" file2 file1 > temp.file

mv temp.file file2

# 6  
Old 01-18-2010
Quote:
Originally Posted by atlantis
Hi Ahmed,

nawk is not working here. I am using HP UNIX. Can you please suggest any other ways to do.

Thanks.

---------- Post updated at 04:12 AM ---------- Previous update was at 04:08 AM ----------

you can use awk or gawk or /usr/xpg4/bin/awk instead of nawk.

try it my code satisfy your demands. SmilieSmilieSmilie

SmilieSmilieSmilie
# 7  
Old 01-18-2010
Hi Franklin,

I think I did not explain it clearly in my previous post. Now for example consider the file2 content as

RMR*CO*206Z130004706B**199.93
NTE*OTH*ACCT#J-445-294-1748 366N
DTM*003*091201


and the file1 content as

GS*RA*071000013*102562451P*091130*0520*334052023*X*003050
ST*820*334052023
BPR*C*509.77*C*ACH*CTX*01*071000013*DA*5529085*9000002008**01*071000013*DA*5529085*091130
NTE*ZZZ*QWEST80217E
REF*PY*A97983
REF*TN*081000602100305*TRACE NUMBER FROM THE ACH PAYMENT
DTM*020*091127
N1*PR*CIS INFO. VEND.*ZZ*PAYER FROM ACH
ENT*1*AG*94*CASS
N1*BT*AT&T MOBILITY
RMR*IV*206Z130004706B**214.93
NTE*OTH*ACCT#J-520-294-1749 877M
DTM*003*091101
RMR*IV*503Z130027168B**294.84
NTE*OTH*ACCT#J-644-294-1659 877M
DTM*003*091018
RMR*IV*456Z130027123C**294.84
SE*000014*334052023
GE*1*334052023

Here some content are highlighted in red. We can find the same lines in file2. I want the highlighted 3 lines in file1 to be replaced with content from file2 like

GS*RA*071000013*102562451P*091130*0520*334052023*X*003050
ST*820*334052023
BPR*C*509.77*C*ACH*CTX*01*071000013*DA*5529085*9000002008**01*071000013*DA*5529085*091130
NTE*ZZZ*QWEST80217E
REF*PY*A97983
REF*TN*081000602100305*TRACE NUMBER FROM THE ACH PAYMENT
DTM*020*091127
N1*PR*CIS INFO. VEND.*ZZ*PAYER FROM ACH
ENT*1*AG*94*CASS
N1*BT*AT&T MOBILITY
RMR*CO*206Z130004706B**199.93
NTE*OTH*ACCT#J-445-294-1748 366N
DTM*003*091201

RMR*IV*503Z130027168B**294.84
NTE*OTH*ACCT#J-644-294-1659 877M
DTM*003*091018
RMR*IV*456Z130027123C**294.84
SE*000014*334052023
GE*1*334052023

Sorry for the confusion from my side. Please advice.

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing part of a delimited string

Hello, I have some tab delimited text data that I am processing. The second column looks like, NAME;pyrimidine-2,4-diol;cpd;2;line;37 I need to clean this up to just the name, pyrimidine-2,4-diol All lines have the same format, NAME;text;cpd;int;line;int followed by tab I have tried... (6 Replies)
Discussion started by: LMHmedchem
6 Replies

2. UNIX for Dummies Questions & Answers

Replacing part of filename

Hi guys! I have quite a lot of files like all_10001_ct1212307460308.alf* and I want to get rid of the first number for all at once like: all_ct1212307460308.alf* How can I do this in the shell? (12 Replies)
Discussion started by: TimmyTiz
12 Replies

3. Shell Programming and Scripting

Renaming Filenames by replacing a part

Hi, I have little experience on Shell scripts, I searched the forum but couldn't make out what I want. I want to rename a set of files to a new file name a_b_20100101 c_d_20100101 ....................... ...................... I want to rename the files to a_b_20140101... (5 Replies)
Discussion started by: JaisonJ
5 Replies

4. Shell Programming and Scripting

Replacing part of the sentence using echo and sed

Hi, Iam using ksh and trying to execute the following syntax to replace one word of the sentence with a new word. But somehow sed is not able to replace the old value with new value. Please let me know where Iam going wrong. Sample Code : --> export line="VORTEX,abcdef" export... (3 Replies)
Discussion started by: ajithab
3 Replies

5. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

6. Shell Programming and Scripting

Replacing part of XML code inside comment tags

Hello! I'd like to modify custom values in a XML config file between comment tags using bash script. <feature> <keyboardshortcut>C-m</keyboardshortcut> <option1>disabled</option2> <option2>enabled</option2> </feature> <!-- bash script features START --> <feature> ... (2 Replies)
Discussion started by: prism1
2 Replies

7. Shell Programming and Scripting

Replacing part of a pattern in sed

Hi I have a piece of xml that has a pattern like this <int>159</int><int>30</int> I want to find this pattern but only substitute the second part of the pattern to {rid1}. Is that possible in sed ? Thanks. ---------- Post updated at 12:10 PM ---------- Previous update was at 12:01 PM... (11 Replies)
Discussion started by: vnn
11 Replies

8. UNIX for Dummies Questions & Answers

Replacing part of a text file with user input.

Ok, I am brand new to UNIX and I am trying to learn a cross between basic script and database use. I had got some ideas off the net on simple ideas for learning UNIX. I am working on creating a simple phone book program that allows myself to enter our employees from work into a phone book text... (0 Replies)
Discussion started by: georgefurbee
0 Replies

9. Shell Programming and Scripting

Replacing the part of file name?

Hi All One of my script generate following files. These files has static TIMESTAMP 20080227. AccAdd_20080227_1000.dat AccBal_20080227_1000.dat Acc_20080227_1000.dat AccGrpMem_20080227_1000.dat AccToCust_20080227_1000.dat What i need to do is, once the file has been generated, it... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies

10. Shell Programming and Scripting

Need help regarding replacing a part of string

Hi all suppose i have a string "abacus sabre", i need to replace occurences 'ab' with 'cd' and i need to store this result into same string and i need to return this result from script to the calling function, where as the string is passed from calling function. i tried like this ... (1 Reply)
Discussion started by: veerapureddy
1 Replies
Login or Register to Ask a Question