Perl use split and remove specific character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl use split and remove specific character
# 1  
Old 05-24-2009
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  
Old 05-24-2009
Quote:
Originally Posted by mingming88
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  
Old 05-24-2009
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  
Old 05-24-2009
Quote:
Originally Posted by durden_tyler
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  
Old 05-24-2009
Quote:
Originally Posted by durden_tyler
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  
Old 05-24-2009
You missed the join function.
# 7  
Old 05-24-2009
Quote:
Originally Posted by mingming88
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";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Perl split string separated by special character

Hello I have string (string can have more sections) LINE="AA;BB;CC;DD;EE"I would like to assigne each part of string separated by ";" to some new variable. Can someone help? (4 Replies)
Discussion started by: vikus
4 Replies

3. Shell Programming and Scripting

Remove line with specific character

HI Input :- Aog:0rt__dev_8 LAAXU24 vs.3 LAA40l0 ** LAAXU241 ** Output :- Aog:0rt__dev_8 LAAXU24 vs.3 Delete the line with ** (3 Replies)
Discussion started by: pareshkp
3 Replies

4. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

5. Shell Programming and Scripting

remove special character from a specific column

Hello , i have a text file like this : A123 c12AB c32DD aaaa B123 23DS 12QW bbbb C123 2GR 3RG cccccc i want to remove the numbers from second and third column only. i tried this : perl -pe 's///g' file.txt > newfile.txt but it will remove the number from... (7 Replies)
Discussion started by: shelladdict
7 Replies

6. Shell Programming and Scripting

Replace/Remove not specific text in perl

Hello, Consider that i have many files that have the below format: file1 900 7777 1000 5 6 23 nnnnnnnnnnnnnnnnnn 1100 kkkkkkk file2 900 1989 1000 5 3 10 kkkdfdfdffd 1100 kkkkkkk What i would like to do is on every file to search the line that starts with... (4 Replies)
Discussion started by: chriss_58
4 Replies

7. Shell Programming and Scripting

Unix Perl split special character $

All I'm trying to split a string at the $ into arrays @data:=<dataFile> a $3.33 b $4.44 dfg $0.56 The split command I have been playing with is: split(/\$/, @data) which results with a .33 b .44 dfg .56 any help with this is appreciated /r Rick (9 Replies)
Discussion started by: schultz2146
9 Replies

8. Shell Programming and Scripting

Remove parenthesis character (Perl)

Hello, i'm unable to remove the parenthesis character. With $parsed_AsciiName =~ s/\(//; the string is the same And with $parsed_AsciiName =~ s/(//; i retrieve "Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE" Any ideas, please? thank you in advanced. (4 Replies)
Discussion started by: aristegui
4 Replies

9. Shell Programming and Scripting

How to remove the specific lines from file using perl

Can anyone tell me what could be the solution to following : I have one .txt file which contains some "seed" information. This seed may appear multiple time in the file so what I want do is if this seed appears again in the file then that line should be removed. Please provide the script code... (4 Replies)
Discussion started by: dipakg
4 Replies

10. Shell Programming and Scripting

Perl RegExp to remove last character from strings

I use SAS (a statistical software) and have to remove last character or the last 1/2 numbers that appear after characters from the string using Perl Regular Expression (which is recognized by SAS). Input: f183ii10 f183ii2 f182ii1 f182ii2 f183iim f22ii f22ii11 f22ii12 pmh4 pmhm Desired... (2 Replies)
Discussion started by: ospreyeagle
2 Replies
Login or Register to Ask a Question