[SOLVED] How to merge lines based on number of columns
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
From your example data, here is a sed example to get you started
Code:
sed ':a;N;s/\n|/|/;ba' file
Code:
:a => create a label named a
N => append the next input line
s/\n|/|/ => replace a newline followed by a '|' with a '|', ie delete the newline before
ba => branch to a, ie goto label a
Thanks. Will it work for any number of columns. Meaning this file should have 4 columns. In the 2nd row I got newline character. Hence needed merge.
I will test and come back to you
This is a file with equal number of columns basically (It is 4 here which is configurable based on file) Could you please help me?
Keefaz *HAS* helped you a lot...but you also need to read the "Question Guidelines" link in my posting signature. At some point, *YOU* are going to have to actually do/try something of your own. keefaz has given you a HUGE head-start, but now is the time for you to apply what you were told, and accomplish your own work...or at least TRY to.
Come back and post what you've done/tried/written on your own, and what results you get.
Sure TB0ne. I appreciate Keefaz has really helped me. Let me proceed further on this. Thanks for your reply TB0ne :-)
Sorry I am a newbie in the forum just joined yesterday
awk has command line options - one of which will allow you to set a variable. Have you tested that code on your own data as specified in post #6 ?. Specifically where input data spans more than 2 lines.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.