Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Sunday, May 22, 2011

Suming a list of numbers of the shell

Manipulating lists on the shell can be done with awk. The most common one I encounter is summing numbers. Here is a quick bit of shell magic to sum up the size of all log files below a certian dictionary. Awk has much more power than this however.

find . -type f | grep "\.log" | xargs ls -al | sed "s/  */ /g" | cut -d " " -f 5 | awk -F: '{total+=$1} END{print total}'

Friday, May 6, 2011

Shell repeat/do/while for a count

Gahh.. simple thing but annoying its for bash shell

 i=245; while [ $i -ne 33 ]; do  echo $i; i=`expr $i - 1`; done

Wednesday, April 27, 2011

Suming a list of numbers

cat numbers.txt | awk -F: '{total+=$1} END{print total}'

Thursday, September 16, 2010

sh simple counted command line loops

For repeated predictable execution of times

i=64; while [ $i -ne 0 ]; do  echo $i; i=`expr $i - 1`; done

Portable script shebang

Let say you need a script to execute on multiple systems but the admins and OS's all have there own idea on where things belong. Here is a quick way to get the script to locate the interpreter 99% of the time

#!/usr/bin/env perl

Wednesday, September 1, 2010

count the occurace of words in a file

cat filename | tr '[:punct:]' ' ' | tr ' ' '\012' | sort | uniq -c | sort -n