Replacing hex characters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing hex characters
# 1  
Old 05-14-2011
Replacing hex characters

I have the following file consisting of dates and sample measurements:
Code:
05��Oct��2010	1.31��
06��Oct��2010	1.32��
07��Oct��2010	1.31��

The hex characters are \xc2\xa0 in sequence.
I have tried to remove the characters as follows:
Code:
sed -i '' -e 's/\xc2\xa0//g' file.dat

and as follows (replace characters with a space):
Code:
tr '\194' '\032' < file.dat > file_.dat
tr '\160' '\032' < file_.dat > file__.dat

However, neither method worked. The first probably couldn't find the characters and left the file untouched and the second replaced some of the hex characters with other hex characters.

Can someone point out the mistakes or indicate an alternative method?
# 2  
Old 05-15-2011
Does this do what you need
Code:
tr -dc '[:print:]' < infile > outfile

# 3  
Old 05-15-2011
Code:
sed 's/[^[:print:]][^[:print:]]*/ /g' infile

---------- Post updated at 02:05 PM ---------- Previous update was at 02:04 PM ----------

Code:
$ cat tst
05��Oct��2010   1.31��
06��Oct��2010   1.32��
07��Oct��2010   1.31��
$ sed 's/[^[:print:]][^[:print:]]*/ /g' tst
05 Oct 2010 1.31
06 Oct 2010 1.32
07 Oct 2010 1.31
$

---------- Post updated at 02:15 PM ---------- Previous update was at 02:05 PM ----------

Tested on FreeBSD 8.2 :

Code:
$ tr -sc '[:rune:]' ' ' <tst
05 Oct 2010     1.31
06 Oct 2010     1.32
07 Oct 2010     1.31

# 4  
Old 05-15-2011
Code:
tr -dc '[:print:]' < infile > outfile

Thank you for the suggestion. The method also leaves out the carriage returns, so that the result appears on one line as opposed to rows.

---------- Post updated at 15:35 ---------- Previous update was at 15:22 ----------

Code:
tr -sc '[:rune:]' ' ' < file.dat > file_.dat
sed -i '' -e 's/[^[:print:]][^[:print:]]*/ /g' file.dat

Both of these commands seem to work. Thank you also for your suggestion.
# 5  
Old 05-15-2011
can you try this. I have tried to include all chars from \x00 to \x7E avoiding the chars you have mentioned.

Code:
tr -cd '[\x00-\x7E]' <infile >outfile[

# 6  
Old 05-15-2011
Thank you for your suggestion. Also here carriage returns are removed as well as spaces, so the result is that all non-space characters appear on one line.
# 7  
Old 05-16-2011
Quote:
tr '\194' '\032' < file.dat > file_.dat
tr '\160' '\032' < file_.dat > file__.dat
These were nearly right but you have used decimal numbers when they should be in Octal.
C2 = Octal 302
A0 = Octal 240
Space = Octal 040
This User Gave Thanks to methyl For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Replacing Hex Characters In A File Using awk?

Hi guys, First off, i'm a complete noob to UNIX and LINUX so apologies if I don't understand the basics! I have a file which contains a hex value of '0D' at the end of each line when I look at it in a hex viewer. I need to change it so it contains a hex value of '0D0A0A' I thought... (10 Replies)
Discussion started by: AndyBSG
10 Replies

2. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

3. Shell Programming and Scripting

Grepping for hex characters - explanation?

Hello, Yesterday I was looking for a way to grep for a tab in the shell, and found this solution in several places: grep $'a' # Grep for the letter 'a' between two tabs I'm fine with most of this, but I don't understand what the $ (dollar sign) before the first quote does. It doesn't work... (7 Replies)
Discussion started by: mregine
7 Replies

4. Shell Programming and Scripting

Convert hex values to displayable characters

Hi, I am a bit stuck with displaying characters. I am having values like below in the proper displayable characters. which I would want to print the actual value on the right hand side. I dont want to create an array because I would have to create 255 different values. isnt there another way of... (17 Replies)
Discussion started by: ahmedwaseem2000
17 Replies

5. Shell Programming and Scripting

Replacing hex characters '\x0D' with '\x0D\x0A'

Hello All, I have a requirement where I need to replaced the hex character - '\x0D' with 2 hex characters - 'x0D' & 'x0A' I am trying to use SED - But somehow its not working. Any pointers? Also the hex character '\x0D' can occur anywhere in the line. Can this also be accomplished... (6 Replies)
Discussion started by: paragkalra
6 Replies

6. Shell Programming and Scripting

Replacing Characters with |

Hi All, example data.log 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 I want like this to 526569346|66815531961|09 526569346|66815531961|09... (4 Replies)
Discussion started by: ooilinlove
4 Replies

7. Shell Programming and Scripting

Replacing Characters

Hi All, I have a file which is delimeted with the character '. i need to replace this character with the same character and also a new line. Can anyone please help me with the tr command for this. Many thanks Karan (11 Replies)
Discussion started by: karansachdeva
11 Replies

8. Shell Programming and Scripting

replacing characters

hi all I have a file that has sone spaces in start then / at last. i want to get rid of this. how to do? eg. 11414/ 49878/ 27627/ I WANT THE FILE AS 11414 49878 27627 PLEASE HELP (3 Replies)
Discussion started by: infyanurag
3 Replies

9. HP-UX

Hex characters of ascii file

Hi, Whats the command or how do you display the hexadecimal characters of an ascii file. thanks Bud (2 Replies)
Discussion started by: budrito
2 Replies

10. Shell Programming and Scripting

Replacing all but last Hex characters in a text line

I must remove hex characters 0A and 0D from several fields within an MS Access Table. Since I don't think it can be done in Access, I am trying here. I am exporting a Table from Access (must be fixed length fields, I think, for my idea to work here) into a text format. I then want to run a... (2 Replies)
Discussion started by: BAH
2 Replies
Login or Register to Ask a Question