Perl: Pattern to remove words with less than 2 characters.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Pattern to remove words with less than 2 characters.
# 1  
Old 10-22-2012
Perl: Pattern to remove words with less than 2 characters.

Hello. I've been thinking about how to go about this. I know I'm close but still does not work. I need to remove any word in that is not at least 2 characters long. I've removed all the non-alphabetic characters already (numbers included). Here's an example:

Code:
my $string = "This string is a sample string a e io u";

# Some pattern matching expression here.

print $string . "\n";    

# Output should be "This string is  sample string io"

I tried using something like this below:
Code:
$string =~ s/[a-z]{1}//g;

But it doesn't work.
# 2  
Old 10-22-2012
Code:
$string =~ s/\b[^ \t]\b//g;

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 10-22-2012
Quote:
Originally Posted by rdrtx1
Code:
$string =~ s/\b[^ \t]\b//g;

Works perfectly!
Thanks rdrtx1.
# 4  
Old 10-24-2012
Alternatively:

Code:
$string =~ s/\b\S\b//g;

tyler_durden
# 5  
Old 10-24-2012
Quote:
Originally Posted by durden_tyler
Alternatively:

Code:
$string =~ s/\b\S\b//g;

tyler_durden
Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 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. Shell Programming and Scripting

Need an awk / sed / or perl one-liner to remove last 4 characters with non-unique pattern.

Hi, I'm writing a ksh script and trying to use an awk / sed / or perl one-liner to remove the last 4 characters of a line in a file if it begins with a period. Here is the contents of the file... the column in which I want to remove the last 4 characters is the last column. ($6 in awk). I've... (10 Replies)
Discussion started by: right_coaster
10 Replies

4. Shell Programming and Scripting

Perl to remove words between patterns

Hi, I am using following code to remove words between start and end points. $mystring = "The start text always precedes the end of the end text."; if($mystring =~ s/start(.*)end/\0/) { print $1; print "\n"; print $mystring; } But this is writing special chars in place of... (1 Reply)
Discussion started by: sarbjit
1 Replies

5. UNIX for Dummies Questions & Answers

Vim help - delete words in a file or characters after pattern

I have a file with words that begin with character #. Whenver that character is found that word should be deleted throughout the file. How do I do that in VIM. e.g: afkajfa ladfa ljafa #222222 kjafad ljl afajkj kjlj uouu #44444 jlkj lkjl Output should be afkajfa ladfa ljafa kjafad... (1 Reply)
Discussion started by: osbourneric
1 Replies

6. Shell Programming and Scripting

Remove junk characters using Perl

Guys, can you help me in removing the junk character "^S" from the below line using perl Reference Data Not Recognised ^S Where a value is provided by the consuming system, which is not reco Thanks, M.Mohan (1 Reply)
Discussion started by: mohan_xunil
1 Replies

7. Shell Programming and Scripting

Need to remove few characters from each line till a pattern is matched

Hi All, I want to remove first few characthers from starting of the line till ',' Comma... which needs to be done for all the lines in the file Eg: File content 1,"1234",emp1,1234 2,"2345",emp2,2345 Expected output is ,"1234",emp1,1234 ,"2345",emp2,2345 How can parse... (4 Replies)
Discussion started by: kiranlalka
4 Replies

8. Shell Programming and Scripting

remove special characters from text using PERL

Hi, I am stuck with a problem here. Suppose i have a variable which is assigned some string containing special charatcers. for eg: $a="abcdef^bbwk#kdbcd@"; I have to remove the special characters using Perl. The text is assigned to the variable implicitly. How to do it? (1 Reply)
Discussion started by: agarwal
1 Replies

9. UNIX for Advanced & Expert Users

I am trying to find pattern between two words but unable to get that pattern..

HI.... It's fallow up file .. #./show.sh click enter button.. i am gettng the fallowup file. its keep on running every time why because there are lots of users working on it. In that file i want to search pattern between two words for ex: SELECT DISTINCT... (7 Replies)
Discussion started by: ksr.test
7 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