path.gex - GrADS Extensions for Pathname Manipulation and Regular Expressions |
path.gex - GrADS Extensions for Pathname Manipulation and Regular Expressions
abspath PATHNAME - Return the absolute pathname
basename PATHNAME - Return filename part of a pathname
cd PATHNAME - Change current directory, same as chdir
chdir PATHNAME - Change current directory, same as cd
dirname PATHNAME - Return directory name part of a pathname
exists PATHNAME - Check whether a pathname exists
getatime PATHNAME - Get last access time of a pathname
getctime PATHNAME - Get metadata change time of a pathname
getmtime PATHNAME - Get modification time of a pathname
glob PATTERN - Return a list of paths matching a pathname pattern
isfile PATHNAME - Test whether a path is a regular file
isdir PATHNAME - Test whether a path is a directory
match PATTERN STRING - Regular expression matching of string
sed PATTERN STRING - SED-like search and replace
wordexp STRING - Perform word expansion like a POSIX shell
abspath(PATHNAME) - Return the absolute pathname
basename(PATHNAME) - Return filename part of a pathname
chdir(PATHNAME) - Change current directory, same as cd
dirname(PATHNAME) - Return directory name part of a pathname
exists(PATHNAME) - Return 1 if pathname exists, 0 otherwise
getatime(PATHNAME) - Get last access time of a pathname
getctime(PATHNAME) - Get metadata change time of a pathname
getmtime(PATHNAME) - Get modification time of a pathname
glob(PATTERN) - Return a list of paths matching a pathname pattern
isfile(PATHNAME) - Return 1 path is a regular file, 0 otherwise
isdir(PATHNAME) - Return 1 path is a directory, 0 otherwise
match(PATTERN,STRING) - Return 1 if PATTERN matches STRING
sed(PATTERN,STRING) - Return result of SED-like search and replace
wordexp(STRING) - Return string with word expansion like a POSIX shell
This library implements GrADS extensions (gex) with commands for pathname manipulation and perl-compatible regular expression matching, search and replace of strings. In addition, it also provides glob and wordexp commands for shell-like file name and variable expansions; wordexp also provides the back-tick operator for executing shell commands and capture its output. These features are available at the GrADS prompt or as GrADS Scription Language functions.
ga-> abspath . /Users/adasilva
Notice that abspath also expands symlinks, e.g.,
ga-> ! ln -s /opt xxx ga-> abspath xxx /opt
ga-> basename /path/to/somefile.nc4 somefile.nc4
ga-> dirname /path/to/somefile.nc4 /path/to
If you ever tried to use !cd to change directories you realized that it does not work,
ga-> !pwd /Users/adasilva ga-> !cd /opt ga-> !pwd /Users/adasilva
This is because the directory is only changed in the subprocess spawned by the "!" operation; it does not propagate to the environment of of the parent process. The newly introduced chdir command allows you to change the current directory,
ga-> chdir /opt New directory: /opt ga-> !pwd /opt
ga-> exists /usr/bin/tar yes
ga-> exists /usr/bin/!@#$%^^& no
ga-> isfile /usr/bin no ga-> isfile /usr/bin/tar yes
ga-> isdir /usr/bin yes ga-> isdir /usr/bin/tar no
Notice these commands can only be used with existing files or directories,
ga-> isfile /usr/bin/!@#%^& *** ERROR *** cannot stat file /usr/bin/!@#%^&
ga-> getmtime /usr/bin/sed Fri Oct 25 17:05:00 2013
Likewise, you can query the last accesss and metadata change times:
ga-> getatime /usr/bin/sed Mon May 26 15:12:53 2014
ga-> getctime /usr/bin/sed Fri Oct 25 17:05:00 2013
ga-> glob '/opt/X11/*' Found 6 matches /opt/X11/bin /opt/X11/etc /opt/X11/include /opt/X11/lib /opt/X11/share /opt/X11/var
Use the isfile, isdir to find out if these are files or directories, e.g.,
ga-> isdir /opt/X11/include yes
Notice that glob does not perform expansion of the shell home
diretory character ~
,
ga-> glob ~ *** ERROR *** cannot find matches for ~
Use wordexp if you would like expansion of this character.
ga-> wordexp '/opt/X11/include/*.h' Found 5 words /opt/X11/include/Xplugin.h /opt/X11/include/png.h /opt/X11/include/pngconf.h /opt/X11/include/pnglibconf.h /opt/X11/include/xpyb.h
Unlike glob, wordexp expands ~
:
ga-> wordexp ~ Found 1 words /Users/adasilva
When a match cannot be found, wordexp returns the original pattern,
ga-> glob ~/!@#$% *** ERROR *** cannot find matches for ~/!@#$%
ga-> wordexp ~/!@#$% Found 1 words /Users/adasilva/!@#$%
ga-> wordexp 'I am $USER and I reside at $HOME' Found 8 words I am adasilva and I reside at /Users/adasilva
ga-> wordexp "I am `whoami` and I reside at ~" Found 8 words I am adasilva and I reside at /Users/adasilva
ga-> wordexp "Today is `date`" Found 8 words Today is Mon May 26 21:25:12 EDT 2014
ga-> match /open/ OpenGrADS no ga-> match /open/i OpenGrADS yes
ga-> match /^grads/i OpenGrADS no ga-> match /grads$/i OpenGrADS yes
ga->sed s/^open/Open/ opengrads Replaced 1 matches Opengrads
ga-> sed s/GRADS/GrADS/i opengrads Replaced 1 matches openGrADS
The following script shows an example
function main()
* Enable library functions * ------------------------ rc = gsfallow('on')
* Check if a file * --------------- if ( isfile('/usr/bin') ) say '/usr/bin is a file' else say '/usr/bin is NOT a file' endif
* Check if a directory * -------------------- if ( isdir('/usr/bin') ) say '/usr/bin is a directory' else say '/usr/bin a NOT file' endif
* Get list of header files * ------------------------ headers = wordexp('/opt/X11/include/png*.h') say 'PNG header files are: ' headers
* Search and replace * ------------------ new = sed('s/grads/GrADS/ig', 'Opengrads provides GRADS extensions') say new
Running this script produces the following output:
/usr/bin is NOT a file /usr/bin is a directory PNG header files are: /opt/X11/include/png.h /opt/X11/include/pngconf.h /opt/X11/include/pnglibconf.h OpenGrADS provides GrADS extensions
A regular expression (or regex for short) is a sequence of characters that forms a search pattern, primarily used in pattern matching with strings, or in find and replace type of operations. The match and sed functions provided here rely extensively on regular expressions, in particular Perl Compatible Regular Expressions as implmented in the PCRE Library (http://www.pcre.org). If you are not familiar with regular expressions you may want to start with this Quick Tutorial (http://www.regular-expressions.info/quickstart.html), or consult the references under SEE ALSO below for additional information.
The following commands are loosely based on the C-Shell syntax:
Return the absolute pathname associated with PATHNAME. It will
expand the .
and ..
directories as well as follow
symlinks. However, it will not expand the shell character ~
.
Return file name part of PATHNAME. This is a simple string manipulation, there is no need for the pathname involved to exist.
Change current directory, same as chdir. An error is returned if PATHNAME does not exists.
Change current directory, same as cd.
Return directory name part of PATHNAME. This is a simple string manipulation, there is no need for the pathname involved to exist.
Check whether PATHNAME exists. The pathname can be a file or a directory.
Return last access time of PATHNAME.
Return metadata change time of PATHNAME.
Return modification time of PATHNAME.
Return a list of files or directories matching a pathname pattern. The PATTERN is a shell-like pattern involving wildcards, etc., not a regular expression.
Test whether PATHNAME is a regular file. An error is returned if the pathname does not exists.
Test whether PATHNAME is a directory. An error is returned if the pathname does not exists.
Check whether the (perl-compatible) regular expression PATTERN matches STRING.
Return string with the result of a search and replace operation on STRING expressed in the (perl-compatible) regular expression PATTERN. See EXAMPLES above.
Perform word expansion like a POSIX shell. Word expansion means the process of splitting a string into words and substituting for variables, commands, and wildcards just as the shell does. See EXAMPLES above.
As shown in the SYNOPSIS above, each of the command above are provided as a GrADS script function. To enable these Library Functions be sure to add this to the top of your own script:
gsfallow('on')
http://opengrads.org - OpenGrADS Home Page
http://opengrads.org/wiki/index.php - OpenGrADS User Defined Extensions
http://www.iges.org/grads/ - Official GrADS Home Page
Arlindo da Silva (dasilva@opengrads.org)
Copyright (C) 2014 Arlindo da Silva; All Rights Reserved.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
path.gex - GrADS Extensions for Pathname Manipulation and Regular Expressions |