Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is wrong with this lines

Status
Not open for further replies.

vptl

Technical User
Jan 22, 2004
120
0
0
US
my $date=`/bin/date| sed -e s/" "/_/g |sed -e s/:/_/g`;
my $file=q(/tmp/fs_list_$date);

Thanks,
 
Try:

Code:
my $date=`/bin/date| sed -e s/[\s:]/_/g`;
my $file= '/tmp/fs_list_' . $date;


Michael Libeson
 
Or maybe pop an extra "q" in next to the other one to keep it company! ;-)


Trojan.

 
Michael,
Thanks.Yep the "." operater did help.

What I am trying to do is replace all the space and ":" from date cmd o/p with "_" so that I can create a file with that ext.
I tried your sed syntex and it did not getnerate the o/p.
I know I am useing two sed , b/c I do not know how to use "|" with in sed to do something like s/" "|":"/"_"/g
 
What about this:

Code:
my $date = `/bin/date`;
chomp $date;
$date =~ s/[\s:]/_/g;
my $file = "/tmp/fs_list_$date";

No sed calls at all! ;-)


Trojan.
 
Code:
my $date = `/bin/date`
could also be replaced by
Code:
my $date = localtime();
although the default localtime() representation doesn't give the Zone abbreviation - it rather depends upon what your exact need is.

As for the sed, as Trojan points out it's far better done in Perl (it's much quicker than sed and much quicker than piping). However, for education's sake, all the following are exactly equivalent in their output:
Code:
sed -e s/" "/_/g | sed -e s/:/_/g
sed -e s/" "/_/g -e s/:/_/g
sed -e 's/ /_/g' -e s/:/_/g
sed -e 's/ \|:/_/g'
sed -e 's/[ :]/_/g'
The | character in the fourth example needs to be escaped in order to perform its desired function. The last example is the most typically used form, as it's simple, short and doesn't use escape sequences, The shortest usable date/sed sequence I can see would be:
Code:
echo -n '/tmp/fs_list_'; date | sed -e 's/\W/_/g'
The equivalent pure perl solution, if you don't mind the lack of the Zone field, would be:
Code:
my ($date, $file);
($date = localtime()) =~ s/\W/_/g;
$file = "/tmp/fs_list_$date";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top