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
>>>

Tuesday, May 25, 2010

Linux: ^s freeze ^q unfreeze

If you accidentally hit Control-S and freeze your display, hit Control-Q to un-freeze it.

Friday, April 30, 2010

Linux: To see tab and newline characters in vi

From http://www.washington.edu/computing/unix/viqr.html
:set list
To stop seeing them:
:set nolist
And if you need to add in a tab, type control-v first and then the tab.

(To grep for tab)

Thursday, April 8, 2010

Linux: ksh condition syntax, e.g. checking result of line count

ksh scripting can be simple to read, but hard to remember the syntax to write it:
# Number of lines in file
    LC=$(wc -l  $DIRNAME/$FILENAME | awk '{print $1}')
    if (("$LC" > 0))
    then
       nonempty file steps
    else
       empty file steps
    fi
  • The value looked at has to be in quotes.
  • You need to start the "then" on a new line, or have a semicolon before it.
    if [ ... ]; then
    
  • That's two round parentheses surrounding the condition, because it's numbers not strings comparison.  
  • Square brackets are for strings, checking file existence, or value returned from last function
if [[ -f "$DIRNAME/${FILENAME}.txt" ]]
then
...
OK, so I'm finding references to
if (test -d $1)
being the same as
if [-d $1]
and to the use of "-eq" for comparing integers, as in
if [$# -eq 3]
And if you use double square brackets, you avoid triggering an error if your variable is unset.

Good enough for now...

Tuesday, March 30, 2010

python: converting from written out date to numeric

Use the chart at the bottom of http://docs.python.org/library/datetime.html for the %B, %d, %m etc.
>>> import time
>>> f=time.strptime("November 29, 2010", "%B %d, %Y")
>>> f[0]
2010
>>> f[1]
11
>>> f[2]
29
>>> g=time.strftime("%m/%d/%y",f)
>>> g
'11/29/10'
>>> h=time.strftime("%m/%d/%y",time.strptime("October 4, 2012", "%B %d, %Y"))
>>> h
'10/04/12'
>>> time.strftime("%m/%d/%y",time.strptime("May 3, 2011", "%B %d, %Y"))
'05/03/11'
>>>time.strftime("%B %d, %Y",time.strptime("23-Jun-14","%d-%b-%y"))
'June 23, 2014'