Mostly Linux & Python syntax notes and hyperlinks.

Monday, June 28, 2010

Linux: Testing for string equality in ksh

In .ksh this is how to do it:
$ plex="SIMPLEX"
$ if [ "$plex" = "SIMPLEX" ]
> then
> echo y
> fi
y
$ if [ "$plex" = "DU" ]
> then
> echo y
> else
> echo n
> fi
n

All of these do not work:
$ plex="SIMPLEX"
$ if (($plex=="SIMPLEX"))
> then
>   echo y
> else
>   echo n
> fi
y
$ if  (($plex=="DUP")) then echo t
> fi
t
$ if (($plex="SIMPLEX"))
> then
>   echo y
> else
>   echo n
> fi
n
$ if [["$plex"=="SIMPLEX"]]
> then
>   echo yes
> fi
ksh: [[SIMPLEX==SIMPLEX]]: not found
$ if [$plex="SIMPLEX"]
> then
>   echo yes
> fi
ksh: [SIMPLEX=SIMPLEX]: not found

Friday, June 25, 2010

Linux/Python: setting shell variable to value returned from python function

From stackoverflow.com.  This works with python 2.4:
$ f=$(python foo.py)
$ echo $f
FOOO
Where foo.py is:
def foo():
  print "FOOO"

if __name__ == '__main__':
   foo()
There was a recommendation to use sys.stdout.write("%s\n", fooPy()) instead of print(), but I got an error about sys.stdout.write() expecting one not two input variables when I tried it. If I use sys.stdout.write("%s\n" % foo()) or print(foo()) in the main clause then the output is:
$ python foo.py
FOOO
None
$ w=$(python foo.py)
$ echo $w
FOOO None
With
import sys
def foo():
  sys.stdout.write("FOOO")
if __name__ == '__main__':
   foo()
I get
$ python foo.py
FOOO$
$ w=$(python foo.py)
$ echo $w
FOOO
$

Thursday, June 24, 2010

Linux: One minus the Line Count

To count the number of output records minus the header in one line of script:
CNT=$(($(wc -l file_with_header | awk '{print $1}')-1))

Tuesday, June 22, 2010

Linux: yesterday's date

This is cool:
$ date
Tue Jun 22 14:11:25 EDT 2010
$ date --date='1 day ago'
Mon Jun 21 14:11:36 EDT 2010

Thursday, June 10, 2010

Linux: remove header and trailer lines from input file

  • Use "sed '$d' infile > outfile" to remove the first line.
  • Use "sed '1d' infile > outfile" to remove the last line.
$ cat hf.dat
head
one
two
three
foot
$ sed '$d' hf.dat > nofoot
$ cat nofoot
head
one
two
three
$ sed '1d' nofoot > noheadfoot
$ cat noheadfoot
one
two
three
$

Tuesday, June 8, 2010

python: take the average of a column of numbers from a file

Could use smoothing out, but easier than getting out the calculator:
def average(infile):
  acount=0
  asum=0
  f=open(infile,"r")
  g=open("avg_"+infile,"w")
  for line in f:
      num=float(line.strip())
      asum= asum + num
      acount=acount+1
      g.write('count=%d,' % acount)
      g.write('num=%f' %  num )
      g.write(',sum=%f \n' % asum)
  if acount > 0:
     average=asum/acount
     g.write('\n sum=%f \n' % asum )
     g.write('average=%f\n' % average)
  f.close();
  g.close();

def main(infile):
    average(infile)

if __name__ == "__main__":
    import sys
    main(*sys.argv[1:])

Note that float() of blank returns zero, so make sure there's no blank line at the end of your input file. Or, improve the script to check for that sort of thing.
>>> float()
0.0
>>> float( )
0.0
>>>