Remove characters and replace with space

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Remove characters and replace with space
# 1  
Old 11-10-2016
Remove characters and replace with space

Code:
tr -cd '\11\12\15\40-\176' < file-with-binary-chars > clean-file

This removes special characters but how can I replace it with space

Last edited by Scrutinizer; 11-10-2016 at 08:37 PM.. Reason: code tags
# 2  
Old 11-11-2016
You can use the command man tr and will find the -s switch which stands for substitute. But now you might not have to look it up anymore.
# 3  
Old 11-11-2016
Hi zaxxon,
Sorry, but the tr -s option is not a substitute option; it is a request to squeeze repeated occurrences of a character in the output to a single occurrence.

Hi essay,
Try:
Code:
tr -c '\11\12\15\40-\176' '[ *0]' < file-with-binary-chars > clean-file

These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 11-11-2016
I'm afraid it's not that easy - in UTF8 (and other) encoded files, characters above the ASCII set are represented by more than one byte, of which every single one will be replaced by a space when running above command. Using the -s option, on the other hand, will squeeze any count of adjacent non-ASCII chars into one single byte.

Last edited by RudiC; 11-11-2016 at 07:15 AM..
# 5  
Old 11-11-2016
Would this come close to what you need:
Code:
LC_ALL=C sed 's/[\xC0-\xDF]./ /g; s/[\xE0-\xFF]../ /g; s/[^[:alnum:][:space:]\o011\o012\o015]/ /g' file

?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove first 3 leadings zeros and replace with space

Hi Folks - I need help manipulating a file. For column 2, I need to replace the first 3 leading zeros with spaces. The file looks like such: 00098|00011250000003|00000000000.0200|D|1|07|51|04INDP |04|00820|CS|000000|092717|000000000000.0000|000|... (3 Replies)
Discussion started by: SIMMS7400
3 Replies

2. Shell Programming and Scripting

Remove first 2 characters and last two characters of each line

here's what im trying to do. i have a file containing lines similar to this: data.txt: 1hsRmRsbHRiSFZNTTA1dlEyMWFkbU5wUW5CSlIyeDFTVU5SYjJOSFRuWmpia0ZuWXpKV2FHTnRU 1lKUnpWMldrZFZaMG95V25oYQpSelEyWTBka2QyRklhSHBrUjA1b1kwUkJkd3BOVXpWM1lVaG5k... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. UNIX for Dummies Questions & Answers

How to replace and remove few junk characters from a specific field?

I would like to remove all characters starting with "%" and ending with ")" in the 4th field - please help!! 1412007819.864 /device/services/heartbeatxx 204 0.547%!i(int=0) 0.434 0.112 1412007819.866 /device/services/heartbeatxx 204 0.547%!i(int=1) 0.423 0.123... (10 Replies)
Discussion started by: snemuk14
10 Replies

4. Shell Programming and Scripting

How to remove alphabets/special characters/space in the 5th field of a tab delimited file?

Thank you for 4 looking this post. We have a tab delimited file where we are facing problem in a lot of funny character. I have tried using awk but failed that is not working. In the 5th field ID which is supposed to be a integer only of that file, we are getting corrupted data as below. I... (12 Replies)
Discussion started by: Srithar
12 Replies

5. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

6. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

7. Shell Programming and Scripting

Insert space between characters using sed

Input: Youcaneasilydothisbyhighlightingyourcode. Putting space after three characters. You can eas ily dot his byh igh lig hti ngy our cod e. How can i do this using sed? (10 Replies)
Discussion started by: cola
10 Replies

8. Shell Programming and Scripting

How to replace characters with random characters

I've got a file (numbers.txt) filled with numbers and I want to replace each one of those numbers with a new random number between 0 and 9. This is my script so far: #!/bin/bash rand=$(($RANDOM % 9)) sed -i s//$rand/g numbers.txtThe problem that I have is that it replaces each number with just... (2 Replies)
Discussion started by: hellocatfood
2 Replies

9. Shell Programming and Scripting

Replace long space to become one space?

Hi, i have the log attached. Actually i want the long space just become 1 space left like this : Rgds, (12 Replies)
Discussion started by: justbow
12 Replies

10. Shell Programming and Scripting

remove space characters

hello I have this output ifspeed 100000000 ifspeed 100000000 collisions 413 collisions 10 duplex full duplex ... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies
Login or Register to Ask a Question