Results 1 to 8 of 8

Thread: Filtering color codes from echo redirect to text file.

  1. #1
    Join Date
    Oct 2011
    Location
    Sacramento, California
    Beans
    40
    Distro
    Xubuntu 14.04 Trusty Tahr

    Filtering color codes from echo redirect to text file.

    I am writing a script that shows some system specs.
    Since the specs list is longer then a screen I am running
    it through '| less -R'. A short version of the code looks
    like this:
    Code:
    ColRESET="\e[0m"
    ColGREEN="\e[1;32m"
    SpecCPUarch=`uname -p`
    SpecCPUvendor=`cat /proc/cpuinfo | grep -m 1 vendor_id`
    SpecCPUmodel=`cat /proc/cpuinfo | grep -m 1 model\ name`
    SpecCPUamount=`cat /proc/cpuinfo | grep -m 1 siblings` 
    SpecRam=`free | grep total && free -h | grep -v buffers`
    
    function sysspecs {
       echo -e ColGREEN"--- Processor/s ---------------------------------------------------------------------"ColRESET
       echo -e "$SpecCPUvendor"
       echo -e "$SpecCPUmodel "
       echo -e "architecture    : $SpecCPUarch"
       echo -e "$SpecCPUamount "; echo
       echo -e ColGREEN"--- Memory --------------------------------------------------------------------------"ColRESET
       echo -e "$SpecRam "; echo
    }
    sysspecs | less -RKeX
    Sometimes I need to save the output. In that case I am redirecting it to a text file,
    like this:

    Code:
    bash myspecs.sh > $HOME/MySpecs.txt
    In the terminal the colors are not absolutely necessary but they sure help.
    However when I redirect it to a text file I see the color codes.
    I need a clean text file, therefor I would like to know if the color codes
    can be filters or removed. I'd like to see them on the display but not in
    text file. Currently I have abandoned the color code in favor of a clean
    text file.

    Any help is much appreciated. Thank you.

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Filtering color codes from echo redirect to text file.

    maybe a simple parameter?

    Code:
    #!/bin/bash
    
    [[ $1 == "-c" ]] && colors=yes
    
    ...
    
    if [[ $colors == "yes" ]]
    then
        ColRESET="\e[0m"
        ColGREEN="\e[1;32m"
    fi
    ...
    calling with -c would yield colors but plain text otherwise.
    Code:
    ./script -c   # colors
    ./script      # plaintext
    Last edited by Vaphell; May 4th, 2014 at 02:55 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Oct 2011
    Location
    Sacramento, California
    Beans
    40
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Filtering color codes from echo redirect to text file.

    It seems remarkably smart and ease. As soon as I get around to try it I'll get back with the outcome.
    Thanks a lot for the idea.

  4. #4
    Join Date
    Oct 2011
    Location
    Sacramento, California
    Beans
    40
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Filtering color codes from echo redirect to text file.

    This code is actually part of the script I was asking about on Thread 2220629. I am actually calling up the specs with a parameter to begin with, but it's still a great idea.

    Code:
    bash Tweaks1404.sh --sysinfo
    # with no color or
    bash Tweaks1404.sh --sysinfoC
    # with colors.
    I would have considers some kind of filter to be the ideal scenario but I'm still glad. Thanks. Maybe when I'm done with it I can post it somehow for people to see and try if anyone wishes, and maybe give me some feedback....

  5. #5
    Join Date
    Oct 2011
    Location
    Sacramento, California
    Beans
    40
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Filtering color codes from echo redirect to text file.

    I just thought about using your solution as a condition towards $2 being the '>' or a pattern including '>' and the delimiter '/', when entering something like: "> $HOME/MySpecs.txt". That way I can keep using only one parameter for the same function. Thanks again.

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Filtering color codes from echo redirect to text file.

    You can use the test command to check if stdout is a terminal or not:

    Code:
    if [ -t 1 ]
    then
        ColRESET="\e[0m"
        ColGREEN="\e[1;32m"
    fi

  7. #7
    Join Date
    Oct 2011
    Location
    Sacramento, California
    Beans
    40
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Filtering color codes from echo redirect to text file.

    That sounds great, thanks. As soon as I have a moment I'll run a test and try to understand it better, because the test command is new to me.

  8. #8
    Join Date
    Oct 2011
    Location
    Sacramento, California
    Beans
    40
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Filtering color codes from echo redirect to text file.

    Just wanted to say thanks. It worked very nicely. I consider this matter solved and closed. Thanks again.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •