Skip to main content
edited tags
Link
Gilles 'SO- stop being evil'
  • 866.2k
  • 205
  • 1.8k
  • 2.3k
added 60 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

I am trying to figure out how to break a string to be on character per line.
I.e ahebhaaa to be:
a
h
e
b
h
a
a
a

a h e b h a a a 

I tried:

$ echo ahebhaaa | sed 's/\(.\)\(.\)/\1\n\2/g'
I.e. my intention was "plugin" between chars a new line using capture groups but I get:
a
he
bh
aa
a

a he bh aa a 

I guess this has to do with greedy/non-greedy but adding ?* anywhere in this does not do anything. What am I doing wrong here?

Additionally I found that this:
echo ahebhaaa | sed 's/[^\n]/&\n/g'
Does the job. But I don't understand how it works. What is &? How does the [^\n] work?

I am trying to figure out how to break a string to be on character per line.
I.e ahebhaaa to be:
a
h
e
b
h
a
a
a

I tried:

$ echo ahebhaaa | sed 's/\(.\)\(.\)/\1\n\2/g'
I.e. my intention was "plugin" between chars a new line using capture groups but I get:
a
he
bh
aa
a

I guess this has to do with greedy/non-greedy but adding ?* anywhere in this does not do anything. What am I doing wrong here?

Additionally I found that this:
echo ahebhaaa | sed 's/[^\n]/&\n/g'
Does the job. But I don't understand how it works. What is &? How does the [^\n] work?

I am trying to figure out how to break a string to be on character per line.
I.e ahebhaaa to be:

a h e b h a a a 

I tried:

$ echo ahebhaaa | sed 's/\(.\)\(.\)/\1\n\2/g'
I.e. my intention was "plugin" between chars a new line using capture groups but I get:

a he bh aa a 

I guess this has to do with greedy/non-greedy but adding ?* anywhere in this does not do anything. What am I doing wrong here?

Additionally I found that this:
echo ahebhaaa | sed 's/[^\n]/&\n/g'
Does the job. But I don't understand how it works. What is &? How does the [^\n] work?

Source Link
Jim
  • 10.7k
  • 17
  • 62
  • 88

Stuck in separating chars into strings in my approach. Found a working approach but can not understand it

I am trying to figure out how to break a string to be on character per line.
I.e ahebhaaa to be:
a
h
e
b
h
a
a
a

I tried:

$ echo ahebhaaa | sed 's/\(.\)\(.\)/\1\n\2/g'
I.e. my intention was "plugin" between chars a new line using capture groups but I get:
a
he
bh
aa
a

I guess this has to do with greedy/non-greedy but adding ?* anywhere in this does not do anything. What am I doing wrong here?

Additionally I found that this:
echo ahebhaaa | sed 's/[^\n]/&\n/g'
Does the job. But I don't understand how it works. What is &? How does the [^\n] work?