LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 02-23-2018, 08:15 PM   #1
xiutuo
Member
 
Registered: Mar 2008
Posts: 55

Rep: Reputation: 15
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
 
Old 02-23-2018, 08:23 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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
 
Old 02-24-2018, 06:51 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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?
 
Old 02-24-2018, 07:58 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
Old 02-25-2018, 12:12 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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.
 
Old 02-25-2018, 02:02 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by ondoho View Post
maybe i didn't understand the question.
You and me both maybe.
I think I got misled by the ccomments - maybe I'll re-look at it sometime.
 
Old 02-25-2018, 03:13 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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.
 
Old 02-25-2018, 07:46 AM   #8
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
Code:
echo 111,2222,33333,4444,555,6666666,77,|sed -r 's/(.{0,8},)/\1\n/g'
add the end I option for case-insensitive alphabet
 
2 members found this post helpful.
Old 02-25-2018, 07:49 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, very nice
 
Old 02-25-2018, 11:37 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Indeed - I was way too overthinking the issue.

Suggested addition removed as it was an artifact of the way I was testing.

Last edited by syg00; 02-26-2018 at 12:05 AM. Reason: removal
 
Old 02-26-2018, 03:25 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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'
 
Old 09-08-2018, 09:30 AM   #12
xiutuo
Member
 
Registered: Mar 2008
Posts: 55

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by BudiKusasi View Post
Code:
echo 111,2222,33333,4444,555,6666666,77,|sed -r 's/(.{0,8},)/\1\n/g'
add the end I option for case-insensitive alphabet
workouts fine.thank you so much
 
Old 09-10-2018, 09:27 AM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
fmt can also join lines:
Code:
echo 111 2222 33333 4444 555 6666666 77 | fmt -9 | fmt -20
That was for demonstration. Of course you can simply split
Code:
echo 111 2222 33333 4444 555 6666666 77 | fmt -20
 
1 members found this post helpful.
  


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
[SOLVED] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 4 09-13-2018 03:41 AM
[SOLVED] How to make Array into string in AWK and split Andy_Crowd Programming 11 05-18-2016 12:06 AM
split a file based on column value awk / sed? cusvenus Linux - Newbie 5 06-15-2013 04:16 AM
how can I split a file into many files using a string in awk or sed atjurhs Linux - Newbie 15 06-11-2013 11:45 PM
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 12:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:31 PM.

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