Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing text in a file

Status
Not open for further replies.

lhg1

IS-IT--Management
Mar 29, 2005
134
DK
Hi

I have a rather complex replacement that I need to make in a very large file.

The file is buield up like this.
Code:
200001	12345432078934
REC0200001 37,47
[b]200011	1234543207893
REC0200011 30,47
[/b]200061	12345432004047
REC0200061 40,47


This eksampel contains 3 elements, I've marked the middelone.

I need to take the amount in the line starting with REC02 and put into the top line.

This is an exampel where the markede element is correct.
Code:
200011	123454320[b]3047[/b]
REC0200011 [b]30,47[/b]

The number 200011 is the identifier and is always unique.

I can make a file from the identifier and the right figure, but I don't know the figure that I have to replace.
Code:
cat file|grep REC02|awk -F "REC02" '{print $2}'|awk '{print $1 " " $2}'|sed s/,//

The result fra this is 130000 lines.

Does anyone have any ideers?

/LHG

 
Hi

Are there other lines beside the presented ones ?

Are the line pairs always in that order ?

Are the line pairs always consecutive ?

Where should be the value from the REC line put ?

What I understand so far :
Code:
awk '!/^REC/{s=$0;next}{n=substr($0,(i=length())-4,2)substr($0,i-1,2);if(substr(s,length(s)-3)!=n)s=s n;print s;print}' /input/file

Feherke.
 
something like this ?
Code:
awk '/^2/{x=substr($0,1,length($0)-4)}/^REC02/{x=x$2;sub(/,/,"",x);print x;print}' file

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top