Journal Journal: bash script for ignoring all .*.cmd files
This command finds all directories with
svn status | grep "\.cmd" | cut -b8- | cut -d. -f1 | sort | uniq | xargs svn propset svn:ignore '.*.cmd'
This command finds all directories with
svn status | grep "\.cmd" | cut -b8- | cut -d. -f1 | sort | uniq | xargs svn propset svn:ignore '.*.cmd'
Ok, I reallt needed a bash command that could list the index of every occurance of a pattern in a file. I found this one-liner before:
perl -0777 -ne 'print index $_, "\x5d\x00\x00\x80\x00\x00"' afile.bin
This locates the first LZMA header in a file, but I need to find ALL headers, not just the first... Ok, I'm no perl specialist, but I came up with this:
#!/usr/bin/perl
use strict;
use warnings;
my $count = @ARGV;
if ( $count < 2 ) {
print "bff - Binary Find in Files. Finds all occurances of a string in a file\nusage: bff <string> <filename>\n";
exit();
}
open FILE, $ARGV[1] or die $!;
my $char = eval '"'.$ARGV[0].'"';
my $offset = 0;
my $result = index(<FILE>, $char);
while ($result != -1) {
$offset += $result;
print "Found at $offset\n";
$offset += 1;
seek FILE, $offset, 0;
$result = index(<FILE>, $char);
}
This allows me to write
bff "\x5d\x00\x00\x80\x00\x00" afile.bin
and lists all occurances of the header in the file.
As always, this is a personal reference page, but use it if you like it...
VAR=hello in all examples
String length: ${#VAR}
ex:
echo ${#VAR} #yields '5'
Substring: ${#VAR:[chars to remove from start]:[chars to return]}
Last parameter can be ignored if you want to the end ("${VAR:2}")
ex:
echo ${VAR:1:3} #yields 'ell'
Remove last character: ${VAR%?}
ex:
echo ${VAR%?} #yields 'hell'
Return last character: ${VAR:${#VAR}-1}
Bit messy, best I could find though
ex:
echo ${VAR:${#VAR}-1} #yields 'o'
To be continued...
This is a follow up on the previous post about easter day, the function getEasterDate is required...
This calculates the number of work days between two dates, using Swedish holidays.
This consists of two functions, one that calculates if a given day is a holiday (isHoliday()) and one that simply loops through the range of days given and checks if any is a holyday (numberOfWorkDays())
If anyone wants to convert this to another country or optimize this one, please tell me about it! I'd love to see it...
Create Function isHoliday(@date as datetime)
returns bit
as begin
if DATEPART(dw, @date) in (1,7)
or (DAY(@date)=1 and MONTH(@date) in (1,5)) --Jan 1, May 1
or (DAY(@date)=6 and MONTH(@date)=6) --6 july, national day
or (MONTH(@date)=12 and DAY(@date) in (25,26)) --xmas
return 1
declare @easter as datetime set @easter = dbo.getEasterDate(YEAR(@date))
if DATEDIFF(day, @easter, @date) in (-2, 1, 39) --easter + ascention
return 1
return 0
end
go
Create function numberOfWorkDays( @fromDate as datetime, @toDate as datetime)
returns int
as begin
declare @day as datetime
declare @endday as datetime
declare @dayCount int
set @day = @fromdate
set @endday = @toDate
set @dayCount = 1
if @endday @day begin
declare @tempday datetime set @tempday=@day
set @day=@endday
set @endday = @tempday
end
while @day @endday begin
if dbo.isHoliday(@day)=0
set @dayCount = @dayCount + 1
set @day = dateadd(day, 1, @day)
end
return @dayCount
end
A SQL function to calculate the easter date on any given year:
create function getEasterDate( @yr int )
returns datetime
As Begin
declare @g int, @c int, @x int, @z int, @d int, @e int, @n int
set @g = @yr % 19 + 1
set @c = @yr / 100 + 1
set @x = (3 * @c) / 4 - 12
set @z = (8 * @c + 5) / 25 - 5
set @d = (5 * @yr) / 4 - @x - 10
set @e = (11 * @g + 20 + @z - @x) % 30
if (@e = 24) or ((@e = 25) and (@g > 11)) set @e = @e + 1
set @n = 44 - @e
if (@n 31 begin
set @g = 4
set @n = @n-31
end
return cast( cast(@yr as varchar) + '-' + cast(@g as varchar) + '-' + cast(@n as varchar) as datetime)
End
All seems condemned in the long run to approximate a state akin to Gaussian noise. -- James Martin