Home > Blockchain >  In csh, how to make "a/b" from "a/b/c/" using variable modifier?
In csh, how to make "a/b" from "a/b/c/" using variable modifier?

Time:10-29

When x contains directory name which contains / at the end, how can I remove the last path include the / pattern in csh?
Here is an example.

% set x=a/b/c
% echo $x:h
a/b
% set x=a/b/c/
% echo $x:h
a/b/c

So when x is "a/b/c/", I want to make "a/b". How can I do it using variable modifier? (in csh)

I'm also curious about how I can do that in bash. ${x%/} will make "a/b/c" and ${x%%/} will make "a".

ADD : of course I can pre-process the input file to remove / at the end , like sed -e '1,$s/\/$//g' file > file.1; \mv file.1 file' but I'm curious if it's possible with variable modifier.

CodePudding user response:

You can capture the output from dirname(1) in whatever way csh does that:

$ dirname a/b/c
a/b
$ dirname a/b/c/
a/b
  • Related