Perl:string substitution Pattern: ='abc...',


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl:string substitution Pattern: ='abc...',
# 1  
Old 01-21-2009
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  
Old 01-21-2009
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  
Old 01-21-2009
Code:
my $str = " c1='fgfasfgasggfgff.,akhk', c2='bbbn', c3='hg5 sh' ";
print "Before substitution-> $str\n";
$str =~ s/='[^']*',?/:/g;
print $str;

# 4  
Old 01-22-2009
Quote:
Originally Posted by KevinADC
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:
Smilie 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  
Old 01-22-2009
Quote:
Originally Posted by Niroj
o/p=> c1: c2: c3:
Smilie 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
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  
Old 01-22-2009
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  
Old 01-22-2009
You can use from tr /.../:/s;
for detail google it;
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Substitution mid pattern?

Hi there, I have a file that goes like this: b_cdbc_db_cd_bcd_aaa-bcd_cd That type of format, for many lines. What I want to do is enter a new line character for after the _ I write an expression to find "_...-" fine, but I don't know how to substitute this to be: "_\naaa-" - where... (1 Reply)
Discussion started by: maximus73
1 Replies

2. Shell Programming and Scripting

PERL : pattern matching a string stored in a variable

I have two variables, my $filename = "abc_yyyy_mm_dd.txt"; my $filename1 = " abc_2011_11_07.txt"; I need to perform some operations after checking if $filename has $filename1 in it i have used the below code, if($filename =~ /^$filename1/) { ---- -- } (2 Replies)
Discussion started by: irudayaraj
2 Replies

3. Shell Programming and Scripting

get value between <abc and > by perl, awk

Hi Everyone, cat 1.txt a <abc b vfff c 000> d 4444 the output is: <abcvfff000> by using perl or awk, can get the value betwee "<abc" and ">", assume 1.txt has lots of those tags, so the output can filter out all those values. Please advice. Thanks (4 Replies)
Discussion started by: jimmy_y
4 Replies

4. Shell Programming and Scripting

How can I use if statement to check a string start with "abc"

I am trying to write a if statement in KSH that if a string is start with abc then print something, I have written some code as below but it doesn't work. Could someone please help me if ] then print success fi (5 Replies)
Discussion started by: yhever
5 Replies

5. Shell Programming and Scripting

pattern match url in string / PERL

Am trying to remove urls from text strings in PERL. I have the following but it does not seem to work: $remarks =~ s/www\.\s+\.com//gi; In English, I want to look for www. then I want to delete the www. and everything after it until I hit a space (but not including the space). It's not... (2 Replies)
Discussion started by: mrealty
2 Replies

6. Shell Programming and Scripting

What is difference between ./abc.sh and . abc.sh

Hi Friends I have one shell script abc.sh If I run it ./abc.sh and . abc.sh , then what is the difference.. Thanks Joy:confused: (1 Reply)
Discussion started by: itsjoy2u
1 Replies

7. Shell Programming and Scripting

string substitution in perl

Hi, I have a template file and want to replace 3 parameters to the values that I want. these values are in a parameter file. Any idea how to do this in perl? the parameter file looks like: host_name = jupiter PORT = 1562 IPADDRESS = 10.1.34.10 the template file has lots of entry.... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

8. Shell Programming and Scripting

String start with ABC

Hi, How to find out the words starting with ABC in a file (K shell) I dont want the word having ABC in middle of any string. Thanks Subrat (1 Reply)
Discussion started by: subrat
1 Replies

9. Shell Programming and Scripting

how to make ABC into "ABC" ina file

suppose u have a file ABC CDF ADF FDG HAA AHH AHA so output shud be like "ABC" "CDF" "ADF" FDG " "HAA" "AHH" "AHA" (8 Replies)
Discussion started by: cdfd123
8 Replies

10. Shell Programming and Scripting

pattern match and substitution, can you help?

pattern match and substitution, can you help? file named test.txt I want to replace all the words Event with the word Fatal in all lines containing the word ERR - but I also want to keep the output of the other lines not matching ERR Test.txt: Event 13 INF egegegege Event 14 INF... (4 Replies)
Discussion started by: frustrated1
4 Replies
Login or Register to Ask a Question