Tuesday, July 8, 2014

Bash script to iterate through RCS versions of a file and output each difference

 1 #!/bin/bash
 2 
 3 file=$1
 4 top_num=10
 5 
 6 
 7 if [[ ! -f ${file} ]]; then
 8    echo "Need valid file to Check RCS Against"
 9    exit
10 fi
11 
12 
13 revisions=( $(rlog ${file} | grep '^revision' | sed
14 's/revision /r/' | cat | awk '{print $1}' | head -n
15 ${top_num}) )
16 
17 echo "number of revisions: " ${#revisions[@]}
18 
19 for (( i = 0; i < ${#revisions[@]}; i++ )); do
20    if [[ ${i} == 0 ]]; then continue; fi
21    echo
22    echo
23    echo
24    echo
25    echo
26    echo "================================================="
27    echo "================================================="
28    echo "${revisions[${i}-1]} -> ${revisions[${i}]}
29    ${file}"
30    echo "================================================="
31    echo "================================================="
32    rcsdiff -zLT -u -${revisions[${i}-1]}
33    -${revisions[${i}]} ${file}
34 done
You can select, copy and paste the code below

Tuesday, April 29, 2014

Bash Script to Convert CSV or TSV to OpenOffice or Excel format file for viewing or emailing

Dependencies:
csvtool (http://forge.ocamlcore.org/projects/csv/)
Libreoffice (https://www.libreoffice.org/)
Unoconv (http://dag.wieers.com/home-made/unoconv/)

Because of work with Data so much, sometimes I need to review table data but the format can be tedious to sort through if the columns aren't fixed width or otherwise aligned.

I created the following script, and dropped it in my bin directory.

Then I modified KDE to have a global shortcut for launch.

Create a link to your shell script by making your own directiry, and pointing it to your new script.



Next, add a global shortcut so it launches automatically.




Note, your can modify your end format by looking at what unoconv offers by executing the command
`unoconv --show`

Source code for script is below.


Cheers!

./Steven



 1 #!/bin/bash
 2 
 3 end_format="xls" #you can get these formats by using the command `unoconv --show`
 4 
 5 pastefile=$(mktemp /tmp/vim.XXXX)
 6 csvfile=${pastefile}.csv
 7 xlsfile=${pastefile}.xls
 8 
 9 function getclip {
10       qdbus org.kde.klipper /klipper org.kde.klipper.klipper.getClipboardContents > ${pastefile}
11 }
12 
13 ##if I'm a file copy it else from clipboard
14 if ! test -z $1; then
15    if test -f $1; then
16       echo File $1 Found
17       cat $1 > ${pastefile}
18    else
19       getclip
20    fi
21 else
22    getclip
23 fi
24 
25 function width {
26    delim=$1
27    width=$(/usr/bin/csvtool -t ${delim} width ${pastefile} 2> /dev/null)
28    if [[ ${width} -gt 1 ]]; then 
29       return 0
30    else 
31       return 1
32    fi
33 }
34 
35 if  width TAB; then
36    sep="TAB"
37    /usr/bin/csvtool -t ${sep} -u COMMA cat ${pastefile} > ${csvfile}
38 elif width COMMA ; then 
39    sep="COMMA"
40    cp ${pastefile} ${csvfile}
41 fi
42 
43 if [ -z ${sep} ]; then
44    rm -f ${pastefile} 
45    echo no separation found  >> ${pastefile}
46    /usr/bin/vim ${pastefile} 
47    rm -f ${pastefile} 
48    exit
49 fi
50 
51 
52 if ! /usr/bin/unoconv -f ${end_format} -o ${xlsfile} ${csvfile} ; then
53    echo "Bad conversion, opening CSV"
54    vim ${csvfile}
55    rm -f ${pastefile} ${csvfile} 
56    exit
57 fi
58 
59 /usr/bin/libreoffice ${xlsfile}
60 rm -f ${pastefile} ${csvfile} ${xlsfile}


You can select, copy and paste the code below



Tuesday, December 31, 2013

SQL Function / Query to take the number of seconds and convert it to a human readable duration

I little MSSQL function I made to give me a table with calculated human readable durations from seconds.

WolframAlpha link to verify durations

 1 use DB
 2 
 3 declare
 4    @s int
 5    ,@secs int
 6    ,@days int
 7    ,@hours int
 8    ,@minutes int
 9    ,@total_hours float
10    ,@total_minutes float
11    ,@txt_long varchar(200)
12    ,@txt_short varchar(200)
13    ,@abbr varchar(200)
14 
15 --- input seconds
16 select @s = 100000
17 
18 select @days    = @s / 86400
19 select @hours   = (@s % 86400) / 3600
20 select @minutes = ((@s % 86400) % 3600) / 60
21 select @secs    = (((@s % 86400) % 3600) % 60) % 60
22 select @total_hours   = CAST(@s as float)  / 3600
23 select @total_minutes = CAST(@s as float)  / 60
24 
25 
26 select @txt_long =
27 CASE WHEN @days > 1 THEN CAST(@days as varchar(20)) + ' days ' ELSE '' END +
28 CASE WHEN @days = 1 THEN CAST(@days as varchar(20)) + ' day ' ELSE '' END +
29 CASE WHEN @hours > 1 THEN CAST(@hours as varchar(20)) + ' hours ' ELSE '' END +
30 CASE WHEN @hours = 1 THEN CAST(@hours as varchar(20)) + ' hour ' ELSE '' END +
31 CASE WHEN @minutes > 1 THEN CAST(@minutes as varchar(20)) + ' minutes ' ELSE '' END +
32 CASE WHEN @minutes = 1 THEN CAST(@minutes as varchar(20)) + ' minute ' ELSE '' END +
33 CASE WHEN @secs > 1 THEN CAST(@secs as varchar(20)) + ' seconds ' ELSE '' END  +
34 CASE WHEN @secs = 1 THEN CAST(@secs as varchar(20)) + ' second ' ELSE '' END 
35 
36 select @txt_short =
37    CAST(@days as varchar(20)) + 'd ' +
38    CAST(@hours as varchar(20)) + 'h ' +
39    CAST(@minutes as varchar(20)) + 'm ' +
40    CAST(@secs as varchar(20)) + 's ' 
41 
42 select @abbr =
43    CAST(@days as varchar(20)) + ':' +
44    CAST(@hours as varchar(20)) + ':' +
45    CAST(@minutes as varchar(20)) + ':' +
46    CAST(@secs as varchar(20)) 
47 
48 select @days,@hours,@minutes,@secs,@total_hours,@total_minutes,@txt_long,@txt_short,@abbr
49 
50 RESULT:
51 "computed","computed1","computed2","computed3","computed4","computed5","computed6","computed7","computed8",
52 "1","3","46","40","27.7777777777777786","1666.66666666666674","1 day 3 hours 46 minutes 40 seconds ","1d 3h 46m 40s ","1:3:46:40",


You can select, copy and paste the code below




Thursday, June 20, 2013

dynamically switch monitor states with nvidia card on laptop (LINUX)



This was driving me nuts so I made a script that changed monitors automatically depending on where I'm plugged in. Previously I setup a hot key combination, but that was hit and miss because of response time on waking up. This could be customized for multiple setups. I couldn't find a good way to compare multi-line strings other than by letting bash sort it out with echo.


 1 #!/bin/bash
 2 
 3 
 4 sleep_time=10
 5 _num=0
 6 
 7 work_setup="
 8 DFP-0 (0x00010000): LGD
 9 DFP-1 (0x00020000): DELL U2211H
10 DFP-2 (0x00040000): DELL U2312HM
11 "
12 
13 standalone="
14 DFP-0 (0x00010000): LGD
15 "
16 
17 function check_state {
18    _num=$((${_num} + 1))
19    _exist=$(echo $1)
20    _current=$(/home/stevenh/bin/nv-control-dpy | grep DFP ) 
21 
22    test -z "${_exist}" && _exist=${_current}
23 
24    case $(echo $_current) in
25     #exists, sleep then repeat
26     $(echo ${_exist}))
27          echo exists ${_exist}
28          sleep ${sleep_time}
29          check_state "${_current}"
30     ;;
31     $(echo ${work_setup}))
32       /home/stevenh/bin/nv-control-dpy --add-metamode "DFP-1: nvidia-auto-select @1920x1080 +1920+0, DFP-2: nvidia-auto-select @1920x1080 +0+0"
33       xrandr -s 3840x1080
34       xrandr --output DP-1 --primary
35       echo swtich to work ${_current} ${_num}
36       sleep ${sleep_time}
37       check_state "${_current}"
38     ;;
39     $(echo ${standalone}))
40       /home/stevenh/bin/nv-control-dpy --add-metamode 1280x800
41       xrandr -s 1280x800
42       echo switch to standalone ${_current} ${_num}
43       sleep ${sleep_time}
44       check_state "${_current}"
45     ;;
46    esac
47 
48    check_state "${_current}"
49 }
50 
51 check_state
You can select, copy and paste the code below

Friday, March 1, 2013

code2html wrapper for previewing different styles

In my effort to continue contributing to the web, I'm documenting coding projects or efforts that I think others may find useful.

At best, I'll teach something to someone they didn't know before. At worst, I'll have a place to document some of my code.

So, a little about this project. I tend to automate things that I use over and over. For example, making dynamic sql queries, or preparing column headers so that I can just paste them into the previous query I was using and do things with them.

Anyhow, this particular script helps me pick out the best formatted code2html entry.

Looks a little like this:

Google Chrome Output of code2html


... except it gives a breakdown of ALL the different code styles.

When I find the one I want, I hit right click in the general area "inspect element" , highligh, copy html, and drop it into blogger.

DONE.









 1 #!/bin/bash
 2 
 3 file=$1
 4 out=$(/bin/mktemp)
 5 browser="/usr/bin/chromium-browser"
 6 
 7 #is it running interactively, or receiving a pipe?
 8 if [ ! -t 0 ]; then
 9    echo "getting STDIN..."
10    file=$(/bin/mktemp)
11    cat - > ${file}
12 else
13    if [[ ! -f ${file} ]] ; then
14       echo "Bailing, invalid INPUT file"
15       rm -f ${out}
16       exit
17    fi
18 fi
19 
20 types=($(code2html -m | grep 'modes:' | sed 's/Defined modes: //;s/\.$//;s/, /\n/g'))
21 html=($(code2html -m | grep 'outputformats:' | sed 's/Defined outputformats: //;s/\.$//;s/, /\n/g'))
22 
23 function print_out {
24    echo "<html>"
25    echo "<body style='background-color:black;color:white;'>"
26    for i in ${types[@]}; do
27       echo -ne "<h1>==============================================<br>\n\n\n\n"
28       echo -ne "${i}\n\n\n\n"
29       echo -ne "<br>==============================================</h1>"
30       echo "<div id='${i}'>"
31       echo "<pre style='background-color:white;color:black;white-space:pre'>"
32       /usr/bin/code2html --replace-tabs 3 --language-mode ${i} --linenumbers  --no-header ${file} 
33       echo "</pre>"
34       echo "You can select, copy and paste the code below"
35       echo "<textarea style='width:100%;height:150px'>"
36       cat ${file}
37       #a little something special in case I run into any code that has the below tag with spaces
38       echo '</ t e x t a r e a >' | sed 's/ //g'
39 
40       echo "<br><br>"
41       echo -ne "\n\n\n\n"
42       echo "</div>"
43    done
44    echo "</body>"
45    echo "</html>"
46 }
47 echo source: ${file}
48 echo outfile: ${out}
49 
50 print_out > ${out}
51 ${browser} ${out} 
You can select, copy and paste the code below

powershell script to write event logs to csv format for parsing

I'm a linux guy, so give me a break if this is bad code :-P This script pull down a list of computers, then goes through the standard event logs and does some specific searches, then dumps the contents into a parsable CSV file.
 1 $before = get-date -month 2 -day 28 -year 2013 -hour 0 -minute 0 -second 0 
 2 $after = get-date -Month 2 -Day 25 -Year 2013 -Hour 0 -Minute 0 -Second 0 
 3 $types = "System","Security","Application" 
 4 $users = '*user*' 
 5 $descs = '*169.254.4.17*'
 6 <# the double backslashes are important for path #> 
 7 $out_dir = "C:\\dest\\directory" 
 8 $computers = Import-Csv 'C:\\Documents\\computers.txt'  
 9 $cmds=@() 
10 $computers | %{ 
11    $pc = $_.COMPUTER 
12    $types | %{ 
13       $type = $_ 
14       $users | %{ 
15          $user = $_ 
16          $fuser = $_ -replace "\*", "" 
17          $file_name =  "$out_dir\\$pc" + "_" + "$fuser" + "_" + "$type" + ".csv" 
18          $cmds += "Get-EventLog -ComputerName $pc -LogName $type -user $user -Before '$before' -After '$after' | Export-Csv '$file_name'"  
19       } 
20       $descs | %{ 
21          $desc = $_ 
22          $fdesc = $desc -replace "\*","" 
23          $file_name =  "$out_dir\\$pc" + "_" + "$fdesc" + "_" + "$type" + ".csv" 
24          $cmds += "Get-EventLog -ComputerName $pc -LogName $type -Before '$before'  -After '$after' -message $desc  | Export-Csv '$file_name'" 
25       } 
26    } 
27 } 
28 
29 $cmds | %{ 
30    $now = get-date 
31    Write-Host "$now ===================== Executing: $_" 
32    iex $_ 
33 }
You can select, copy and paste the code below

Monday, February 4, 2013

bash function to output set bits in a decimal integer

Doing some redesign of our webserver and wrote a quick shell function to give me the bitwise properties of a given integer. enjoy if you have any use for it. ./Stevo
  1 #!/bin/bash
  2
  3 function bitwise {
  4    val=$1
  5    if [[ ${val} -le $((2**8)) ]]; then
  6       max=8
  7    elif [[ ${val} -le $((2**16)) ]]; then
  8       max=16
  9    elif [[ ${val} -le $((2**32)) ]]; then
 10       max=32
 11    elif [[ ${val} -le $((2**64)) ]]; then
 12       max=64
 13    fi
 14    printf "binary: %0${max}dn" $(echo "obase=2; ${val}" | bc -l)
 15    for i in $(seq 1 ${max}); do
 16    if test  $((${val} & $(((1<<${i} - 1))))) -gt 0; then
 17       echo "bit:${i} set"
 18    fi
 19    done
 20 }

output:

stevenh@lnx:~$ bitwise 500
binary: 0000000111110100
bit:3 set
bit:5 set
bit:6 set
bit:7 set
bit:8 set
bit:9 set