LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   inserting text from one line into others below (http://www.linuxquestions.org/questions/programming-9/inserting-text-from-one-line-into-others-below-887337/)

Eppo 06-20-2011 10:29 AM

inserting text from one line into others below
 
i have a text file with fields delimited by |, the format is called HL7. what i'm trying to do is see if i can have the data from one field on one line and insert it into a blank field on lines below.
here is an example:
OBR|1||NUMBER|874^^L|||201106101200|201106161535||||||||16261^,PLLC||||||201106170601|||F

OBX|1|CE|22327-1^blah^LN||^^^^blah^L||||||F||WANT NUMBER HERE TOO|201106161535
The lines always start with OBR, and the line below Starts with OBX. there could also be mulitple lines that start with OBX below the OBR also, so i would need to account for that.
my guess would be that it can be done with awk, i'm going to research how it can be done, but i was hoping someone here could chime in to how i could do this.
thanks
Joe

David the H. 06-20-2011 11:09 AM

I think you need to define your problem in a little more detail. Exactly what fields do you want to alter, and how can we match them? Also show us a longer sample of the text, including these "other OBX lines" that we have to "account for". We need to know exactly how they relate to the first lines, so we can...what...avoid them? Or insert the text into them too? Your post doesn't make that clear.

And please use [code][/code] tags around your code, to preserve formatting and to improve readability.

This includes the contents of the files you want to post, as they obviously can have very long lines.

Awk can almost certainly do it, but the devil is in the details.

colucix 06-20-2011 11:19 AM

Here is a starting point:
Code:

awk 'BEGIN{FS=OFS="|"} /^OBR/{number=$4} /^OBX/{$14=number} 1' file
until details are provided as requested.

Eppo 06-20-2011 11:47 AM

in my above example:
Code:

OBR|1||NUMBER|874^^L|||201106101200|201106161535||||||||16261^,PLLC||||||201106170601|||F

OBX|1|CE|22327-1^blah^LN||^^^^blah^L||||||F||WANT NUMBER HERE TOO|201106161535

what i need to do is just take the contents of field 4 of the lines that start with OBR in insert that value into the 14th field of the lines that start with OBX below it. in addition to the code below, its possible to have text that looks like this too:
Code:

OBR|1||NUMBER|874^^L|||201106101200|201106161535||||||||16261^,PLLC||||||201106170601|||F

OBX|1|CE|22327-1^blah^LN||^^^^blah^L||||||F||WANT NUMBER HERE TOO|201106161535
OBX|1|CE|22327-1^blah^LN||^^^^blah^L||||||F||WANT NUMBER HERE TOO|201106161535
OBX|1|CE|22327-1^blah^LN||^^^^blah^L||||||F||WANT NUMBER HERE TOO|201106161535
OBX|1|CE|22327-1^blah^LN||^^^^blah^L||||||F||WANT NUMBER HERE TOO|201106161535

OBR|1||NUMBER|874^^L|||201106101200|201106161535||||||||16261^,PLLC||||||201106170601|||F

OBX|1|CE|22327-1^blah^LN||^^^^blah^L||||||F||WANT NUMBER HERE TOO|201106161535
OBX|1|CE|22327-1^blah^LN||^^^^blah^L||||||F||WANT NUMBER HERE TOO|201106161535

OBR|1||NUMBER|874^^L|||201106101200|201106161535||||||||16261^,PLLC||||||201106170601|||F

OBX|1|CE|22327-1^blah^LN||^^^^blah^L||||||F||WANT NUMBER HERE TOO|201106161535
OBX|1|CE|22327-1^blah^LN||^^^^blah^L||||||F||WANT NUMBER HERE TOO|201106161535
OBX|1|CE|22327-1^blah^LN||^^^^blah^L||||||F||WANT NUMBER HERE TOO|201106161535

i can also have multiple records in the same file as above, with different number of OBX lines below each OBR.
thank you very much for your help.
Joe

grail 06-20-2011 08:59 PM

colucix has already provided an appropriate solution to your new detail.


All times are GMT -5. The time now is 05:24 AM.