Search a string and replace the same string above two lines?.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search a string and replace the same string above two lines?.
# 1  
Old 11-24-2012
Search a string and replace the same string above two lines?.

I need to search this "<CardinalMPI>" string and replace the same string above two lines of the string.

Code:
Nov 16, 2012 12:58:34 PM
INFO: Centinel LookUp ResponseXML  :  <CardinalMPI>
	<ReasonCode></ReasonCode>
	<ErrorDesc></ErrorDesc>
	<MerchantReferenceNumber></MerchantReferenceNumber>
	<ErrorNo>0</ErrorNo>
	<Payload>202|50515|soZF7ZQk0c33MH4TAgHg|HC</Payload>
	<EciFlag></EciFlag>
	<Enrolled>Y</Enrolled>
	<ActionCode>AUTHENTICATE</ActionCode>
	<TransactionId>soZF7ZQk0c33MH4TAgHg</TransactionId>
	<OrderNumber>5210409</OrderNumber>
	<ReasonDesc></ReasonDesc>
	<MerchantData></MerchantData>
	<OrderId>8581769081969420</OrderId>
</CardinalMPI>
Nov 16, 2012 12:58:34 PM
INFO: Centinel LookUp ResponseXML  :  <CardinalMPI>
	<ReasonCode></ReasonCode>
	<ErrorDesc></ErrorDesc>
	<MerchantReferenceNumber></MerchantReferenceNumber>
	<ErrorNo>0</ErrorNo>
	<Payload>202|50515|soZF7ZQk0c33MH4TAgHg|HC</Payload>
	<EciFlag></EciFlag>
	<Enrolled>Y</Enrolled>
	<ActionCode>AUTHENTICATE</ActionCode>
	<TransactionId>soZF7ZQk0c33MH4TAgHg</TransactionId>
	<OrderNumber>5210409</OrderNumber>
	<ReasonDesc></ReasonDesc>
	<MerchantData></MerchantData>
	<OrderId>8581769081969420</OrderId>
</CardinalMPI>
Nov 16, 2012 12:58:34 PM
INFO: Centinel LookUp ResponseXML  :  <CardinalMPI>
	<ReasonCode></ReasonCode>
	<ErrorDesc></ErrorDesc>
	<MerchantReferenceNumber></MerchantReferenceNumber>
	<ErrorNo>0</ErrorNo>
	<Payload>202|50515|soZF7ZQk0c33MH4TAgHg|HC</Payload>
	<EciFlag></EciFlag>
	<Enrolled>Y</Enrolled>
	<ActionCode>AUTHENTICATE</ActionCode>
	<TransactionId>soZF7ZQk0c33MH4TAgHg</TransactionId>
	<OrderNumber>5210409</OrderNumber>
	<ReasonDesc></ReasonDesc>
	<MerchantData></MerchantData>
	<OrderId>8581769081969420</OrderId>
</CardinalMPI>

expected output.

Code:
<CardinalMPI>
<StartDate>Nov 16, 2012 12:58:34 PM</StartDate>
<RequestType>INFO: Centinel LookUp ResponseXML  :</RequestType>  
	<ReasonCode></ReasonCode>
	<ErrorDesc></ErrorDesc>
	<MerchantReferenceNumber></MerchantReferenceNumber>
	<ErrorNo>0</ErrorNo>
	<Payload>202|50515|soZF7ZQk0c33MH4TAgHg|HC</Payload>
	<EciFlag></EciFlag>
	<Enrolled>Y</Enrolled>
	<ActionCode>AUTHENTICATE</ActionCode>
	<TransactionId>soZF7ZQk0c33MH4TAgHg</TransactionId>
	<OrderNumber>5210409</OrderNumber>
	<ReasonDesc></ReasonDesc>
	<MerchantData></MerchantData>
	<OrderId>8581769081969420</OrderId>
</CardinalMPI>
<CardinalMPI>
<StartDate>Nov 16, 2012 12:58:34 PM</StartDate>
<RequestType>INFO: Centinel LookUp ResponseXML  :</RequestType> 
	<ReasonCode></ReasonCode>
	<ErrorDesc></ErrorDesc>
	<MerchantReferenceNumber></MerchantReferenceNumber>
	<ErrorNo>0</ErrorNo>
	<Payload>202|50515|soZF7ZQk0c33MH4TAgHg|HC</Payload>
	<EciFlag></EciFlag>
	<Enrolled>Y</Enrolled>
	<ActionCode>AUTHENTICATE</ActionCode>
	<TransactionId>soZF7ZQk0c33MH4TAgHg</TransactionId>
	<OrderNumber>5210409</OrderNumber>
	<ReasonDesc></ReasonDesc>
	<MerchantData></MerchantData>
	<OrderId>8581769081969420</OrderId>
</CardinalMPI>
<CardinalMPI>
<StartDate>Nov 16, 2012 12:58:34 PM</StartDate>
<RequestType>INFO: Centinel LookUp ResponseXML  :</RequestType>  
	<ReasonCode></ReasonCode>
	<ErrorDesc></ErrorDesc>
	<MerchantReferenceNumber></MerchantReferenceNumber>
	<ErrorNo>0</ErrorNo>
	<Payload>202|50515|soZF7ZQk0c33MH4TAgHg|HC</Payload>
	<EciFlag></EciFlag>
	<Enrolled>Y</Enrolled>
	<ActionCode>AUTHENTICATE</ActionCode>
	<TransactionId>soZF7ZQk0c33MH4TAgHg</TransactionId>
	<OrderNumber>5210409</OrderNumber>
	<ReasonDesc></ReasonDesc>
	<MerchantData></MerchantData>
	<OrderId>8581769081969420</OrderId>
</CardinalMPI>

Can you please advise me?.

Last edited by laknar; 11-24-2012 at 11:35 PM.. Reason: Changed icode tags to code tags
# 2  
Old 11-24-2012
Could you specifiy the output are you looking for? What have you tried so far?
# 3  
Old 11-24-2012
I have updated the output. Im still working on the script. Any suggestion or hint will be appreciated.
# 4  
Old 11-25-2012
Other than the double trailing space laknar expects after the 1st and 3rd occurrences of </RequestType> and the single trailing space laknar expects after the 2nd occurence of </RequestType>, the following seems to produce the expected output:
Code:
awk -F: '/<CardinalMPI>/{
        printf("<CardinalMPI>\n<StartDate>%s</StartDate>\n", previous)
        printf("<RequestType>%s:%s:</RequestType>\n", $1, $2)
        previous = ""
        next}
{       if(previous != "") printf("%s\n", previous)
        previous = $0}
END {   if(previous != "") printf("%s\n", previous)}' input

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-25-2012
Thank you so much it worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

3. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

4. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

5. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

6. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

7. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

8. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

9. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

10. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies
Login or Register to Ask a Question