Hi,
I have a cut command and i want to make it parameterized.
e.g
cut -f1-$some_value -d"/" $file_path
so in above e.g
$some_value
could be a number.
$file_path
is a path to a file.
But I am not able to execute the above.
The $file_path actually opens the file .....I want it to be just some path e.g /a/b/c/ and cut fields from 1-$some_value
How can this be achieved ?
VAR=$(echo "$string" | cut -f$whatever)
But depending on what your shell is, there may be far better ways to do this using shell builtins. In bash for instance:
OLDIFS="$IFS" ; IFS="/"
ARR=( $string )
IFS="$OLDIFS"
# Any contents you want from the string are available in ${ARR[0]} to however many you have.
mine is ksh.but what you have suggested wont help me....how do I parametrize it ?
In ksh, you would do:
OLDIFS="$IFS" ; IFS="/"
set -a ARR $string
IFS="$OLDIFS"
...then use the array the same way as in bash.
1 Like
isnt there a simpler way to execute the cut commands with the way I have asked..
I am trying to get the directory of the file using the cut command.
I am not master with unix commands so looking for a simpler command ?
Read my posts again, the 'simple' way is the very first thing I posted. For a quick-fix or something you only do once, it will do.
If you call it in a loop more than once, you will find it is extremely slow and may want to try one of the other options.
I think you want to get only the path of the filename in a string
echo "$path"|cut -d"/" -f1-$somevalue
Also look into
basename path/filename
and
dirname $path/filename