I'm aware I can use awk
to parse on multiple delims, but that spawns subprocesses. I wanted to know if compound/nested bash parameter expansion is possible.
I have PDFs in a directory named like "Px_MM-DD-YY_SSSSSSSSSS.pdf" where:
- "Px" means "Page x", and x has no leading zeroes.
- "MM" corresponds to the two digit month, with a leading zero if applicable.
- "DD" corresponds to the two digit day, with a leading zero if applicable.
- "YY" corresponds to the two digit year, with a leading zero if applicable.
- "SSSSSSSSSS" corresponds to the ten digit epoch time the PDF was created, which allows me to keep PDF page revisions.
I have a for loop (I'll drop "-mtime" when I'm ready to operate on all the PDFs)
for file in $(find -type f -iname '*_??????????.pdf' -mtime -1)
do
echo $file
done
where I want to echo only the epoch time.
I can use this for loop
for file in $(find -type f -iname '*_??????????.pdf' -mtime -1)
do
echo ${file##*_}
done
and for the file named like "./P14_07-21-18_4X_1532144458.pdf", "1532144458.pdf" is echoed to the screen.
I can use this for loop
for file in $(find -type f -iname '*_??????????.pdf' -mtime -1)
do
echo ${file%.*}
done
and for the file named like "./P14_07-21-18_4X_1532144458.pdf", "./P14_07-21-18_4X_1532144458" is echoed to the screen.
If I replace the echo ...
line with any of the formats below
echo ${${file##*_}:0:10}
echo ${(${file##*_}):0:10}
echo ${${file##*_}%.*}
echo ${{file%.*}##_}
echo ${${file%.*}##_}
I get -bash: ... : bad substitution
. Am I not getting the syntax right or is nested/compound bash expansion not possible?
front=${file%.*}; echo ${front##*_}
into thedo
portion of the loop and it worked. Thanks.time
says it's as fast (225ms) as the single part substitution. Piping the file names intoawk
with multiple delims took almost 5 seconds. – user208145 Jul 21 '18 at 04:19