![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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.. |
|
||||
|
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... |
|
||||
|
Quote:
(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.. |
|
||||
|
Quote:
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. |
|
||||
|
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.. |
|
||||
|
Quote:
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' |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|