So i have this text:

2019/06/16 22:36:28 (UNIVERSE) * @{EE3F}Trytrytry sagt: @{F80F}going afk for a bit

I want to color the whole line in red for example. Then remove the unecessary parts to:

22:36:28 Trytrytry sagt: going afk for a bit

The removing of the parts is works already, but when i add:

norm="$(printf '\033[0m')"
boldred="$(printf '\033[0;1;31m')" 
sed -ue s/UNIVERSE/${boldred}&${norm}/g

It only colorize UNIVERSE and show it up, but i don't want let it show. I want to color the whole line where UNIVERSE was. So just adding color. So lets say:

IF (UNIVERSE) THEN color the line red in terminal, but let it be clean and don't show up the removed parts.

Any ideas?

closed as unclear what you're asking by Scott, msp9011, X Tian, Dmitry Grigoryev, muru Jun 27 at 8:25

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • The removing of the parts is works already please show what you mean by unnecessary parts and how you remove them. – simlev Jun 17 at 10:27

Change your sed line, for this one:

sed -ue 's/\(.*UNIVERSE.*\)/'${boldred}'\1'${norm}'/g'

with .*UNIVERSE.*, you are matching any whole line that contains UNIVERSE on it; then, you add the parentheses: \(.*UNIVERSE.*\) to capture the match (the whole line in this case), and then, you backreference your previous match with \1.

  • Thank you! Worked very good. – Megavolt Jun 17 at 12:22

Well, you should:

  1. match lines containing your search string /UNIVERSE/
  2. remove the unnecessary parts
  3. replace beginning (^) with ${boldred}
  4. replace end ($) with ${norm}

Here's a tentative snippet that should show you the way, based on the posted specifications:

norm="$(printf '\033[0m')"; boldred="$(printf '\033[0;1;31m')"; sed -ure "/UNIVERSE/{s/^[^ ]* //;s/\(UNIVERSE[^}]+}//;s/sagt:[^}]+}/sagt: /;s/^/${boldred}/;s/$/${norm}/}" input1

Or, split for readability:

norm="$(printf '\033[0m')";
boldred="$(printf '\033[0;1;31m')";
sed -ure "/UNIVERSE/ {
    s/^[^ ]* //;
    s/sagt:[^}]+}/sagt: /;

I'm also providing a full, self-contained Perl solution:

perl -pe 'BEGIN{use Term::ANSIColor qw(:constants)} if(/UNIVERSE/){s/^\S*\s//;s/\(UNIVERSE[^}]+}//;s/sagt:[^}]+}/sagt: /;$_=RED.$_.RESET}'

Or, split for readability:

perl -pe 'BEGIN {use Term::ANSIColor qw(:constants)}
    if (/UNIVERSE/) {
        s/sagt:[^}]+}/sagt: /;
        $_ = RED . $_ . RESET

The sed line in this snippet matches against UNIVERSE and then applies the red+bold attributes to the entire line. I've used tput to obtain the attribute values because it adapts depending on the terminal type (including where one isn't set, such as in a cron job):

# See "man 5 terminfo" for other portable settings
red=$(tput setaf 1 2>/dev/null)
bold=$(tput bold 2>/dev/null)
norm=$(tput sgr0 2>/dev/null)

# Perform the substitution
sed -e "/UNIVERSE/s/^.*$/${red}${bold}&${norm}/"

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