Thursday, March 5, 2015

script for reading file or stream

I often wanted to pass the file to a pipeline some command, and also if the file is parameter to command it should accept


Voila!!

here is 'stream.sh'

#!/bin/bash

file=${1:--}

cat $file 


Usage and output

$ ./stream.sh underSTANDING.txt 

If you understand, say "understand".
If you don't understand, say "don't understand".
But if you understand and say "don't understand".
How do I understand that you understand? Understand!



$ cat underSTANDING.txt | ./stream.sh 

If you understand, say "understand".
If you don't understand, say "don't understand".
But if you understand and say "don't understand".
How do I understand that you understand? Understand!



$ ./stream.sh < underSTANDING.txt 

If you understand, say "understand".
If you don't understand, say "don't understand".
But if you understand and say "don't understand".
How do I understand that you understand? Understand!

bytes to gb/tb conversion

Often had to go to websites to do the bytes to KB.. TB.. GB conversions
here is the quick script for handy use

#!/bin/bash

# Give numeric value, which is in bytes
# will show all possible conversions 

call_bc() {
n1=$1
n2=$2
echo "scale=4; $n1/($n2)" |bc
}

nm=$1
pw=$2
pn=""

k_ilo=1024;
m_ega=$k_ilo*$k_ilo;
g_iga=$m_ega*$k_ilo;
t_era=$g_iga*$k_ilo;
p_eta=$t_era*$k_ilo;


[ -z "$nm" ] && exit

echo  "B: "$nm
for i in K M G T P; do
case $i in
K)pn=$k_ilo;;
M) let pn=$m_ega;;
G) let pn=$g_iga;;
T) let pn=$t_era;;
P) let pn=$p_eta;;
esac
echo -n "$i: "; call_bc $nm $pn
done

Usage and output
$ ./convert.sh 74274576
B: 74274576
K: 72533.7656
M: 70.8337
G: .0691
T: 0
P: 0

 

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