sed replacing specific characters and control characters by escaping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replacing specific characters and control characters by escaping
# 8  
Old 05-02-2012
Another thing you could do is give a range of the characters you do want, then invert it with ^, and thus avoid specifying all possible nasty characters in the universe.

Code:
# Everything but alphanumerics and spaces replaced with _
echo "..." | sed 's/[^a-zA-Z0-9 \t]/_/g'

# 9  
Old 05-02-2012
Quote:
Originally Posted by ijustneeda
Wonder how fast tr is compared to sed on GB of text files?
Faster, certainly. tr is extremely simple, considering everything one character at a time instead of worrying about entire expressions. Far less work.

It also has no limitations on the size of lines for the same reason, while some versions of sed break on lines longer than 2000 bytes or so.
# 10  
Old 05-02-2012
The only problem might be if there are UTF-8 characters as tr cannot translate those AFAIK.
# 11  
Old 05-03-2012
By definition it can't since those are multi-byte sequences. It can certainly strip them out, though, since they're all values higher than ASCII 127.
Code:
tr -d '[\200-\377]' < input > output

This User Gave Thanks to Corona688 For This Post:
# 12  
Old 05-03-2012
I tried it, and like that it did not remove all characters, but like this it did:
Code:
LANG=C tr -d '[\200-\377]' < input > output

Code:
$ printf "%s\n" 'An preost wes on leoden, Laȝamon was ihoten
He wes Leovenağes sone -- liğe him be Drihten.
He wonede at Ernleȝe at æğelen are chirechen,
Uppen Sevarne staşe, sel şar him şuhte,
Onfest Radestone, şer he bock radde.' |
LANG=C tr -d '[\200-\377]'
An preost wes on leoden, Laamon was ihoten
He wes Leovenaes sone -- lie him be Drihten.
He wonede at Ernlee at elen are chirechen,
Uppen Sevarne stae, sel ar him uhte,
Onfest Radestone, er he bock radde.


Last edited by Scrutinizer; 05-03-2012 at 04:47 PM..
This User Gave Thanks to Scrutinizer 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 control characters in HPUX

Hello dears, Please tell me how can I replace control characters with normal string. I have a file that contains normal string and ^A ^C control characters. I tried to use sed and awk without any luck. sed 's/^A/foo/g' text > text1 //not worked sed 's/\x01/foo/g' text > text1 ... (6 Replies)
Discussion started by: sembii
6 Replies

2. Shell Programming and Scripting

Replacing specific characters using sed

Hi, I have a text file which is output from a server and it lists all the files in a specific volume. However, the volume name appears as volume_name:. I would like to replace this with \\volume_name\volume_name. This is not a problem in itself as I can use sed to globally look for the... (8 Replies)
Discussion started by: vnayak
8 Replies

3. Shell Programming and Scripting

How delete characters of specific line with sed?

Hi, I have a text file with some lines like this: /MEDIA/DISK1/23568742.MOV /MEDIA/DISK1/87456321.AVI /MEDIA/DISK2/PART1/45753131.AVI /IMPORT/44452.WAV ... I want to remove the last 12 characters in each line that it ends "AVI". Should look like this: /MEDIA/DISK1/23568742.MOV... (12 Replies)
Discussion started by: inaki
12 Replies

4. Shell Programming and Scripting

Sed - merge lines bw 2 specific characters

Hi, I have a bash script and I am looking for a command that will merge specific lines together. Sample Data: registration time = 1300890272 Id = 1 setd = 0 tagunt = 26 tagId=6, length=8, value= tagId=9, length=5, value= tagId=7, length=2, value= tagId=16, length=2, value= tagId=32,... (8 Replies)
Discussion started by: Winsarc
8 Replies

5. UNIX for Dummies Questions & Answers

sed replacing in vi, / characters in the middle

I tried to replace the following in vi: old: 'e/thesis/pp/zones/zones' new: 'd/so162/fix/pp' For that, I used: :%s/e/thesis/pp/zones/zones/d/so162/fix/pp/g but doesn't work, a trailing character error message appeared. How can I get it? Thanks in advance (3 Replies)
Discussion started by: Gery
3 Replies

6. UNIX for Dummies Questions & Answers

Escaping non-readable characters using grep, sed or awk

I'm trying to parse out DNS logs from dozens of different domain controllers over a large period of time. The logs are rolled up into individual text files by size, which may contain only a portion of a day's activity or several day's worth (depending on amount of activity). I'm splitting them by... (4 Replies)
Discussion started by: seanwpaul
4 Replies

7. Shell Programming and Scripting

Escaping Special characters

I want to append the following line to /var/spool/cron/root: */7 * * * * /root/'Linux CPU (EDF).sh' > /dev/null 2>&1 How to accomplish this using echo? ---------- Post updated at 04:09 PM ---------- Previous update was at 04:07 PM ---------- "Linux CPU (EDF)" is actually stored in a... (11 Replies)
Discussion started by: proactiveaditya
11 Replies

8. Shell Programming and Scripting

help on sed replacing special characters

Hello, I have a file with many lines with below format: \abc\\1234 jkl\\567 def\\345 \pqr\\567 \xyz\\234 Here, i need to do 2 things. 1. replace \\ with \ 2. remove starting \ so output to be as below: (11 Replies)
Discussion started by: prvnrk
11 Replies

9. Shell Programming and Scripting

replacing specific characters in a string

Hi Friends, Following is an output of a script OPWQERIUTYKLSADFJHGXZNMCVBWQOPERIUTYKLSADFJHGXZNMCVB I want to replace above string's 11 to 17 character by ******* (7 stars) How can it be done? Please somebody guide me. (6 Replies)
Discussion started by: anushree.a
6 Replies

10. Shell Programming and Scripting

how to replace control characters using sed?

How can I use sed to replace a ctrl character such as 'new line' (\0a) to something else? Or any other good command can do this job? Thanks, Hillxy (5 Replies)
Discussion started by: hillxy
5 Replies
Login or Register to Ask a Question