There is a line /hdd/3Tb/ARHIVE/2004/АПРЕЛЬ/Date_mgn4/Account/account.dbf you need to bash 1 to pull out the following: mgn4; Account; Date_mgn4; account.dbf from all this I know that account.dbf can be pulled basename (), and how to get the rest? 2 drive this string into the array so that a (0) = / hdd, a (1) = / 3Tb, etc.
|
3 answers
Option 1: put elements into an array:
p=/hdd/3Tb/ARHIVE/2004/АПРЕЛЬ/Date_mgn4/Account/account.dbf IFS=/ read -aa <<< $p echo ${a[7]} # выведет строку "Account" echo ${a[6]} # выведет строку "Date_mgn4" Option 2: use the dirname and basename programs:
p=/hdd/3Tb/ARHIVE/2004/АПРЕЛЬ/Date_mgn4/Account/account.dbf echo $(basename $(dirname "$p")) # выведет строку "Account" echo $(basename $(dirname $(dirname "$p"))) # выведет строку "Date_mgn4" |
For cutting on a divider there is a utility cut
For the part of the famous line - grep
For complex actions to convert a string to a string, there is sed
|
I solved the nesting problem of the path using find (GNU findutils), tried 30TB on a cache server.
~# find /library -type d -printf "%d %p\n" \ | sort -rn \ | awk "/2\s/{printn $2}" One-liner will create a list of directories sorted by nesting - option % d .
And depending on the task, change the value of awk "/ depth \ s / {print $ 2}"
In your case:
ARRAY_PATH=( $(find /hdd -type d -printf "%d %p\n" | sort -nr | awk '{print $2}') ) The deepest path:
echo ${ARRAY_PATH[1]} Number of ways:
echo $ {# ARRAY_PATH}
|
str="/hdd/3Tb/ARHIVE/2004/АПРЕЛЬ/Date_mgn4/Account/account.dbf"; IFS="/"; a=( $str );str="/hdd/3Tb/ARHIVE/2004/АПРЕЛЬ/Date_mgn4/Account/account.dbf"; IFS="/"; a=( $str );then return the IFS if you need it. And look at the numbering of the elements, the zero will be empty. there is a slant. But how to do the rest is impossible to say because there are no clear criteria for how to get these particular items, by number or by content or something else. As well as highlighting mgn4 underscores or the last 3 letters - MikeIFS=/convenient to use here."${a[6]#*_}"will returnmgn4. See Shell script: How to take part of the string - jfs