Add a space between certain characters Using Perl Scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add a space between certain characters Using Perl Scripting
# 1  
Old 05-04-2012
Add a space between certain characters Using Perl Scripting

Hello All,

I have a variable which is having values like this..

Code:
$var=A,B,C,D,E,F,G,H

I want to add space after every three alphabets so that my final variable should look like this..

Code:
$var=A,B,C, D,E,F, G,H

the space should come after the comma of third alphabet..

Pls tell me how to achive this using perl scripting..

Thanks in advance..
# 2  
Old 05-04-2012
Code:
 
$ perl a.pl 
A,B,C, D,E,F, G,H
 
$ cat a.pl
#!/bin/perl -w
$var="A,B,C,D,E,F,G,H";
@splitArray=split(/,/,$var);
for($i=0;$i<=scalar(@splitArray);$i=$i+3)
{
        if ($i==0) {next;}
        $splitArray[$i]=" " . $splitArray[$i];
}
print join (",",@splitArray);

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 05-04-2012
Thanks Kamaraj.. working fine Smilie

---------- Post updated 05-04-12 at 07:39 AM ---------- Previous update was 05-03-12 at 11:33 PM ----------

Quote:
Originally Posted by itkamaraj
Code:
 
$ perl a.pl 
A,B,C, D,E,F, G,H
 
$ cat a.pl
#!/bin/perl -w
$var="A,B,C,D,E,F,G,H";
@splitArray=split(/,/,$var);
for($i=0;$i<=scalar(@splitArray);$i=$i+3)
{
        if ($i==0) {next;}
        $splitArray[$i]=" " . $splitArray[$i];
}
print join (",",@splitArray);

Hi I have a small problem here...

the above code is working fine if the no of aplhabets in input variable is not multiple of 3... if it is mutliple of three then it places a comma at the end like this

Code:
case 1:

input : $var="A,B,C"   //Only 3 alphabets

output: A,B,C,  // comma not reqd at the end

expected : A,B,C

case2:

input : $var="A,B,C,D,E,F"  // 6 alphabets

output: A,B,C, D,E,F,  // comma not reqd at the end

expected : A,B,C, D,E,F  // a white space after 3 alphabets

case 3: 

input : $var="A,B,C,D,E,F,G,H" // not a multiple of 3

output: A,B,C, D,E,F, G,H  // coming as expected

expected : same as output

pls help me to remove the comma coming at the end

---------- Post updated at 08:22 AM ---------- Previous update was at 07:39 AM ----------

can somebody pls help me
# 4  
Old 05-04-2012
Hi smarty86,

One way:
Code:
$ cat script.pl
use warnings;
use strict;

while ( my $var = <DATA> ) {
        chomp $var;
        $var =~ s/((?:[[:upper:]],){3})(?!$)/$1 /g;
        printf qq[%s\n], $var;
}

__DATA__
A,B,C,D,E,F,G,H
A,B,C
A,B,C,D,E,F
$ perl script.pl
A,B,C, D,E,F, G,H
A,B,C
A,B,C, D,E,F

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove characters and replace with space

tr -cd '\11\12\15\40-\176' < file-with-binary-chars > clean-file This removes special characters but how can I replace it with space (4 Replies)
Discussion started by: eskay
4 Replies

2. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

3. Shell Programming and Scripting

Know unix scripting somewhat ,now how to add perl to my skillset.

Hi All, I have learned a bit of unix scripting since i have been working on it for the past 2 months. I would like to add perl to my skills. I have read the O'reillys Learning perl book. Can someone suggest how to use this forum for facing newer challenges on a daily basis starting with the... (1 Reply)
Discussion started by: kunwar
1 Replies

4. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

5. Shell Programming and Scripting

Insert space between characters using sed

Input: Youcaneasilydothisbyhighlightingyourcode. Putting space after three characters. You can eas ily dot his byh igh lig hti ngy our cod e. How can i do this using sed? (10 Replies)
Discussion started by: cola
10 Replies

6. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

7. Shell Programming and Scripting

escape space characters in loop from file

Hi Everyone! I want to build sql inserts from a list of countries/regions saved in a file. The list looks like this: United Kingdom Czech Republic ... The script I run is: while read i; do var=`expr $var + 1`; echo "INSERT INTO calltypes VALUES($var, '$i','$i');" >>... (5 Replies)
Discussion started by: linuca
5 Replies

8. Shell Programming and Scripting

remove space characters

hello I have this output ifspeed 100000000 ifspeed 100000000 collisions 413 collisions 10 duplex full duplex ... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

9. UNIX for Dummies Questions & Answers

wc of characters in a file ignoring white space

Hi everyone, $ more abcdefg.ksh abcdef alpha beta gamma abcdef abcdef lmnop $ wc sachin1.ksh 5 7 132 abcdefg.ksh if you see it shows that file has got 240 characters. I actually want to count how many characters... (1 Reply)
Discussion started by: sachin.gangadha
1 Replies

10. Shell Programming and Scripting

how to separate every 10 characters then space then another 10 charactes

suppose u have a file P2122 AAJJSKKSKSLSKSKMSKMS P2123 AASSJMWJNSJNWHNSKSJ P2126 AHJMKNSAJNSKAKMOKALM P3533 KKKSJMAKKLLMLMSLMSPM P2122 JKKSKKMKKSKKKS--------------NSNJ P2123 NSJN---------MSKMKMKKSLLNSLL P2126 JNJSJNWKMSK-------------SKKSMK P3533 JSNSNPWLLL-SKK----SKKKSKKS so the... (2 Replies)
Discussion started by: cdfd123
2 Replies
Login or Register to Ask a Question