Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file name conversion 1

Status
Not open for further replies.

iribach

Technical User
Oct 12, 2002
211
CH
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.

vox clamantis in deserto.
 
?????????????????? jamisar, please say more
 
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 &quot;Recalling at %d string to return = %s\n&quot;, n, substr(str,1,len)
return numdashes(str,len,n,o + 1)
} else {
#print &quot;Final&quot;, 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(/-/,&quot;/&quot;,tmp)
printf &quot;%s%s%s%s\n&quot;,&quot;/&quot;,tmp, orig, substr($0,ret + 1,length($0) - ret + 1)
exit
}
printf &quot;Error\n&quot;
exit
}

OP:
echo &quot;aaaa-bbbb-cccc-dddd-,dhdikd&quot; | awk -f splitit.awk
/aaaa/bbbb/cccc/dddd/aaaa-bbbb-cccc-dddd-,dhdikd

 
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.
 
[tt]#!/bin/ksh

function mash {
RESULT=`echo &quot;${1}&quot; | sed s/&quot;\-&quot;/&quot;\\/&quot;/1`
RECURSE=$((${2}-1))
if test ${RECURSE} -gt 0
then
RESULT=`mash ${RESULT} ${RECURSE}`
fi
echo &quot;${RESULT}&quot;
}

VAR=&quot;a-b-c-d-e-f&quot;

mash ${VAR} 4

[/tt]
 
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

you get a star (from me and my boss)
ira
 
iribach
appent afte 'do'
[ -d $file ] && continue

this will skip directories in case you have to rerun the script. vox clamantis in deserto.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top