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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Oneliner ---split string to character by piping shell output to perl
# 1  
Old 01-07-2013
Oneliner ---split string to character by piping shell output to perl

Hello,
I was trying to split a string to characters by perl oneliner.
Code:
echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // '

But did not work as with bash script pipe:
Code:
echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq  -ic

Code:
      8  
      1 T
      1 a
      1 b
      1 c
      1 d
      3 e
      1 f
      1 g
      2 h
      1 i
      1 j
      1 k
      1 l
      1 m
      1 n
      4 o
      1 p
      1 q
      2 r
      1 s
      1 t
      2 u
      1 v
      1 w
      1 x
      1 y
      1 z

However, I want ignore the space and case.
1) What did I miss with my perl oneliner?
2) What are the options to ignore the space and case?
3) Want perl oneliner for learning purpose for 2)
Googled for a while, but no example for my exact case. Thanks a lot!
# 2  
Old 01-07-2013
The following code:

Code:
echo 'The quick brown fox jumps over the lazy dog' | perl -le 'print join "$/", grep /\S/, split //,<>'

produces:

Code:
T
h
e
q
u
i
c
k
b
r
o
w
n
f
o
x
j
u
m
p
s
o
v
e
r
t
h
e
l
a
z
y
d
o
g

Could you clarify what exactly do you mean by "ignore the case"?
# 3  
Old 01-07-2013
I meant to combine upper case and lower case letters. In this example only T and t are redundant. So that letter "t" appears 2 times, not 1 for each. Thanks!

Last edited by yifangt; 01-07-2013 at 10:18 AM.. Reason: improve expression, typo
# 4  
Old 01-07-2013
Code:
echo 'The quick brown fox jumps over the lazy dog' | 
  perl -le 'print join "$/", grep /\S/ && !$_{"\L$_"}++, split //, <>
    '

# 5  
Old 01-07-2013
If you need the number of occurrences:

Code:
echo 'The quick brown fox jumps over the lazy dog' | 
  perl -le '/\S/ and $l{$_} = ++$cnt{"\L$_"} for split //, <>;
  END {
    print $cnt{"\L$_"}, " ", $_
      for keys %l;
    }'

Do you need to preserve the original order?
# 6  
Old 01-07-2013
Thanks roudlov!
There is small bug where "T" and "t" were both counted 2 times. Actually only "t" is needed as "T" should be counted as "t". Anyway, "split //, <>" played the trick for my case. I can pipe the output with sort | unique to get the count:
Code:
echo 'The quick brown fox jumps over the lazy dog' | perl -le 'print join "$/", grep /\S/, split //, <>' | tr '[:upper:]' '[:lower:]' | sort | uniq -c

By the way, do you have a good resource to learn perl oneliner? Thanks again!
# 7  
Old 01-07-2013
All in Perl:
Code:
echo 'The quick brown fox jumps over the lazy dog' | 
  perl -le '/\S/ and $cnt{"\L$_"}++ for split //, <>;
  END {
    print "$cnt{$_} $_"
      for sort keys %cnt;
    }'

If I understand correctly what you mean by "perl oneliner",
I would suggest Effective Perl Programming: Ways to Write Better, More Idiomatic Perl (2nd Edition).
This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Split the string in perl

Hi All, How to split the string KAR_Celltick_Ban_GSMGW3 and want to pickup the third filed. Sometime the string may be "KAR_Celltick_Ban" like this Thanks in advance (1 Reply)
Discussion started by: sujit_kashyap
1 Replies

3. Shell Programming and Scripting

Regex to split a string and write the output in another file.

hi, i am trying to write a script to generate ouput in the following format: ##### buildappi abcd_sh nodebug.##### ##### buildappi ijk_sh nodebug.##### The given string is as follows: xtopSharedDLLs = "abcd_sh def_sh ijk_sh " \ + "jkl_sh any_sh... (15 Replies)
Discussion started by: Rashid Khan
15 Replies

4. Shell Programming and Scripting

split string using sed, perl

How can I split following input into stwo strings: Input: 1^~^2^~^3^~^4^~^5^~^6^~^7^~^8^~^9 Output: $string1 = 1^~^2^~^ $string2 = 3^~^4^~^5^~^6^~^7^~^8^~^9 Note: the length of string may vary, say upto 15. String 1 will contain only first two. string2 will contain... (10 Replies)
Discussion started by: som.nitk
10 Replies

5. Shell Programming and Scripting

perl oneliner to cut the file

Hi I have a file say text.txt and has data as below. text.txt ------- /abc/def/tom/hanks /abc/def/al/pacino /def/dgg/matt/damon Now I have to cut the field 3 and field 4 treating / as delimiter and save in the same file. Below is the how the output should be in the same file. I... (1 Reply)
Discussion started by: lijjumathew
1 Replies

6. Shell Programming and Scripting

perl oneliner not works .pl script

I am trying to take first 3 columns in a file which matches the word "abc", but i am getting the below error, <error> Global symbol "@F" requires explicit package name at ./new.pl </error> whereas when i give the below,grep abc /home/test/file.txt|perl -lane 'print \"$F $F $F\" in unix prompt... (4 Replies)
Discussion started by: anspks
4 Replies

7. Shell Programming and Scripting

how to get split output of a file, using perl script

Hi, I have file: data.log.1 ### s1 main.build.3495 main.build.199 main.build.3408 ###s2 main.build.3495 main.build.3408 main.build.199 I want to read this file and store in two arrays in Perl. I have following command, which is working fine on command prompt. perl -n -e... (1 Reply)
Discussion started by: ashvini
1 Replies

8. 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

9. Shell Programming and Scripting

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 #!/usr/bin/perl -w while (<>) { $line = <>; @word = split(' ',... (6 Replies)
Discussion started by: mingming88
6 Replies

10. Shell Programming and Scripting

Piping and assigning output to a variable in Perl

Hi All, I am trying to convert the below Csh code into Perl. But i have the following error. Can any expert help ? Error: ls: *tac: No such file or directory Csh set $ST_file = `ls -rt *$testid*st*|tail -1`; Perl my $ST_file = `ls -rt *$testid*st*|tail -1`; (10 Replies)
Discussion started by: Raynon
10 Replies
Login or Register to Ask a Question