Mostly Linux & Python syntax notes and hyperlinks.

Friday, May 27, 2011

Linux: grep for $

This was harder than grep for tab.
I found instructions to escape the special character, but none of these
grep $ *
grep \$ *
grep "$" *
grep "\$" *
grep - $ *
grep "$" *
grep '$' *
egrep '$' *


worked until I read "Note that a $ sign loses its meaning if characters follow it" and remembered I was looking for a $ followed by a capital letter. So this works:

grep '$[A-Z]' *

but I still don't know how to find a $ by itself.

Wednesday, May 25, 2011

Python: os.path.lexists() vs os.path.exists() before os.symlink()

We were having intermittent failures with shutil.copy(src,dest) and decided to replace it with just a symbolic link, using os.symlink(src,dest). This triggered an error when we tried to create a symbolic link with the name of an existing file. So the code checks first. It uses os.path.lexists(), since that will return True for broken as well as valid links. os.path.exists() returns True for valid links, False for broken ones.
The os.remove() works whether or not the link is broken or valid or an actual file.
if os.path.lexists(dest):
    os.remove(dest)
os.symlink(src,dest)
(For more detail, see also June2013)

Linux: paste and diff -b

tidbits recently of use to me:
  • paste to put two files together by merging their columns, without the complications of join
  • diff -b to ignore blank lines etc, when comparing how my new code is different from the old code.