Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
there is a string format like this,split with comma:
111,2222,33333,4444,555,6666666,77,
I want to split with 9 character each line.if each line not eq 9 character and end with comma,display the all the character as one line before the last comma,the other characters after comma put it into the next line
the result will be like :
111,2222,
33333,[the second line should be "33333,444" but is not end with comma,so display all the context before the last comma,and the other after comma "444" to next line ]
4444,555,[this line should be "4444,555,",it end with comma and eq 9 character ]
6666666,[this line should be "6666666,7",same situation with line two]
so my question is how to do this with awk or sed.
thank you for your help
You've been registered almost 10 years - you should know LQ is not a free coding centre.
Make an effort, explain where you are having problems, we'll try to point you in the right direction.
With the logic required, sed would be a waste of time. awk, perl, python, ... pick a language with logic you are comfortable with and use that
i question what the point would be.
apparently the input follows a certain logic (comma-separated values), and it seems you are trying to break that logic. why?
wouldn't it be easier & better to fill an array with all the values, then work with that?
maybe i didn't understand the question.
did op mean to respect the commas as separators, but ensure that a line never gets longer than 9 characters?
that would imply that any one value would never be longer than 8 characters, i think.
looks like split at comma, but only if the text will not fit into 9 chars (including commas). But who knows?
Yes, it can easily implemented with awk, a bit hard using sed (looks like a challenge).
But first just please show us what did you try, what's happened, what is your real problem (where did you stuck) - and we will gladly help you to step forward.
The {0,8} tries to match the maximum of characters (greedyness) - exactly what is required here.
In sed there is normally no need to put the entire search expression in brackets, because it can be referenced as & in the substitution string:
Code:
echo 111,2222,33333,4444,555,6666666,77,| sed -r 's/.{0,8},/&\n/g'
A Unix sed needs a BRE, and a newline is represented by a \ followed by a newline:
Code:
echo 111,2222,33333,4444,555,6666666,77,| sed 's/.\{0,8\},/&\
/g'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.