LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 04-04-2012, 05:40 AM   #1
ravi t
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Rep: Reputation: Disabled
unix logic to merge lines based on search


Hi,

I want to merge lines based on string starts with ALT.
in below example ALT is header record and RMK is child records. child record is dynamic.
input file
ALT 00623053520 20120306041920120320195927
RMK THIS IS A TEST FOR CODE RED LINE 1
RMK THIS IS A TEST FOR CODE RED LINE 2
ALT 00623053520 20120306041920
RMK STILL LOOKING
output file :-
00623053520 20120306041920120320195927THIS IS A TEST FOR CODE RED LINETHIS IS A TEST FOR CODE RED LINE

I want to merge lines till next master record comes. like in above case want to merge line 1,2,3 in one line and in second line next master and child record in one line.

Please let me know how i can do in unix..

thanks in advance..
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-04-2012, 06:23 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
What have you tried? My initial suggestion would be something like or awk or Perl, but it it could be done in bash or even with sed. So you will need to give some direction as to what you know about
and where you are stuck.
 
Old 04-04-2012, 06:52 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by ravi t View Post
I want to merge lines based on string starts with ALT.
Perhaps merge is the wrong term because merge usually means combining two files according to a specific algorithm. It seems you want to catenate records such that no data is lost but every output record starts with ALT and then remove all those leading ALTs.

Try this...
Code:
sed 's/$/~/' $InFile   \
|tr '\n' ' '           \
|sed 's/~ ALT/\nALT/g' \
|sed 's/~//g'          \
|sed 's/ALT //'        \
|sed '$s/$/\n/'
Daniel B. Martin

Last edited by danielbmartin; 04-04-2012 at 07:31 AM. Reason: Minor code improvement
 
1 members found this post helpful.
Old 04-04-2012, 08:30 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

If you can live with two extra empty lines, here is an awk one-liner:
Code:
$ awk -F'\nRMK *' -vRS='\n*ALT *' '{$1=$1} 1' infile.txt 

00623053520 20120306041920120320195927 THIS IS A TEST FOR CODE RED LINE 1 THIS IS A TEST FOR CODE RED LINE 2
00623053520 20120306041920 STILL LOOKING
Here -F sets the Field Separator, -vRS sets the Record Separator. I had to use '{$1=$1}' to force recomputing the record so that OFS (Output Field Separator), which equals to space by default, is used. Maybe there is a more clever way to do this.

To remove empty lines use sed '/^$/d'.
 
2 members found this post helpful.
Old 04-12-2012, 04:54 AM   #5
ravi t
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you guys its working fine
 
Old 04-12-2012, 08:17 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by firstfire
Maybe there is a more clever way to do this.
What you have is fine and if you remove the braces and the 1 at the end it will print the same for you as the evaluation of assigning $1 to $1 equates to true and hence a 1 which will result in a print
line.
 
2 members found this post helpful.
Old 04-12-2012, 12:32 PM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by grail View Post
What you have is fine and if you remove the braces and the 1 at the end it will print the same for you as the evaluation of assigning $1 to $1 equates to true and hence a 1 which will result in a print
line.
Indeed. Thanks.
 
  


Reply



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
Merge lines in a file using sed arobic Programming 8 01-20-2012 02:11 PM
Merge Two Lines in perl ankitjainfsl Programming 1 09-14-2011 02:57 AM
[SOLVED] A shell script to search and replace strings on certain logic rockie321 Linux - Newbie 5 06-16-2011 04:04 PM
I Need to merge 2 lines in a text file into 1 Turkeyboy Linux - Newbie 4 12-31-2008 04:37 PM
Merge lines from a file lourencojunior Linux - Newbie 5 05-12-2008 01:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:29 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration