How does this sed expression to remove non-alpha characters work?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How does this sed expression to remove non-alpha characters work?
# 1  
Old 07-24-2013
How does this sed expression to remove non-alpha characters work?

Hello!

I know that this expression gets rid of non-alphanumeric characters:
Code:
sed 's/[^a-zA-z0-9]//g'

and I understand that it is replacing them with nothing - hence the '//'-, but I don't understand how it's doing it.
It seems it's finding strings that begin with alphanumeric and replacing them with nothing! Obviously that would not give the required output so I'm a little confused and clearly missing something...

Thanks for your help!

Last edited by Don Cragun; 07-24-2013 at 01:48 PM.. Reason: Added CODE tags.
# 2  
Old 07-24-2013
Could you please post your input code as well your expected output so that we can help you more on same.

Thanks,
R. Singh
# 3  
Old 07-24-2013
Quote:
Originally Posted by bgnersoon2be#1
Hello!

I know that this expression gets rid of non-alphanumeric characters:
Code:
sed 's/[^a-zA-z0-9]//g'

and I understand that it is replacing them with nothing - hence the '//'-, but I don't understand how it's doing it.
It seems it's finding strings that begin with alphanumeric and replacing them with nothing! Obviously that would not give the required output so I'm a little confused and clearly missing something...

Thanks for your help!
The circumflex I marked in red in your code (and please use CODE tags) is not an anchor. When a circumflex is the first character in a matching expression (i.e., [expression specifying a set of characters to match]), it specifies a non-matching expression where all of the characters except those specified by the expression following the circumflex will be matched. So, in this case everything that is not a lowercase letter, not an uppercase letter, and not a digit is removed (or, as you said, replaced by nothing).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: -e expression #1, char 2: extra characters after command

Greetings.. getting the error while execution of the script, correct where i am missing #!/bin/bash DATE=`date +%Y-%m-%d:::%H:%M` HOSTNAME=`hostname` TXT="/log/temp.txt" LOGPATH="/log1/commanlogs/" IP=`/sbin/ifconfig | grep -i inet| head -n1| awk '{print $2}'| awk -F : '{print $2}'`... (7 Replies)
Discussion started by: manju98458
7 Replies

2. Shell Programming and Scripting

perl regular expression to remove the special characters

I had a string in perl script as below. Tue Augáá7 03:54:12 2012 Now I need to replace the special character with space. After removing the special chaacters Tue Aug 7 03:54:12 2012 Could anyone please help me here for writing the regular expression? Thanks in advance.. Regards, GS (1 Reply)
Discussion started by: giridhar276
1 Replies

3. UNIX for Dummies Questions & Answers

sed remove expression from output I'm watching

I'm watching a particular expression as it is appended in a line to a file: tail -f LOG | sed -n /"$@"/p So whatever value I pass into this script will tail -f the file, but only show me lines that contain the value: lgwatch expression However some of the output contains a #20 control... (8 Replies)
Discussion started by: MaindotC
8 Replies

4. Shell Programming and Scripting

Remove the Characters '[' and ']' with Sed

Hi, I am new to Sed and would like to know if it is possible to remove the characters . I have a couple of files with a keyword and would like to remove the substring. I am Using sed s/// but Its not working Thanks for your Support Andrew Borg (2 Replies)
Discussion started by: andrewborg
2 Replies

5. Shell Programming and Scripting

Sed or trim to remove non alphanumeric and alpha characters?

Hi All, I am new to Unix and trying to run some scripting on a linux box. I am trying to remove the non alphanumeric characters and alpha characters from the following line. <measResults>883250 869.898 86432.4 809875.22 804609 60023 59715 </measResults> Desired output is: 883250... (6 Replies)
Discussion started by: jackma
6 Replies

6. UNIX for Dummies Questions & Answers

sed command to remove characters help!

I am trying to analyse a large file of sequencing data, example of first 10 lines below, @HWUSI-EAS656_0044_FC:7:1:2447:1039#GCAATT/1 GNCTATGGCTTGCCGGGCTCAGGGAAGACAATCATAGCCATGAAAATCATGGAAAAGATCAGAAAAACATTTCAA +HWUSI-EAS656_0044_FC:7:1:2447:1039#GCAATT/1... (1 Reply)
Discussion started by: Adeleh
1 Replies

7. Shell Programming and Scripting

non alpha characters in sed + making it fast?

hello, I'm trying to write the fastest sed command possible (large files will be processed) to replace RICH with NICK in a file which looks like this (below) if the occurance of RICH is uppercase, replace with uppercase if it's lowercase, replace with lowercase SOMTHING_RICH_SOMTHING <- replace... (10 Replies)
Discussion started by: rich@ardz
10 Replies

8. Shell Programming and Scripting

grep or sed. How to remove certain characters

Here is my problem. I have a list of phone numbers that I want to use only the last 4 digits as PINs for something I am working on. I have all the numbers in a file but now I want to be removed all items EXCEPT the last 4 digits. I have seen sed commands and some grep commands but I am... (10 Replies)
Discussion started by: Sucio
10 Replies

9. Shell Programming and Scripting

Sed replace characters not equal to an expression

Hi all, Suppose I have a file with the contents below, and I only want to print words %S_ then | sort -u. ------------------------------ The %S_MSG that starts with '%.*s' is too long. Maximum length is %d. The %S_MSG name '%.*s' contains more than the maximum number of prefixes. The... (5 Replies)
Discussion started by: poldo
5 Replies

10. Shell Programming and Scripting

Perl: How do I remove leading non alpha characters

Hi, Sorry for silly question, but I'm trying to write a perl script to operate a log file that is in following format: (4)ab=1234/(10)bc=abcdef9876/cd=0.... The number in the brackets is the lenghts of the field, "/" is the field separator. Brackets are not leading every field. What I'm... (9 Replies)
Discussion started by: Juha
9 Replies
Login or Register to Ask a Question