delete all characters that aren't a letter or number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete all characters that aren't a letter or number
# 1  
Old 01-21-2012
MySQL delete all characters that aren't a letter or number

hey Smilie

if i have a variable that is

example=lewisdenny(copywrite symbol)

so its not a nomal letter but a symbol, how can i remove everything in the varible that isnt letter or number thanks Smilie

and as a side little question do you know how to remove .zip from a file like if i

Code:
ls /temp
test.zip

how can i remove the .zip and set it as a varable like this?
Code:
example=$(ls /temp | grep (command to remove .zip)

thanks
# 2  
Old 01-21-2012
Remove numbers and letters
Code:
variable=$(printf "$variable" | sed 's/[0-9a-zA-Z]//g')

Remove zip extension
Code:
variable=$(printf "$variable" | sed 's/\.zip$//g')

# 3  
Old 01-21-2012
You can try % or %%. ${variable%%.*}
# 4  
Old 01-21-2012
Not in all shells:
Code:
variable=${variable/%\.zip}

---------- Post updated at 11:53 AM ---------- Previous update was at 11:49 AM ----------

What you're looking for:
Code:
 example=$(ls /temp | sed 's/\.zip$//')

# 5  
Old 01-21-2012
thanks

thanks for the answers but im trying to remove everything BUT the letters and numbers Smilie
# 6  
Old 01-21-2012
Sorry lad, I was almost sleeping!!! Hahaha

---------- Post updated at 01:09 PM ---------- Previous update was at 12:54 PM ----------

This leaves only the letters and numbers (removes ©...):

Code:
$ example=lewisdenny©
$ echo $example
lewisdenny©
$ example=$(printf "$example" | sed 's/[^0-9a-zA-Z]//g')
$ echo $example
lewisdenny


Last edited by teresaejunior; 01-21-2012 at 11:07 AM..
This User Gave Thanks to teresaejunior For This Post:
# 7  
Old 01-21-2012
You could also use the POSIX character class "[:alnum:]" like so -

Code:
$
$ echo $str
lewisdenny©
$
$ str=$(echo $str | sed 's/[^[:alnum:]]//g')
$
$ echo $str
lewisdenny
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

2. UNIX for Dummies Questions & Answers

Delete all words not containing letter /s/

I have a word file that looks like: pens binder spiral user I want to delete all the words without the letter /s/, so output looks like: pens spiral user I tried using sed: sed '//d' infile.txt > out.txt (5 Replies)
Discussion started by: pxalpine
5 Replies

3. UNIX for Dummies Questions & Answers

Assigning a number to a letter without printing it

Hello Unix.com ! Newbie question. With awk, i can we give a particular letter a number without printing the number? input: X|X A|X A|A X|A If field is "A", then it counts as 2 If field is "X", then it counts as 3 Multiply the 2 first field and give the result in the 3th field, but... (7 Replies)
Discussion started by: beca123456
7 Replies

4. Shell Programming and Scripting

how to delete the line if the first letter is a single digit

Hi, I'm trying to acheive the following, I have a dat file in which i have several addresses, If the address starts with a single digit then i have to delete the line, if it starts with 2 or more digits then i have to keep the line Here is a sample of my file: 377 CARRER DE LA... (5 Replies)
Discussion started by: ramky79
5 Replies

5. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

6. Shell Programming and Scripting

Get letter from number and assign to a variable

Hi to all in forum, I'm trying to convert the letter number between 1 (A) and 26 (Z), that part is working, my issue is how to assign the printf output to a variable:LetterNumber=10 printf "\x$(printf %x $((${LetterNumber}+64)))" $ J #The problem, how to assign printf output (J in this... (8 Replies)
Discussion started by: Ophiuchus
8 Replies

7. Shell Programming and Scripting

Check input for lenght, special characters and letter case

I made menu script for users so they can run other script without going in shell just from menu. But i must control their input. These are criteria: Input must have 4 signs First two signs are always lower case letters Input shall not have some special signs just letters and numbers ... (1 Reply)
Discussion started by: waso
1 Replies

8. Shell Programming and Scripting

How to delete characters using a file

Hi All, I have a configuration file (file.cfg) in which data will be like this ; , _ + a to z A to Z Now i have to read a textfile (file.txt) and i need to check whether there is any other character present in text file that is not existing in (file.cfg). If other characters are present... (4 Replies)
Discussion started by: krishna_gnv
4 Replies

9. Shell Programming and Scripting

Delete lines that starts with a certain letter

How can I delete those lines that starts with a certain letter? abc def ghi xyz abc def ace gik moq abe imq gxm I want to delete the line that starts with "x". Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

10. AIX

Delete specific characters

Hi every1 Well i have a list of numbers e.g 12304 13450 01234 00123 14567 what i want is a command to check if the number is starting from 0 and then delete the 0 without doing anything else!!!! any help wud b appreciated!!!!!!!!:( (4 Replies)
Discussion started by: masquerer
4 Replies
Login or Register to Ask a Question