I'm trying to figure out a way to search and replace in the command line. Preferably the solution would work in tcsh. Here's what I want to do.
$ echo $PWD
/projects/apples/001/0440/my/files/are/here/
$ cd 0999
/projects/apples/001/0999/my/files/are/here/
$ cd oranges
/projects/oranges/001/0999/my/files/are/here/
$ cd were
/projects/oranges/001/0999/my/files/were/here/
The script would search from the end of the line and find the first occurrence of a matching directory structure and then cd into that directory.
I'm fluent in python and this script woulds be easy to write in this language but python runs in a process so changing the current directory of one process has no effect on the current work directory of other existing processes.
What is the best way to write such a script? What is the most efficient way of accomplishing this task? My directory structures can get quite large with many thousands of folders so it needs to be quick.
Let example.py
be a script that outputs some shell commands.
#!/usr/bin/python
# example.py
print("cd foo/bar/baz")
Then you can execute the output of that script, in the current shell, with backticks or the $(...)
notation.
mob ~ $ $(./example.py)
mob ~/foo/bar/baz $ echo $PWD
mob ~/foo/bar/baz $ /home/mob/foo/bar/baz