0

Sample colored text (this is produced from ls):

\e[0m\e[01;34m.cache\e[0m/
\e[01;34minferno\e[0m/
\e[01;34m.JUNK\e[0m/
\e[01;34m.nano\e[0m/
\e[01;34mpoo\e[0m/
\e[01;34mPrograms\e[0m/
\e[01;34mrestore\e[0m/
\e[01;34m.ssh\e[0m/
\e[34;42mWorkspace\e[0m/
a.sh
.bash_history
\e[01;32m.bash_profile\e[0m*
.bashrc
gg dd.txt

Sed command that won't work:

sed -r 's/^((\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK])*[^\.])/ \1/'

I'm attempting to do this by echoing a variable containing the text and piping it into sed, like so:

 echo "$text" | sed -r 's/^((\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK])*[^\.])/ \1/'

I just want any line that does not start with a period to have a single space prepended before it. I've spent hours on this, please help.

0

I just want any line that does not start with a period to have a single space prepended before it.

Update as per this comment:

It should ignore colors. \e[01;34m.nano\e[0m/ should not have a space in front of it

Try this sed:

sed -E '/^\.|\\e/! s/(.)/ \1/'

Example:

echo "\e[0m\e[01;34m.cache\e[0m/
\e[01;34minferno\e[0m/
\e[01;34m.JUNK\e[0m/
\e[01;34m.nano\e[0m/
\e[01;34mpoo\e[0m/
\e[01;34mPrograms\e[0m/
\e[01;34mrestore\e[0m/
\e[01;34m.ssh\e[0m/
\e[34;42mWorkspace\e[0m/
a.sh
.bash_history
\e[01;32m.bash_profile\e[0m*
.bashrc
gg dd.txt" | sed -E '/^\.|\\e/! s/(.)/ \1/'
\e[0m\e[01;34m.cache\e[0m/
\e[01;34minferno\e[0m/
\e[01;34m.JUNK\e[0m/
\e[01;34m.nano\e[0m/
\e[01;34mpoo\e[0m/
\e[01;34mPrograms\e[0m/
\e[01;34mrestore\e[0m/
\e[01;34m.ssh\e[0m/
\e[34;42mWorkspace\e[0m/
 a.sh
.bash_history
\e[01;32m.bash_profile\e[0m*
.bashrc
 gg dd.txt

Only the lines that do not start with a dot (.), OR starts with a "color" (\e) has one space character in front.

  • It should ignore colors. \e[01;34m.nano\e[0m/ should not have a space in front of it. – John Bradley Jan 10 '17 at 8:44
  • See my edit/update. – maulinglawns Jan 10 '17 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.