The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding Multiple Lines to Multiple Files dayinthelife Shell Programming and Scripting 2 06-04-2008 08:50 AM
retrieved multiple lines on multiple places in a file dala Shell Programming and Scripting 8 03-14-2008 12:28 PM
Help Needed : Split one big file to multiple files monicasgupta Shell Programming and Scripting 5 03-03-2008 04:09 PM
Split a huge line into multiple 120 characters lines with sed? jerome_1664 Shell Programming and Scripting 2 08-17-2006 09:03 AM
multiple pattern split in perl umen Shell Programming and Scripting 3 07-31-2006 11:43 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-30-2007
Registered User
 

Join Date: Mar 2007
Posts: 15
Split data into multiple lines

All,

I have a requirement where I will need to split a line into multiple lines.

Ex:

Input:

2ABCDEFGH2POIYUY2ASDGGF2QWERTY

Output:

2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY

The data is of no fixed lenght. Only the lines have to start with 2.

How can this be done.

Thanks
KP.
Reply With Quote
Forum Sponsor
  #2  
Old 07-30-2007
awk awk is offline
Registered User
 

Join Date: Feb 2007
Posts: 119
nawk -v RS="2" 'length()==0{next}{print "2" $0}' <<EOF
2ABCDEFGH2POIYUY2ASDGGF2QWERTY
EOF
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY
Reply With Quote
  #3  
Old 07-30-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
Quote:
Originally Posted by awk View Post
nawk -v RS="2" 'length()==0{next}{print "2" $0}' <<EOF
2ABCDEFGH2POIYUY2ASDGGF2QWERTY
EOF
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY
not exactly....
Code:
nawk -v RS="2" 'length()==0{next}{print "2" $0}' <<EOF
> aa2ABCDEFGH2POIYUY2ASDGGF2QWERTY
> EOF
2aa
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY
a bit better:
Code:
echo 'aa2ABCDEFGH2POIYUY2ASDGGF2QWERTY' | nawk 'gsub("2", "\n2")' | sed '/^$/d'
aa
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY
Reply With Quote
  #4  
Old 07-30-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Code:
echo "2ABCDEFGH2POIYUY2ASDGGF2QWERTY" | sed -e 's/2/\
2/g'
Reply With Quote
  #5  
Old 07-31-2007
Shell_Life's Avatar
Registered User
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Matrix,
Your 'sed' solution displays the first line as empty:
Code:
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY
Vgersh removed this line with:
Code:
sed '/^$/d'
Your solution could be modified not to generate the empty line:
Code:
echo "2ABCDEFGH2POIYUY2ASDGGF2QWERTY" | sed -e 's/\(.\)2/\1\
2/g'
Output:
Code:
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY
Reply With Quote
  #6  
Old 07-31-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Quote:
Matrix,
Your 'sed' solution displays the first line as empty:
Thats a catch !

Sorry I didn't notice that.

Thanks for correcting it !
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0