The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove parenthesis character (Perl) aristegui Shell Programming and Scripting 4 10-28-2008 02:53 PM
How to remove the specific lines from file using perl dipakg Shell Programming and Scripting 4 06-11-2008 03:45 AM
Perl RegExp to remove last character from strings ospreyeagle Shell Programming and Scripting 2 04-03-2008 03:55 PM
remove specific lines from flat file using perl meghana Shell Programming and Scripting 12 02-12-2008 09:50 PM
perl split funciton - special character "/" deepakwins UNIX for Dummies Questions & Answers 5 02-08-2008 12:19 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-24-2009
mingming88 mingming88 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 44
Perl use split and remove specific character

i want to split the input by a space and remove specific characters like full stop, comma...... etc. and then save each word in an array.
i got something below, but it didn't work. can anyone please help me?
Thank you

Code:
#!/usr/bin/perl -w

while (<>) 
{
	$line = <>;
	@word = split(' ', $line);
	$line =~ s/[^\w]/ /g;
	@lowerword = map {lc($_)}@word;
	print(@lowerword);
}
Input: Hello. How are you?
Output: hello how are you
  #2 (permalink)  
Old 05-24-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 536
Quote:
Originally Posted by mingming88 View Post
i want to split the input by a space and remove specific characters like full stop, comma...... etc. and then save each word in an array.
...
Input: Hello. How are you?
Output: hello how are you
You haven't mentioned if you want to change the case as well; I'm assuming you do (as per the output).

Code:
$
$ cat splitrem.pl
#!/usr/bin/perl -w
while (<>) {
  $line = $_;
  print "Input  : ",$line;
  $line =~ s/[[:punct:]]//g;
  $line = lc($line);
  @word = split(' ', $line);
  print "Output : ",join(" ",@word);
  print "\n";
}

$
$ echo "Hello, how are you?" | perl splitrem.pl
Input  : Hello, how are you?
Output : hello how are you
$
HTH,
tyler_durden
  #3 (permalink)  
Old 05-24-2009
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,879
As a oneliner:

Code:
% perl -E'say lc join "-",shift=~/(\w+)/g' 'Hello. How are you?'
hello-how-are-you
For older versions:

Code:
perl -le'print lc join "-",shift=~/(\w+)/g'
  #4 (permalink)  
Old 05-24-2009
mingming88 mingming88 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 44
Quote:
Originally Posted by durden_tyler View Post
You haven't mentioned if you want to change the case as well; I'm assuming you do (as per the output).

Code:
$
$ cat splitrem.pl
#!/usr/bin/perl -w
while (<>) {
  $line = $_;
  print "Input  : ",$line;
  $line =~ s/[[:punct:]]//g;
  $line = lc($line);
  @word = split(' ', $line);
  print "Output : ",join(" ",@word);
  print "\n";
}

$
$ echo "Hello, how are you?" | perl splitrem.pl
Input  : Hello, how are you?
Output : hello how are you
$
HTH,
tyler_durden
Thank you, it is working.
  #5 (permalink)  
Old 05-24-2009
mingming88 mingming88 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 44
Quote:
Originally Posted by durden_tyler View Post
You haven't mentioned if you want to change the case as well; I'm assuming you do (as per the output).

Code:
$
$ cat splitrem.pl
#!/usr/bin/perl -w
while (<>) {
  $line = $_;
  print "Input  : ",$line;
  $line =~ s/[[:punct:]]//g;
  $line = lc($line);
  @word = split(' ', $line);
  print "Output : ",join(" ",@word);
  print "\n";
}

$
$ echo "Hello, how are you?" | perl splitrem.pl
Input  : Hello, how are you?
Output : hello how are you
$
HTH,
tyler_durden

i have try yours, it is working, but mine is not working....
Code:
#!/usr/bin/perl -w

while (<>) 
{
	$line = $_;
  	$line =~ s/[[:punct:]]//g;
  	$line = lc($line);
 	@word = split(' ', $line);
	print (@word);
}
Input : Hello, how are you?
Output : hellohowareyou

i don't know why my output is sticking together? i want it to separate.... can anyone help?
Thank you
  #6 (permalink)  
Old 05-24-2009
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,879
You missed the join function.
  #7 (permalink)  
Old 05-24-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Quote:
Originally Posted by mingming88 View Post
i don't know why my output is sticking together? i want it to separate.... can anyone help?
Thank you
because you are printing the array, (the parenthisis are extra garbage not needed in your code):

Code:
print (@word);
you can use join() to print it as a string with any delimeter between the array elements, but if all you want is a single space you can simply double-quote the array which pefroms a bit of magic and converts the array into a string and adds a space between the elements:

Code:
print "@word";
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0