Thursday, July 17, 2014

Creating date range for next few hours

We often get cases when we need date ranges to be generated, so it can take care of hours, days, months properly.

Script: file dategen.sh

#!/bin/bash _g_DT="$1" _g_TM="$2" _DT=`date -d "$_g_DT $_g_TM" +"%Y%m%d %H"` echo "For: $_DT" for i in `seq 0 5`; do _DT_gen=`date -d "$_DT +$i hours" +"%Y-%m-%dT%H:00"` echo -e "\t$_DT_gen" done

E.g with Sep 30, at 23 hours
bash dategen.sh "2014-09-30" "23"


For: 20140930 23
 2014-09-30T23:00
 2014-10-01T00:00
 2014-10-01T01:00
 2014-10-01T02:00
 2014-10-01T03:00
 2014-10-01T04:00

Monday, January 21, 2013

check how old is the file

Hi,

we often need to check, how old is the file from the current date

$ export  FILE="the-big-file.log" ; echo `date +"%s"` - `stat -c %Z $FILE`|bc


this command gives us the difference in secs; how old is the file (the-big-file.log)



Friday, November 4, 2011

Getting the odd lines

Hi, if you have to display only the odd lines from the file we can try
let file
$ cat myfile.txt
1
2
3
4
5
6
7
8
9

$ sed -ne '1~2p' myfile.txt
1
3
5
7
9

$ awk '{if (NR%2==1) {print;} }' myfile.txt
1
3
5
7
9

Friday, October 14, 2011

Scatter Gather on linux

Hi,

I have been doing this many times but never blogged, its the scatter gather use to be on dbase days;

This program:
$ cat hello.sh
source e/big.conf

set `date` && export WeekDay=$1 Month=$2 Day=$3 Time=$4 Year=$6

echo Hello $NAME
echo $NO
echo Time: $Time
echo Date: $WeekDay $Month $Day $Year



$ sh hello.sh
Hello SmaLl
2000
Time: 11:24:29
Date: Fri Oct 14 2011


In hello.sh, i am calling the name/value pairs file,
$ cat e/big.conf
NAME=SmaLl
NO=2000

Exporting the date o/p to the positional parameters, hey did it in college days.
set `date` && export WeekDay=$1 Month=$2 Day=$3 Time=$4 Year=$6

Here we can have large name value pairs from static file or runtime command and play with it.

Wednesday, August 24, 2011

more on date and old date

Hi,

I had to work with epoch dates, that was older than the current, struggled a bit.
check this,

#!/bin/bash
dt1=`date`
dt2=`date -d "$dt 1 hour ago"`

s1=`date -d "$dt1" +"%s"`
s2=`date -d "$dt2" +"%s"`

echo Now - $dt1
echo
echo "1 Hour Before   -- $dt2 : $s2"
echo "Now             -- $dt1 : $s1"


put whatever for 1 hour ago as (ago for old, just 1 hour for future, you can have day, minutes)

change dt1 for any date, here its current can be date by -d option


PS:
check good examples in the info pages, i checked this late.. :(
$info date examples

Tuesday, May 10, 2011

reading from file or stream

check  Occurrences of words in file

What if you had to use if frequently.. simple put it in shell program,
and allow it to either read the given file or from the stream, like wc does.

man bash|col -b > bash.txt

$ wc  bash.txt
  4910  37273 260193 bash.txt

$ cat bash.txt |wc
   4910   37273  260193

the below program does it 'occurrences'

                                                                                                                                                     
#!/bin/bash                                                                                                                                     
#program: occurrences
file=${1:--}                                                                                                                                  
cat $file | sed -e 's/[^a-zA-Z0-9.]/ /g' | xargs | tr ' ' '\n' | sort |  uniq -c | sort -nr -k1,2          
                                                                                                                                                      




$ cat bash.txt | occurrences

   2969 the
   1501 is
    948 to
    944 of
    868 a
    620 and
..
..

converting man to text

This will give the text output for 'wc' man page
 
$ man wc |col -b

this man page may not be that big, try 'bash'

$ man bash|col -b

redirect it to a file, dont ask me how.. this is quite helpful, when you open the text page in you favourite editor and search and read the options on 'bash'

its quite big and does lots of things you tried using other commands.