Visit Our UNIX and Linux User Community


regular expression format string in one line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting regular expression format string in one line.
# 1  
Old 10-30-2009
regular expression format string in one line.

Hi All,

Code:
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
$day=091023;
$day_combine = $day;
$day_combine =~ s/([0-9]{2})([0-9]{2})([0-9]{2})/20$1-$months[$2-1]-$3/;

Instead of three lines, is possible to combine the last two lines into a single line? means no need assign $day to $day_combine first, just one line. Smilie

Thanks

Last edited by jimmy_y; 10-30-2009 at 01:59 PM..
# 2  
Old 11-02-2009
Quote:
Originally Posted by jimmy_y
...
Instead of three lines, is possible to combine the last two lines into a single line? means no need assign $day to $day_combine first, just one line. Smilie
...
Sure it's possible:

Code:
$
$ cat -n tst1.pl
     1  #!/usr/bin/perl
     2  @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
     3  $day="091023";
     4  ($day_combine = $day) =~ s/([0-9]{2})([0-9]{2})([0-9]{2})/20$1-$months[$2-1]-$3/;
     5  print $day_combine,"\n";
     6
$
$ perl tst1.pl
2009-Oct-23
$
$

And, taking it a step further, why not remove the middle-man completely ? Smilie

Code:
$
$ cat -n tst2.pl
     1  #!/usr/bin/perl
     2  @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
     3  $day_combine="091023";
     4  $day_combine =~ s/([0-9]{2})([0-9]{2})([0-9]{2})/20$1-$months[$2-1]-$3/;
     5  print $day_combine,"\n";
     6
$
$ perl tst2.pl
2009-Oct-23
$
$

tyler_durden
# 3  
Old 11-02-2009
Hi tyler,
Thanks, i never thought can do ($day_combine = $day) =~ . Smilie
By the way, i can not remove the middle-man, bec i need $day="091023"; this variable to be there.

Thanks

Previous Thread | Next Thread
Test Your Knowledge in Computers #655
Difficulty: Medium
In the mid-1980s Microsoft developed a multitasking version of DOS.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

2. Shell Programming and Scripting

Regular Expression - Switch Entire String

I would like to be able to use a regular expression to find and replace entire strings, but not replace if the string is a substring in a larger string. Example: $string = "ABC ABCDEF ABC ABCDEF ABC"; Something like - $string =~ s/ABC/XYZ/g; ->Desired: $string = "XYZ ABCDEF XYZ ABCDEF... (3 Replies)
Discussion started by: rjulich
3 Replies

3. Shell Programming and Scripting

String regular expression

Hi, temp="/usr=25,/usr/lib=12" How to get only dir names with out values. I tried like below but no use. tmp=${temp##*,} echo $tmp o/p: /usr/lib=12 expected o/p: /usr /usr/lib ---> in array (13 Replies)
Discussion started by: munna_dude
13 Replies

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

5. Shell Programming and Scripting

How can awk search a string without using regular expression?

Hello, Awk seem treat the pattern as regular expression, how can awk search not using regular expression? e.g. just represent for "", not "A" or "a" . I don't want to add backslash . (2 Replies)
Discussion started by: 915086731
2 Replies

6. Shell Programming and Scripting

Required help in perl regular expression substitution for this date format

Hi, I have written a small perl script to handle particular date format using perl, but it is not substituting the whole string. Can some one please check on what is the issue with the code. $_ = "Date: November 25, 2010 09:02:01 PM";... (1 Reply)
Discussion started by: sarbjit
1 Replies

7. Shell Programming and Scripting

perl regular expression format date

Hi Everyone, Mon 18 Jan 2010 09:52:10 AM MYT the output is 20100118 09:52:10 how to do it in perl regular expression =~ Thanks (3 Replies)
Discussion started by: jimmy_y
3 Replies

8. Shell Programming and Scripting

validate a string against a regular expression

Hi there i have a script which will create unix user accounts. Id like to validate the entered string so that it is specifically 8 characters or less and consists of only ! not Is there a way to validate a string against a regular expression.. i.e size=`printf "$var | wc -m` ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

9. UNIX for Dummies Questions & Answers

Regular Expression - match 'b' that follows 'a' and is at the end of a string

Hi, I'm struggling with a regex that would match a 'b' that follows an 'a' and is at the end of a string of non-white characters. For example: Line 1: aba abab b abb aab bab baa I can find the right strings but I'm lacking knowledge of how to "discard" the bits that precede bs.... (2 Replies)
Discussion started by: machinogodzilla
2 Replies

10. Shell Programming and Scripting

regular expression format

. . . AA = 0.000000, 0.000000, 0.006000, 0.006000, 1, 1.000000, 0.000000 ;item 1 #line 1 BB = -0.002990, -0.002990, 0.002990, 0.002990, 0, 1.000000, 0.000000 ;List 1 #line 2 CC = 0.023620, 0.023620, 0.035430, 0.035430, 1, 1.000000, 0.000000 ;Counter strike ... (1 Reply)
Discussion started by: trynew
1 Replies

Featured Tech Videos