hi all,
given:
aaaa-bbbb-cccc-dddd-.........and-so-on
how can i build the string:
/aaaa/bbbb/cccc/dddd/aaaa-bbbb-cccc-dddd-.........and-so-on
assuming the first 4 substrings are significant.
thanks for help, ira
assuming '-' is a trailer
try something like
echo your-string| sed -e 's/\([^-]*\)-\([^-]*\)-\([^-]*\)-\([^-]*\)-.*/\/\1\/\2\/\3\/\4\/&/
sure awk, perl and all other can also do it.
function numdashes(str,len,n,o) {
#printf"%s return length = %d counter = %d string index = %d\n", str,len, n, o
for (; o <= length(str) ; o++) {
if (substr(str,o,1) ~ /\-/) {
#print substr(str,o,1)
len = o ; n++
if (n <= 3) {
#printf "Recalling at %d string to return = %s\n", n, substr(str,1,len)
return numdashes(str,len,n,o + 1)
} else {
#print "Final", substr(str,1,len)
return len
}
}
}
return len
}
{
ret = numdashes($0,0,0,0)
if (ret) {
orig = substr($0,1,ret)
tmp = substr($0,1,ret)
gsub(/-/,"/",tmp)
printf "%s%s%s%s\n","/",tmp, orig, substr($0,ret + 1,length($0) - ret + 1)
exit
}
printf "Error\n"
exit
}
iribach:
the statement \([^-]*\)
select a string of any chars but '-' AND remember it!
so: \([^-]*\)- is a string without '-' followed by a '-'
the statement: \1
returns the FIRST remembered string
\2 the second and so on
finally: & return the whole matching line
vox clamantis in deserto.
function mash {
RESULT=`echo "${1}" | sed s/"\-"/"\\/"/1`
RECURSE=$((${2}-1))
if test ${RECURSE} -gt 0
then
RESULT=`mash ${RESULT} ${RECURSE}`
fi
echo "${RESULT}"
}
thank you all
thank you jamisar
your solution is fast enough to solve my problem:
in some directories we have >70'000 files
our programmers have no ideas about sys-admin problems
it ist impossible to work in this dirs, a simply 'ls' takes minutes,
the format is: XXX-YYYY-MM-DD-......
where XXX=prefix, YYYY=year, MM=month, DD=day the rest does not matter.
i will split the dirs in sub-dirs XXX/YYYY,MM/DD and move XXX-YYYY-MM-DD-...... to it
just using a shell-script.
#!/bin/sh
[ x$1 = x -o ! -d $1 ] && echo enter a dir-name && exit 1
exec >/otherdir/logme 2>&1 # i need a status
cd $1 || exit 1
for file in `ls`
do
dir=`echo $file | sed -e 's/\([^-]*\)-\([^-]*\)-\([^-]*\)-\([^-]*\)-.*/\1\/\2,\3\/\4/'`
[ -d $dir ] || mkdir -p $dir || exit 1
mv $file $dir || exit 1
echo $file moved to $dir
done
exit 0
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.