Escaping special character stored in variables : perl


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Escaping special character stored in variables : perl
# 1  
Old 02-13-2009
Escaping special character stored in variables : perl

Hi

just for regular use i m working on small module written in perl for getting date in specified format like i have to specify date format and then seperator to seperate date i am 95% done. now i m sure explanation i gave is not good enough so i am putting output here :
Code:
C:\Documents and Settings\idc193577\Desktop>perl d.pl

    Usage : d.pl <date_format> ['<seperator>']

    Desc  :
    --> valid date_formats - dd   # day of month ( 01 - 31 )
                           - ddd  # 3 letter day name ( Mon Tue Wed ...)
                           - DD   # full day name ( Monday )
                           - DDD  # day of month and name in full ( 13 Friday )
                           - mm   # month no ( 01 - 12 )
                           - mmm  # 3 letter month name ( May Jun Jul ...)
                           - MM   # full month name ( February )
                           - yy   # last 2 digits of year ( 2009 -> 09 )
                           - yyyy # 4 digit year ( 2009 )

    --> valid seperator    - All Non Digits & Characters are valid.

    *** P.S : Specify seperator within single quotes like ','

C:\Documents and Settings\idc193577\Desktop>perl d.pl ddmmyy
Input Format : ddmmyy | Seperator : [] | Output : [130209]

C:\Documents and Settings\idc193577\Desktop>perl d.pl ddd 
Input Format : ddd | Seperator : [] | Output :[Fri]                                                                
C:\Documents and Settings\idc193577\Desktop>perl d.pl mmm 
Input Format : mmm | Seperator : [] | Output [Feb]                                                                
C:\Documents and Settings\idc193577\Desktop>perl d.pl MM  
Input Format : MM | Seperator : [] | Output : [February]  

C:\Documents and Settings\idc193577\Desktop>perl d.pl yyyymmdd "/"
Input Format : yyyymmdd | Seperator : [/] | Output : [2009/02/13/]

now i can put anything as a seperator other than WORDS & NUMBERS this is where problem is coming look in above output last "/" in red is not required so i want to remove this and in code i am doing this as :
Code:
my $seperator = "$ARGV[1]" # which is "/"
my $result      = "2009/02/13/" ; # hardcoded here 

$result =~ s/${$seperator}$// ; # delete seperator which is at last but its not working i even tries escaping with \e${seperator} but did not work. 
print $result ;

thanks
# 2  
Old 02-13-2009
keep it simple....

why not just not use substitution?

Code:
print "${separator}$MM${separator}$MM${separator}$YYYY\n";

# 3  
Old 02-13-2009
Quote:
Originally Posted by quirkasaurus
keep it simple....

why not just not use substitution?

Code:
print "${separator}$MM${separator}$MM${separator}$YYYY\n";

thanks for reply problem is i can try any combination of date format it can be yyyymmdd/dd/mmdd/ddmmyy anything ...so i am not sure which one is coming first ....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl split string separated by special character

Hello I have string (string can have more sections) LINE="AA;BB;CC;DD;EE"I would like to assigne each part of string separated by ";" to some new variable. Can someone help? (4 Replies)
Discussion started by: vikus
4 Replies

2. Shell Programming and Scripting

Grep correct pattern with special character and variables

cat file time="north_south_east_west_08:00" location="A" start="left" status="ok" end="north" time="north_south_east_west_12:00" location="C" start="right" status="ok" end="south" time="north_south_east_west_23:00" location="G" start="left" status="ok" end="east"... (7 Replies)
Discussion started by: ctphua
7 Replies

3. Shell Programming and Scripting

Escaping special characters

I'm attempting a little hack to get grep to highlight (change foreground color to red) a found string. Assuming a target file "test" consisting of the word "albert": My executable "algrep" consists of this: grep $1 $2 | sed "s/$1/\\\033 And when run: algrep al test Produces this:... (2 Replies)
Discussion started by: tiggyboo
2 Replies

4. Shell Programming and Scripting

Escaping Special characters

I want to append the following line to /var/spool/cron/root: */7 * * * * /root/'Linux CPU (EDF).sh' > /dev/null 2>&1 How to accomplish this using echo? ---------- Post updated at 04:09 PM ---------- Previous update was at 04:07 PM ---------- "Linux CPU (EDF)" is actually stored in a... (11 Replies)
Discussion started by: proactiveaditya
11 Replies

5. Shell Programming and Scripting

Unix Perl split special character $

All I'm trying to split a string at the $ into arrays @data:=<dataFile> a $3.33 b $4.44 dfg $0.56 The split command I have been playing with is: split(/\$/, @data) which results with a .33 b .44 dfg .56 any help with this is appreciated /r Rick (9 Replies)
Discussion started by: schultz2146
9 Replies

6. Shell Programming and Scripting

Escaping Special Characters-Help

Hi All, I am having a trouble in passing special characters to a script. As I am new to bash script I dont know how to go and solve this. mypwd=(a+sdfg!h# if i pass $mypwd to a bash script, it is not accepting "(,!,+ etc". It would be a great help if some one can help to escape these... (3 Replies)
Discussion started by: Tuxidow
3 Replies

7. Shell Programming and Scripting

Escaping embedded variables

I'm running into a problem with a differential backup script written in GNU Bash 3.0 - the following stripped down code demonstrates the problem quite nicely. $ DATE="last tuesday" $ date --date="$DATE" Tue Jan 6 00:00:00 PST 2009 So far so good. $ CMD="date --date=\"$DATE\"" $... (6 Replies)
Discussion started by: vertigo23
6 Replies

8. Shell Programming and Scripting

Perl Script Syntax to Extract Everything After Special Character

Hi, I am writing a Perl script that reads in many lines, if a line meets the criteria I want to edit, it. For example, the script will return the following example line... test=abc123 All I want to do is strip off the "test=" and just be left with the abc123. In my script I can easily... (3 Replies)
Discussion started by: edrichard
3 Replies

9. Shell Programming and Scripting

Escaping the * character in ksh.

Hi All, In ksh script i'm trying to assign "sqlstmt1" varaible value, update VAREntryTb set VAR10num = VAR1num * Mltplr where BusD = '$val1' and RunI = 1"` Hence i wrote below statement, the issue with this is shell is expanding "*" character adn thus subistuting it with the content of my... (6 Replies)
Discussion started by: arvindcgi
6 Replies

10. Shell Programming and Scripting

escaping special characters in file name...

dear, I would like to rename files in a dir to another format, so I write a bash shell script to handle it. But my problem now is how to handle files having special characters like spaces, (, ): "a b c (d).doc" It seems that I need to escape those characters before applying the "mv" command.... (1 Reply)
Discussion started by: lau0001
1 Replies
Login or Register to Ask a Question