OF=myhome_directory_$(date +%Y%m%d).tar.gz
tar -czf $OF /home/linuxconfig
Apr 12, 2010
Backing up folder in Bash
#!/bin/bash
Dec 16, 2009
Finding Largest and Smallest value from a set
echo "enter 5 nos"
read a b c d e
echo "biggest: " $(echo "$a $b $c $d $e" | tr " " "\n" | sort -rn | head -1)
echo "smallest: "$(echo "$a $b $c $d $e" | tr " " "\n" | sort -n | head -1)
Summing intergers in a file
Method : 1
awk '{s+=$0}END{print s}' file
Method : 2
echo $(tr "\n" "+" < file)0|bc
awk '{s+=$0}END{print s}' file
Method : 2
echo $(tr "\n" "+" < file)0|bc
Dec 3, 2009
Using xargs for rm & cp
Remove all files with .bak extension -
find . -name "*.bak" -type f -print | xargs /bin/rm -f
Remove all files with .bak extension who were last modified before today -
find . -mtime +0 -name "*.bak" -print | xargs rm
Copy all .html file to /tmp/movedhtml -
find . -name "*.html" -exec cp -p "{}" /tmp/movedhtml \;
Nov 25, 2009
Getting Date - Adding and Subtracting
use DateTime;
my $dt1 = DateTime-> new (
year =>'1998',
month =>'4',
day =>'4',
hour =>'21',
time_zone =>'local'
);
my $Tomorrow = $dt1->add( days => 1 )
my $Yesterday = $dt1->subtract( days => 1 );
* You can also change date time by hour or seconds.
Labels:
Perl
Nov 12, 2009
Kill all same name spawn process in one go
Syntax :
ps -aef | grep -v grep | grep PROC_NAME | awk {'print $2'} | xargs kill -9
E.g :
ps -aef | grep -v grep | grep test.pl | awk {'print $2'} | xargs kill -9
JOIN FILES
SYNTAX :
join -t'DELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE
join -t'\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE
join -t'\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE
join -t'\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE
join -t$'\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE
join -t$'\t' -1 1 -2 2 a.txt b.txt > a_b.out
join -t'DELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE
join -t'\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE
join -t'\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE
join -t'\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE
join -t$'\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE
join -t$'\t' -1 1 -2 2 a.txt b.txt > a_b.out
Aug 11, 2009
Bach shell SPLIT
x='A:B:C';
echo ${x%:*};
output - A:B
echo ${x#*:};
output - B:C
echo $x;
A:B:C
x=${x% *};
echo ${x% *};
output - A
Jun 12, 2009
Calculate the time difference (inline)
Usage: $timeDiffStr = &timeDiff( date1 => '2004-04-28 15:36', date2 => $iso_now );
my ($y, $m, $d, $hh, $mm, $ss) = (localtime)[5,4,3,2,1,0];
$y += 1900; $m++;
my $iso_now = sprintf("%d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $hh, $mm, $ss);
my $timeDiffStr = &timeDiff( date1 => $sale_time, date2 => $iso_now );
sub timeDiff (%) {
my %args = @_;
my @offset_days = qw(0 31 59 90 120 151 181 212 243 273 304 334);
my $year1 = substr($args{'date1'}, 0, 4);
my $month1 = substr($args{'date1'}, 5, 2);
my $day1 = substr($args{'date1'}, 8, 2);
my $hh1 = substr($args{'date1'},11, 2) || 0;
my $mm1 = substr($args{'date1'},14, 2) || 0;
my $ss1 = substr($args{'date1'},17, 2) if (length($args{'date1'}) > 16);
$ss1 ||= 0;
my $year2 = substr($args{'date2'}, 0, 4);
my $month2 = substr($args{'date2'}, 5, 2);
my $day2 = substr($args{'date2'}, 8, 2);
my $hh2 = substr($args{'date2'},11, 2) || 0;
my $mm2 = substr($args{'date2'},14, 2) || 0;
my $ss2 = substr($args{'date2'},17, 2) if (length($args{'date2'}) > 16);
$ss2 ||= 0;
my $total_days1 = $offset_days[$month1 - 1] + $day1 + 365 * $year1;
my $total_days2 = $offset_days[$month2 - 1] + $day2 + 365 * $year2;
my $days_diff = $total_days2 - $total_days1;
my $seconds1 = $total_days1 * 86400 + $hh1 * 3600 + $mm1 * 60 + $ss1;
my $seconds2 = $total_days2 * 86400 + $hh2 * 3600 + $mm2 * 60 + $ss2;
my $ssDiff = $seconds2 - $seconds1;
my $dd = int($ssDiff / 86400);
my $hh = int($ssDiff / 3600) - $dd * 24;
my $mm = int($ssDiff / 60) - $dd * 1440 - $hh * 60;
my $ss = int($ssDiff / 1) - $dd * 86400 - $hh * 3600 - $mm * 60;
"$dd Tage $hh Std. $mm Min.";
}
my ($y, $m, $d, $hh, $mm, $ss) = (localtime)[5,4,3,2,1,0];
$y += 1900; $m++;
my $iso_now = sprintf("%d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $hh, $mm, $ss);
my $timeDiffStr = &timeDiff( date1 => $sale_time, date2 => $iso_now );
sub timeDiff (%) {
my %args = @_;
my @offset_days = qw(0 31 59 90 120 151 181 212 243 273 304 334);
my $year1 = substr($args{'date1'}, 0, 4);
my $month1 = substr($args{'date1'}, 5, 2);
my $day1 = substr($args{'date1'}, 8, 2);
my $hh1 = substr($args{'date1'},11, 2) || 0;
my $mm1 = substr($args{'date1'},14, 2) || 0;
my $ss1 = substr($args{'date1'},17, 2) if (length($args{'date1'}) > 16);
$ss1 ||= 0;
my $year2 = substr($args{'date2'}, 0, 4);
my $month2 = substr($args{'date2'}, 5, 2);
my $day2 = substr($args{'date2'}, 8, 2);
my $hh2 = substr($args{'date2'},11, 2) || 0;
my $mm2 = substr($args{'date2'},14, 2) || 0;
my $ss2 = substr($args{'date2'},17, 2) if (length($args{'date2'}) > 16);
$ss2 ||= 0;
my $total_days1 = $offset_days[$month1 - 1] + $day1 + 365 * $year1;
my $total_days2 = $offset_days[$month2 - 1] + $day2 + 365 * $year2;
my $days_diff = $total_days2 - $total_days1;
my $seconds1 = $total_days1 * 86400 + $hh1 * 3600 + $mm1 * 60 + $ss1;
my $seconds2 = $total_days2 * 86400 + $hh2 * 3600 + $mm2 * 60 + $ss2;
my $ssDiff = $seconds2 - $seconds1;
my $dd = int($ssDiff / 86400);
my $hh = int($ssDiff / 3600) - $dd * 24;
my $mm = int($ssDiff / 60) - $dd * 1440 - $hh * 60;
my $ss = int($ssDiff / 1) - $dd * 86400 - $hh * 3600 - $mm * 60;
"$dd Tage $hh Std. $mm Min.";
}
Labels:
Perl
Subscribe to:
Posts (Atom)
