Find the answer to your Linux question:
Results 1 to 8 of 8
I have a file that I need to remove some characters. They are hidden non-printable characters. If I hexedit the file I see A0 0D 0A the octal value is ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    187

    Help with tr


    I have a file that I need to remove some characters. They are hidden non-printable characters. If I hexedit the file I see
    A0 0D 0A the octal value is 240 015 012. The problem is the 0A. I can remove all the characters but this removes all the 0A's in the file. I only want to remove the 0A if it is after A0 0D. I want to keep all other 0A's that do not show up in this order.

    I have tried

    Code:
    tr -d '\240\015\012' <file> file.out
    This removes the A0 0D 0A but it also removes all 0A's in the file.

    Any help?

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hmmm...I couldn't figure that out with tr either. How about this perl bit? It uses the Hex equivalent of the characters:

    Code:
    #!/usr/bin/perl
    while(<STDIN>){
      chomp;
      my $string = $_;
      $string =~ s/\xa0\x0d\x0a//;
      print "LINE: '$string'\n";
    }
    call it "replace.pl" and call it like this:
    Code:
    cat yourfile.txt|./replace.pl

  3. #3
    Just Joined!
    Join Date
    Sep 2008
    Posts
    23
    tr works on single characters. It will not match a pattern. So in your case, trying to match \240\015\012 really is saying to tr, if you find any of these individuals, delete them.

    I haven't tested, but sed might work for you. Wasn't sure if you wanted to remove the entire pattern when found, or keep the first two and only remove the trailing \012.

    This should find and remove the entire \240\015\012 pattern when found
    Code:
    sed 's/\240\015\012//g' <file_in >file_out
    This should find the \240\015\012 pattern, then replace with \240\015 (removes trailing \012)
    Code:
    sed 's/\240\015\012/\240\015/g' <file_in >file_out
    Either way, your other \012's within the file shouldn't be affected. Let me know how it worked out. I've only used sed to replace or remove words/phrases, not hidden hex.


    James

  4. #4
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    187
    Both of these options did not work. It should be able to be done. I still am trying a few other options but it's not looking good.

  5. #5
    Just Joined!
    Join Date
    Nov 2010
    Posts
    3
    According to sed man page:
    On Unix platforms, the 'echo' command may allow insertion of octal or hex values, e.g., `echo "\0nnn"` or `echo -n "\0nnn"`.
    Try J-Dude answer with `echo "\0nnn"` instead of \0nnn

  6. #6
    Just Joined!
    Join Date
    Mar 2008
    Posts
    20
    Have you tried
    Code:
    sed -r 's/\xA0\x0D\x0A//g'

  7. #7
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    187
    Quote Originally Posted by chebarbudo View Post
    Have you tried
    Code:
    sed -r 's/\xA0\x0D\x0A//g'

    sed: Not a recognized flag: r

  8. #8
    Just Joined!
    Join Date
    Mar 2008
    Posts
    20
    What's your version of sed?
    Code:
    sed --version
    Maybe try to use
    Code:
    sed --regexp-extended
    instead of
    Code:
    sed -r
    .

Posting Permissions

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