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.

nawk -v RS="2" 'length()==0{next}{print "2" $0}' <<EOF
2ABCDEFGH2POIYUY2ASDGGF2QWERTY
EOF
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY

not exactly....

nawk -v RS="2" 'length()==0{next}{print "2" $0}' <<EOF
> aa2ABCDEFGH2POIYUY2ASDGGF2QWERTY
> EOF
2aa
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY


a bit better:

echo 'aa2ABCDEFGH2POIYUY2ASDGGF2QWERTY' | nawk 'gsub("2", "\n2")' | sed '/^$/d'
aa
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY
echo "2ABCDEFGH2POIYUY2ASDGGF2QWERTY" | sed -e 's/2/\
2/g'

Matrix,
Your 'sed' solution displays the first line as empty:


2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY

Vgersh removed this line with:

sed '/^$/d'

Your solution could be modified not to generate the empty line:

echo "2ABCDEFGH2POIYUY2ASDGGF2QWERTY" | sed -e 's/\(.\)2/\1\
2/g'

Output:

2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY

Thats a catch ! :slight_smile:

Sorry I didn't notice that.

Thanks for correcting it ! :slight_smile: