Tuesday, April 14, 2009

UNIX Shell substrings. Cut last substring.

To extract file name from the absolute file path:

Assuming that DIR is something like /u01/oracle/apps/appl/bin/myFile.sh

Then
basename $DIR - gives you the file name. It returns myFile.sh

If you need to return path without the file name :echo $DIR sed 's/`basename $DIR`//'

If you need to return the directory name prior to the file name Then:

echo $DIR | awk -F/ '{print $(NF-1)}'

NF - is built-in AWK variable indicating the total number of fields.
NF-1 - is the field preceeding the last field.
-F - separator indicator - in this case it is /
-f source-file - awk option indicating that source file is found in source-file.


Another built-in variables"

FILENAME - file name which awk is currently reading
FNR - is current record number awk is processing in the current file
NR - is the number of records awk processed so far

1 comments:

Post a Comment