linux - append string to file name when string is missing -
i'm trying append string file name , each time string missing.
example :
i have
idbank.xls idbank.xls idbank.xls
i'm looking string
codegroup
as string not exist, i'm appending file name before extension. required output be
idbankxxxcodegroupea1111.xls idbankxxxcodegroupea1111.xls idbankxxxcodegroupea1111.xls
i made script (see below) not working properly
for file in idbank*.xls; if $(ls | grep -v 'codegroupe' $file); printf '%s\n' "${f%.xls}codegroupea1111.xls" fi; done
the grep -v check if string here or not. read on different post can use option -q in checking man , says silent...
any suggestions helpful.
best
this can make it:
for file in idbank*xls [[ $file != *codegroup* ]] && mv $file ${file%.*}codegroup.${file##*.} done
- the
for file
using. [[ $file != *codegroup* ]]
checks if file name containscodegroup
or not.- if not,
mv $file ${var%.*}codegroup.${var##*.}
performed: renames file movingfilename_without_extension
+codgroup
+extension
(further reference in extract filename , extension in bash).
note
[[ $file != *codegroup* ]] && mv $file ${file%.*}codegroup.${file##*.}
is same as:
if [[ $file != *codegroup* ]]; mv $file ${file%.*}codegroup.${file##*.} fi
Comments
Post a Comment