remove characters that are different from ABCD in a file

Discussion in 'Command Line' started by papori, Jan 3, 2013.

  1. papori

    papori New Member

    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hi guys!
    I have a text file, and I want to delete all the characters that are not A,B,C and D.
    For example if my file contain this 2 lines:
    ABCDEFGHIJ
    GHKLABC

    the new file will look like:
    ABCD
    ABC

    I am looking for an easy way.. ideas?


    Thanks,
    Pap
  2. cfajohnson

    cfajohnson New Member

    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1

    If you remove all characters other than A, B, C, and D, that will not
    be the result; it will also remove the newline:
    Code:
    tr -dc 'ABCD' < "$file"
    
    The result will be:
    ABCDABC

    What you want is:

    Code:
    tr -dc 'ABCD\n' < "$file"
    

Share This Page