how to split string with awk or sed
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? |
Define "better" - the task can be simply done in awk using substr and index and loop over the input record(s).
Always multiple ways to skin a cat. |
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. |
Quote:
I think I got misled by the ccomments - maybe I'll re-look at it sometime. |
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. |
Code:
echo 111,2222,33333,4444,555,6666666,77,|sed -r 's/(.{0,8},)/\1\n/g' |
yes, very nice
|
Indeed - I was way too overthinking the issue.
Suggested addition removed as it was an artifact of the way I was testing. |
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' Code:
echo 111,2222,33333,4444,555,6666666,77,| sed 's/.\{0,8\},/&\ |
Quote:
|
The forgotten Unix tool: fmt
If the separator were space (not comma) then you could use fmt
Code:
echo 111 2222 33333 4444 555 6666666 77 | fmt -9 Code:
echo 111 2222 33333 4444 555 6666666 77 | fmt -9 | fmt -20 Code:
echo 111 2222 33333 4444 555 6666666 77 | fmt -20 |
All times are GMT -5. The time now is 02:46 AM. |