Results 1 to 4 of 4

Thread: Drop special character from piped value

  1. #1
    Join Date
    May 2008
    Location
    Ottawa ON, Canada
    Beans
    132
    Distro
    Ubuntu 16.04 Xenial Xerus

    Drop special character from piped value

    I'm trying to drop the character ^M from my piped command, however, I can't seem to get it to drop.

    In one terminal window, I am running
    Code:
    echo -en "adc read 0\r"  > /dev/ttyACM0
    which makes my device send back some results with numbers to the same port. To read the returned value, I am in a separate window I am running
    Code:
    cat -v < /dev/ttyACM0 | grep -v ">"
    function that is already cleaning up some of the results, but still allowing ^M to come back. I have tried dropping all but numbers by expanding my cat to
    Code:
    cat -v < /dev/ttyACM0 | grep -v ">" | tr -dc '[:alnum:]'
    , but no luck.

    I'm open to ideas on how to drop the two characters that I don't want to see.
    Last edited by Superdude_123; September 6th, 2017 at 11:53 PM. Reason: Add tags

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Drop special character from piped value

    Simply
    Code:
    cat -v < /dev/ttyACM0 | grep -v ">" | tr -d '\r'
    will drop the carriage returns. Which other character are you wanting to drop?

  3. #3
    Join Date
    May 2008
    Location
    Ottawa ON, Canada
    Beans
    132
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Drop special character from piped value

    That just got me some blank results.

    If I wanted to filter the character ^ and the letter M out, how could that be done?

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Drop special character from piped value

    If you don't want carriage returns to be displayed as ^M, don't add the -v to the cat

    Code:
    cat < /dev/ttyACM0 | grep -v ">" | tr -d '\r'
    If you want to keep the -v for some reason, then you will need to treat ^M as literal characters e.g.

    Code:
    cat -v < /dev/ttyACM0 | grep -v ">" | sed 's/\^M$//'

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
  •