LinuxQuestions.org
Visit Jeremy's Blog.
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, 09: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, 09:23 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,487

Rep: Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099
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, 07:51 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,413
Blog Entries: 9

Rep: Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657
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, 08:58 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,487

Rep: Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099
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, 01:12 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,413
Blog Entries: 9

Rep: Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657Reputation: 3657
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, 03:02 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,487

Rep: Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099
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, 04:13 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,591

Rep: Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353
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, 08:46 AM   #8
BudiKusasi
Member
 
Registered: Apr 2017
Posts: 54

Rep: Reputation: Disabled
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, 08:49 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,591

Rep: Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353Reputation: 4353
yes, very nice
 
Old 02-26-2018, 12:37 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,487

Rep: Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099
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 01:05 AM. Reason: removal
 
Old 02-26-2018, 04:25 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,290

Rep: Reputation: 592Reputation: 592Reputation: 592Reputation: 592Reputation: 592Reputation: 592
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, 10: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, 10:27 AM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,290

Rep: Reputation: 592Reputation: 592Reputation: 592Reputation: 592Reputation: 592Reputation: 592
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

All times are GMT -5. The time now is 06:37 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration