The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
String substitution ctrl-alt-del Shell Programming and Scripting 3 10-14-2008 02:37 AM
String Substitution laxmi UNIX for Advanced & Expert Users 1 02-16-2008 06:08 AM
pattern match and substitution, can you help? frustrated1 Shell Programming and Scripting 4 02-20-2006 08:48 AM
vi/vim : complex pattern substitution c444l UNIX for Dummies Questions & Answers 2 08-03-2004 10:04 AM
Sed String Substitution pciatto UNIX for Dummies Questions & Answers 2 04-29-2002 10:10 AM

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

Join Date: Aug 2008
Location: Bangalore, INDIA
Posts: 55
Perl:string substitution Pattern: ='abc...',

Hi friends,

I want to substitute "a ='....'," with ":" in everywhere in a string using Perl.

Details:
----------
my $str= " c1='fgfasfgasggfgff.,akhkhahha', c2='bbbn', c3='hg5 sh' ";

Required o/p: $str= " c1:c2:c3 "

I tried as below:

$str=~ s/=\'.*\',/:/g ;

print "str= $str\n";



Acual o/p:

str= c1: c3='hg5 sh'


Please help me to get the required o/p..


  #2 (permalink)  
Old 01-21-2009
Niroj Niroj is offline
Registered User
  
 

Join Date: Aug 2008
Location: Bangalore, INDIA
Posts: 55
Trying more...

my $str=" c1='fgfasfgasggfgff.,akhk', c2='bbbn', c3='hg5 sh' ";
print "Before substitution-> $str\n";
$str=~ s/(=.*\'.*\'.*,?)/:/g;

print "After substitution-> str= $str\n";

o/p:
==============
Before substitution-> c1='fgfasfgasggfgff.,akhk', c2='bbbn', c3='hg5 sh'
After substitution-> str= c1:

But I need the below o/p..
================

str= c1:c2:c3

Here my problem is I am not able substitute ':' for " ='bbbn'," & "c3='hg5 sh' " doesn't end with a ","


plz help...
  #3 (permalink)  
Old 01-21-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Code:
my $str = " c1='fgfasfgasggfgff.,akhk', c2='bbbn', c3='hg5 sh' ";
print "Before substitution-> $str\n";
$str =~ s/='[^']*',?/:/g;
print $str;
  #4 (permalink)  
Old 01-22-2009
Niroj Niroj is offline
Registered User
  
 

Join Date: Aug 2008
Location: Bangalore, INDIA
Posts: 55
Quote:
Originally Posted by KevinADC View Post
Code:
my $str = " c1='fgfasfgasggfgff.,akhk', c2='bbbn', c3='hg5 sh' ";
print "Before substitution-> $str\n";
$str =~ s/='[^']*',?/:/g;
print $str;
o/p=> c1: c2: c3:
Thanks Kelvin ! But could plz explain How does it work for c3='hg5 sh'.
(c3='.....' does not end with a comma "," !!)?

And my 2nd query is why "$str =~ s/='.*',?/:/g;" gives o/p as "c:" , though it should stop at c2='...', bcoz c3='..' doesn't end with a comma..
  #5 (permalink)  
Old 01-22-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Quote:
Originally Posted by Niroj View Post
o/p=> c1: c2: c3:
Thanks Kelvin ! But could plz explain How does it work for c3='hg5 sh'.
(c3='.....' does not end with a comma "," !!)?
In the regexp you see ,?. In this context the ? symbol is a quantifier that means zero or one of whatever is to the left of it. So it will match zero or one comma.

Quote:
Originally Posted by Niroj View Post
And my 2nd query is why "$str =~ s/='.*',?/:/g;" gives o/p as "c:" , though it should stop at c2='...', bcoz c3='..' doesn't end with a comma..
In your regexp you have used .* which means to match zero or more of anything as much as possible. This is called greedy matching. So it matches whatever is between the first single quote and the last single quote in the string. What you should have done was used .*? which means to match zero or more of anything but as little as possible. This is called stingy matching. But using the negated character class [^'] is actually more efficient then using .*? to achieve the same result.
  #6 (permalink)  
Old 01-22-2009
Niroj Niroj is offline
Registered User
  
 

Join Date: Aug 2008
Location: Bangalore, INDIA
Posts: 55
Another one of same type...!!

Thanks agn Kalvin! The below is the new one I need to solve. Here the same type of string is present but I want the o/p differently.

my $str = " c1 ='mmdnn, ,sdm = sm,m,jkjk',c2 = 'chj=kjk, khj ', c3='hhshhj, hsjh'";

print "Before substitution-> $str\n";

$str =~ s/[^= *'[^']*'] *, *?/:/g;

print "After substitution-> $str\n"; # Getting Wrong o/p

================================

But I need the o/p:
c1 ='mmdnn, ,sdm = sm,m,jkjk':c2 = 'chj=kjk, khj ':c3='hhshhj, hsjh'




Plz help...

Last edited by Niroj; 01-22-2009 at 02:40 AM..
  #7 (permalink)  
Old 01-22-2009
Niroj Niroj is offline
Registered User
  
 

Join Date: Aug 2008
Location: Bangalore, INDIA
Posts: 55
Quote:
Originally Posted by KevinADC View Post
In the regexp you see ,?. In this context the ? symbol is a quantifier that means zero or one of whatever is to the left of it. So it will match zero or one comma.



In your regexp you have used .* which means to match zero or more of anything as much as possible. This is called greedy matching. So it matches whatever is between the first single quote and the last single quote in the string. What you should have done was used .*? which means to match zero or more of anything but as little as possible. This is called stingy matching. But using the negated character class [^'] is actually more efficient then using .*? to achieve the same result.


Thanks agn Kalvin! The below is the new one I need to solve. Here the same type of string is present but I want the o/p differently.

my $str = " c1 ='mmdnn, ,sdm = sm,m,jkjk',c2 = 'chj=kjk, khj ', c3='hhshhj, hsjh'";

print "Before substitution-> $str\n";

$str =~ s/[^= *'[^']*'] *, *?/:/g;

print "After substitution-> $str\n"; # Getting Wrong o/p

================================

But I need the o/p:
c1 ='mmdnn, ,sdm = sm,m,jkjk':c2 = 'chj=kjk, khj ':c3='hhshhj, hsjh'
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 04:38 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