perl : regular expression to replace the strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl : regular expression to replace the strings
# 1  
Old 09-18-2012
perl : regular expression to replace the strings

There are 2 strings as below.

Code:
$str1 = "41148,,,,,,,,,,,,,,,,,,,,,,,,";
$date = "TUE 08-28-2012";

The output should be as below
Code:
 
$str1 = "TUE 08-28-2012,,,,,,,,,,,,,,,,,,,,,,,,";

Could anyone please help with the perl regular expression or any other alternative?
# 2  
Old 09-18-2012
One way:
Code:
#!/usr/bin/perl
use warnings;
use strict;

my $str1 = "41148,,,,,,,,,,,,,,,,,,,,,,,,";
my $date = "TUE 08-28-2012";

print "Before : $str1\n";
my @temp = split /,/, $str1, -1;
shift @temp;
$str1 = join ",", $date, @temp;
print "After  : $str1\n";

producing
Code:
Before : 41148,,,,,,,,,,,,,,,,,,,,,,,,
After  : TUE 08-28-2012,,,,,,,,,,,,,,,,,,,,,,,,

--------

And with a regexp:
Code:
#!/usr/bin/perl
use warnings;
use strict;

my $str1 = "41148,,,,,,,,,,,,,,,,,,,,,,,,";
my $date = "TUE 08-28-2012";

print "Before : $str1\n";
$str1 =~ s/\A.*?,/$date,/;
print "After  : $str1\n";


Last edited by elixir_sinari; 09-18-2012 at 07:52 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 09-18-2012
Many thanks... It helped me a lot... Thanks once again...

Also one more doubt..
I had a text file which contains the data as below.
Now I need to check only the lines that start with a digit.

Code:
xyz.net-Multilink1,BW: 1.07 M,,,,,,,,,,,,,,,,,,,,,,,
Hrly Avg (IN / OUT),0:00,1:00,2:00,3:0041148,,,,,,,,,,,,,,,,,,,,,,,,
41149,,,,,,,,,,,,,,,,,,,,,,,,
41150,,,,,,,,,,,,,,,,,,,,,,,,
41151,,,,,,,,,,,,,,,,,,,,,,,,
41152,,,,,,,,,,,,,,,,,,,,,,,,

In the above, line 1 and 2 can be ignored as it starts with characters and
line 3,4 and line 5 has to be processed.

Could you please help me in this regard?

Thanks in advance..

Regards,
GS
# 4  
Old 09-18-2012
Code:
$ cat testfile
xyz.net-Multilink1,BW: 1.07 M,,,,,,,,,,,,,,,,,,,,,,,
Hrly Avg (IN / OUT),0:00,1:00,2:00,3:0041148,,,,,,,,,,,,,,,,,,,,,,,,
41149,,,,,,,,,,,,,,,,,,,,,,,,
41150,,,,,,,,,,,,,,,,,,,,,,,,
41151,,,,,,,,,,,,,,,,,,,,,,,,
41152,,,,,,,,,,,,,,,,,,,,,,,,

$ perl -wne 'print if /\A[0-9]+/' testfile
41149,,,,,,,,,,,,,,,,,,,,,,,,
41150,,,,,,,,,,,,,,,,,,,,,,,,
41151,,,,,,,,,,,,,,,,,,,,,,,,
41152,,,,,,,,,,,,,,,,,,,,,,,,

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl regular expression

Hi , I have the below array my @actionText = ("delivered to governor on 21/23/3345" , "deliver jllj" , "ram 2345/43"); When i am trying to grep the contents of array and if mathced substituting with the digitis or some date format from the element like below my @action = grep { $_ =~... (7 Replies)
Discussion started by: ragilla
7 Replies

2. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

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

4. Shell Programming and Scripting

Perl regular expression and %

Could you help me with this please. This regular expression seems to match for the wrong input #!/usr/bin/perl my $inputtext = "W1a$%XXX"; if($inputtext =~ m/+X+/) { print "matches\n"; } The problem seems to be %. if inputtext is W1a$XXX, the regex doesnot match.... (5 Replies)
Discussion started by: suppandi7
5 Replies

5. Shell Programming and Scripting

perl regular expression

Dear all, I have a simple issue on a perl regular expression. I want to get the characters in red from the next lines : POWER_key LEFT_key RIGHT_key OK_key DOWN_key and so on... Thanks in advance for reply. Ludo (1 Reply)
Discussion started by: lsaas
1 Replies

6. Shell Programming and Scripting

PERL regular expression

Hello all, I need to match the red expressions in the following lines : MACRO_P+P-_scrambledServices_REM_PRC30.xml MACRO_P+P-_scrambledServices_REM_RS636.xml MACRO_P+P-_scrambledServices_REM_RS535.xml and so on... Can anyone give me a PERL regular expression to match those characters ? ... (5 Replies)
Discussion started by: lsaas
5 Replies

7. UNIX for Dummies Questions & Answers

replace words in sed using regular expression

hi, I need to replace all these lines from my text file 123end 234end 324end 234end 989end 258end 924end At the moment I know how to replace "end". But I want to replace the numbers before end as well. How can I do this ? sed s/end/newWord/ myfile.txt newFile.txt thanks (3 Replies)
Discussion started by: aneuryzma
3 Replies

8. Shell Programming and Scripting

regular expression in perl

hi, i want to extract the sessionID from this line. QnA Session Id : here the output should be-- QnA_SessionID=128589 Thanks NT (3 Replies)
Discussion started by: namishtiwari
3 Replies

9. UNIX for Dummies Questions & Answers

Regular Expression - Replace if x contains y except if...

Hi all, I have a file which contains 1000s of lines of text. I need to delete all lines with the words "Red" EXCEPT if the line also contains the word "GREEN"... For example: ThisIs some random text that should be red deleted ThisIs some random text that should NOT be red deleted green ... (4 Replies)
Discussion started by: not4google
4 Replies

10. Shell Programming and Scripting

perl regular expression

letz say that my file has 7 records with only one field. So my file has: 11111111 000000000000000 1111 aaaabbbccc 1111111222000000 aaaaaaaa zz All i need is: 1. when the field has a repetition of the same instance(a-z or 0-9), i would consideer it to be invalid.... (1 Reply)
Discussion started by: helengoldman
1 Replies
Login or Register to Ask a Question