ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.