# Optimize shell code

**URL:** <https://community.unix.com/t/optimize-shell-code/268918>\
**Category:** Shell Programming and Scripting\
**Created:** [July 6, 2010, 3:26am UTC](https://community.unix.com/t/optimize-shell-code/268918 "2010-07-06T03:26:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandy1028](https://community.unix.com/letter_avatar/sandy1028/32/6_449c533f586939b6e5f246632a46421f.png) [@sandy1028](https://community.unix.com/u/sandy1028)\
**Post date:** [July 6, 2010, 3:26am UTC](https://community.unix.com/t/optimize-shell-code/268918/1 "2010-07-06T03:26:02Z")

</div>

```nohighlight
#!/usr/bin/perl
use strict;
use warnings;

use Date::Manip;
my $date_converted = UnixDate(ParseDate("3 days ago"),"%e/%h/%Y");
open FILE,">$ARGV[0]";

while(<DATA>){

my @tab_delimited_array = split(/\t/,$_);
  $tab_delimited_array[3] =~ s/^\[//;
  $tab_delimited_array[3] =~ s/^\-//;
        my $converted_date = Date_ConvTZ( UnixDate($tab_delimited_array[3],"%Y%m%d%H:%M:%S"),'GMT','PST');
        my $pst_converted_date = UnixDate($converted_date,"%e/%h/%Y:%H:%M:%S");
        $pst_converted_date =~ s/^\s//g;
        my $extracted_YMD=UnixDate($converted_date,"%e/%h/%Y");
if($extracted_YMD =~ m/$date_converted/){
print FILE $_;
}
}

close FILE;
close DATA;

__DATA__
66.249.65.43 www.cabc.com - 3/Jul/2010:19:00:01 +0000 "GET http://www.abc.com HTTP/1.1" 200 0.730 7362 0 0 - - Mozilla/5.0+(compatible;+Googl/2.1;+\+http://www.google.com/bot.html) TCP_MISS
66.249.65.43 www.cabc.com - 4/Jul/2010:19:00:01 +0000 "GET http://www.abc.com HTTP/1.1" 200 0.730 7362 0 0 - - Mozilla/5.0+(compatible;+Googl/2.1;+\+http://www.google.com/bot.html) TCP_MISS
66.249.65.43 www.cabc.com - 2/Jul/2010:09:00:01 +0000 "GET http://www.abc.com HTTP/1.1" 200 0.730 7362 0 0 - - Mozilla/5.0+(compatible;+Googl/2.1;+\+http://www.google.com/bot.html) TCP_MISS
66.249.65.43 www.cabc.com - 3/Jul/2010:09:00:01 +0000 "GET http://www.abc.com HTTP/1.1" 200 0.730 7362 0 0 - - Mozilla/5.0+(compatible;+Googl/2.1;+\+http://www.google.com/bot.html) TCP_MISS

```

---------- Post updated at 02:26 AM ---------- Previous update was at 02:25 AM ----------

The file is very large and it takes long time to execute.  
The input is tab delimited file

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/6_449c533f586939b6e5f246632a46421f.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [July 6, 2010, 8:25am UTC](https://community.unix.com/t/optimize-shell-code/268918/2 "2010-07-06T08:25:52Z")

</div>

I guess you need find out the old logs which convert the date from GMT to PST first, and 3 days ago.

Use GNU date, you can get it in one line command.

```nohighlight
date -d "-3 days -8 hours" +%e/%h/%Y |xargs -i grep {} urfile

```

---

<div class="post-metadata">

**Author:** ![sandy1028](https://community.unix.com/letter_avatar/sandy1028/32/6_449c533f586939b6e5f246632a46421f.png) [@sandy1028](https://community.unix.com/u/sandy1028)\
**Post date:** [July 7, 2010, 6:02am UTC](https://community.unix.com/t/optimize-shell-code/268918/3 "2010-07-07T06:02:36Z")

</div>

Our machine will not support GNU date ☹

---------- Post updated 07-07-10 at 05:02 AM ---------- Previous update was 07-06-10 at 10:30 AM ----------

Other than GNU date, how can I optimise the code
