LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-20-2011, 09:29 AM   #1
Eppo
Member
 
Registered: Feb 2007
Location: NY
Distribution: Arch, Ubuntu
Posts: 77

Rep: Reputation: 27
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
 
Old 06-20-2011, 10:09 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.

Last edited by David the H.; 06-20-2011 at 10:14 AM. Reason: clarified request a bit more
 
Old 06-20-2011, 10:19 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Here is a starting point:
Code:
awk 'BEGIN{FS=OFS="|"} /^OBR/{number=$4} /^OBX/{$14=number} 1' file
until details are provided as requested.
 
Old 06-20-2011, 10:47 AM   #4
Eppo
Member
 
Registered: Feb 2007
Location: NY
Distribution: Arch, Ubuntu
Posts: 77

Original Poster
Rep: Reputation: 27
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
 
Old 06-20-2011, 07:59 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,502

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
colucix has already provided an appropriate solution to your new detail.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
Inserting Text for drivers normael Linux - Newbie 7 07-26-2008 01:41 AM
Gimp-1.3 inserting text nadine.mauch Linux - Software 0 07-26-2004 03:07 AM
Inserting Text wildcat22 Linux - General 8 04-25-2004 07:51 AM
inserting text into a file DavidPhillips Programming 5 08-15-2003 04:53 PM


All times are GMT -5. The time now is 09:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration